From d1cff966eaa60b6a285d9342752d13aeee6752e1 Mon Sep 17 00:00:00 2001 From: Bela Date: Thu, 22 Feb 2018 21:58:16 +0100 Subject: [PATCH] fill pdf with data implemented --- fillform.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/fillform.py b/fillform.py index 8a22a63..3f99da7 100755 --- a/fillform.py +++ b/fillform.py @@ -18,7 +18,9 @@ Outputs are: import os.path import argparse import itertools +import subprocess as cmd import commands +import fdfgen # import formfield import readformdata @@ -82,6 +84,25 @@ def parse(): return args +def fillpdf(fields, pdf, pdfout, fdf): + """Fill a pdf form with data. + + Attributes: + fields ([FormField]): list of FormFields that already have values + saved in ["Value"]. + pdf (str): path to the pdf form that is to be filled + pdfout (str): path to the pdf form after it is filled with data + fdf (str): path to the temporarily created fdf file + + """ + values = [(field["FieldName"], field["Value"]) for field in fields + if "Value" in field] + values = fdfgen.forge_fdf("", values, [], [], []) + with open(fdf, "wb") as fdf_file: + fdf_file.write(values) + cmd.Popen(["pdftk", pdf, "fill_form", fdf, "output", pdfout]) + + if __name__ == "__main__": try: arguments = parse() @@ -96,7 +117,10 @@ if __name__ == "__main__": comms = itertools.chain(*[commands.Command.extractCommands( field, config, data) for field in config]) # comms = commands.Command.extractCommands(config[0], config, data) - print(comms) + # print(comms) for command in comms: command() - print(config[0]) + # print(config[0]) + fillpdf(config, arguments["pdffile"], arguments["outputpdf"], + arguments["fdf"]) + readformdata.writeFields(config, arguments["form"])