fill pdf with data implemented

This commit is contained in:
Bela 2018-02-22 21:58:16 +01:00
parent debe75e11e
commit d1cff966ea

View file

@ -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"])