diff --git a/pdftools/formfill.py b/pdftools/formfill.py index 7f8a477..49ebf5e 100644 --- a/pdftools/formfill.py +++ b/pdftools/formfill.py @@ -9,17 +9,7 @@ from reportlab.lib.units import mm from reportlab.lib.pagesizes import A4 -def parse_text_file(text_file, ignore_first_line=True): - with open(text_file) as fh: - if ignore_first_line: - next(fh) - lines = [line.strip() for line in fh] - - parts = [line.split("\t") for line in lines if line] - return [tuple(map(str.strip, p)) for p in parts] - - -def fill(original_form, draw_function, values, output_file="filled.pdf"): +def fill(original_form, values, draw_function, output_file="filled.pdf"): p = Path(output_file) if p.exists(): p.unlink() @@ -60,42 +50,3 @@ def fill(original_form, draw_function, values, output_file="filled.pdf"): subprocess.run(["open", output_file]) - - - - -if __name__=="__main__": - - attend = parse_text_file("teilnehmertabelle.txt") - - def create_overlay(c, item): - company, addr, name = item - c.setFont("Helvetica", 11) - c.drawString( 75*mm, 234*mm, "Universität Freiburg, IMTEK, CPI") - c.drawString(118*mm, 225*mm, "20126 N") - c.drawString( 58*mm, 216*mm, "VDK-Bestimmung in Jungbier") - - if company.startswith("Wissenschaftsförderung"): - c.setFont("Helvetica", 10) - c.drawString( 35*mm, 204*mm, f"{company}, {addr}") - if company.startswith("Wissenschaftsförderung"): - c.setFont("Helvetica", 11) - c.drawString( 35*mm, 191*mm, f"19.02.2019") - c.drawString(113*mm, 191*mm, f"10:30 Uhr") - c.drawString(150*mm, 191*mm, f"16:00 Uhr") - - c.drawString( 25.8*mm, 177.75*mm, f"X") - - c.drawString( 35*mm, 169*mm, name) - c.drawString( 35*mm, 156*mm, "Teilname Kickoff-Meeting") - - c.drawString( 37.15*mm, 134.35*mm, f"X") - c.drawString( 54*mm, 134*mm, "1") - - c.drawString( 24*mm, 82*mm, "Freiburg, den 19.02.2019") - - c.setFont("Helvetica-Bold", 12) - c.drawString( 117*mm, 100*mm, "1.000") - - #fill("form.pdf", create_overlay, attend) - ruler_overlay("form.pdf") diff --git a/pdftools/ruler.py b/pdftools/ruler.py index e88b6d4..3b9020d 100644 --- a/pdftools/ruler.py +++ b/pdftools/ruler.py @@ -23,4 +23,4 @@ def ruler_overlay(original_form): ofp = Path(original_form) out_path = ofp.parent / f"{ofp.stem}_ruler.pdf" - formfill.fill(original_form, ruler, [1], output_file=out_path) + formfill.fill(original_form, [1], ruler, output_file=out_path) diff --git a/readme.md b/readme.md index 93ab292..7f63d25 100644 --- a/readme.md +++ b/readme.md @@ -6,10 +6,11 @@ This package provides some cli commands: - `nameplates` generate a pdf with nameplates - `addruler` adds a blue ruler to a pdf +And a module to assist with filling out pdf forms: `pdftools.formfill` -nameplates ----------- +nameplates (cli) +---------------- Usage: nameplates [OPTIONS] ATTENDEES [LOGO] @@ -32,8 +33,8 @@ nameplates -addruler --------- +addruler (cli) +-------------- Usage: addruler [OPTIONS] PDF @@ -41,3 +42,39 @@ addruler Options: --help Show this message and exit. + + + +formfill (module) +----------------- + + +The formfill module helps filling out pdf forms with other data. But there is +no command line interface, you have to create a function on where to position +what on the form. + +The function requires some Reportlab knowledge. An example: + + from pdftools.formfill import fill + from reportlab.lib.units import mm + + # `parse_file` is an exercise to the reader + attendees = parse_file("teilnehmertabelle.txt") + + + # function defining where to draw what. + def custom_function(canvas, item): + + # one item consists of three things + company, address, name = item + + canvas.setFont("Helvetica", 11) + canvas.drawString( 35*mm, 204*mm, f"{company}, {address}") + canvas.setFont("Helvetica-Bold", 11) + canvas.drawString( 35*mm, 169*mm, name) + canvas.drawString( 35*mm, 82*mm, "19.02.2019") + + # "form.pdf" should be the path to the form file + fill("form.pdf", attendees, custom_function) + +