Python port of the PHP forge_fdf library by Sid Steward
PDF forms work with FDF data. I ported a PHP FDF library to Python a while back when I had to do this and released it as fdfgen. I use that to generate an fdf file with the data for the form, then use pdftk
to push the fdf into a PDF form and generate the output.
pip install fdfgen
You (or a designer) design the form.pdf
in Acrobat.
Mark the form fields and take note of the field names. This can be done either through Acrobat or by installing pdftk and entering the command line
pdftk [pdf name] dump_data_fields
Let's say your form has fields "name" and "telephone".
Use fdfgen to create a FDF file:
#!/usr/bin/env python
from fdfgen import forge_fdf
fields = [('name', 'John Smith'), ('telephone', '555-1234')]
fdf = forge_fdf("",fields,[],[],[])
with open("data.fdf", "wb") as fdf_file:
fdf_file.write(fdf)
Then you run pdftk to merge and flatten:
pdftk form.pdf fill_form data.fdf output output.pdf flatten
and a filled out, flattened (meaning that there are no longer editable form fields) pdf will be in output.pdf
.
TypeError
in python 3.6 by Tom Grundy (@caver456)pip install -r requirements-tests.txt
tox
to run tests for all Python versions.tox -e py27 -- tests/test_encoding.py