PyPDFForm fills PDF forms from a mapping of field names to values. In the library, that mapping is a Python dictionary; in the CLI, it is a JSON object.
Most fields use flat, non-nested values. Image and signature fields can also use nested CLI objects when you need per-field options such as preserve_aspect_ratio.
Filled forms stay editable by default. Pass flatten=True to fill, or add --flatten to the CLI command, to flatten fields after filling so their values can no longer be edited.
Use string values for text fields and boolean values for checkboxes. The following example fills this PDF:
fromPyPDFFormimportPdfWrapperfilled=PdfWrapper("sample_template.pdf").fill({"test":"test_1","check":True,"test_2":"test_2","check_2":False,"test_3":"test_3","check_3":True,},flatten=False# optional, set to True to flatten the filled PDF form)filled.write("output.pdf")
A radio button group is a collection of radio buttons sharing the same name on a PDF form.
Fill each radio button group with the zero-based index of the option to select. For example, to fill this PDF:
fromPyPDFFormimportPdfWrapperfilled=PdfWrapper("sample_template_with_radio_button.pdf").fill({"radio_1":0,"radio_2":1,"radio_3":2,},flatten=False# optional, set to True to flatten the filled PDF form)filled.write("output.pdf")
A dropdown can be filled with either the zero-based option index or the option text. For example, to fill this PDF:
fromPyPDFFormimportPdfWrapperfilled=PdfWrapper("sample_template_with_dropdown.pdf").fill({"dropdown_1":1},flatten=False# optional, set to True to flatten the filled PDF form)filled.write("output.pdf")
You can also specify a dropdown option by its string value:
fromPyPDFFormimportPdfWrapperfilled=PdfWrapper("sample_template_with_dropdown.pdf").fill({"dropdown_1":"bar"},flatten=False# optional, set to True to flatten the filled PDF form)filled.write("output.pdf")
If you fill a dropdown field with a string value that is not one of its existing options, the new value is added as the last option in the dropdown and automatically selected.
fromPyPDFFormimportPdfWrappersigned=PdfWrapper("sample_template_with_signature.pdf").fill({"signature":"sample_signature.png"},flatten=False# optional, set to True to flatten the filled PDF form)signed.write("output.pdf")
fromPyPDFFormimportPdfWrapperwithopen("sample_signature.png","rb+")assig:signed=PdfWrapper("sample_template_with_signature.pdf").fill({"signature":sig},flatten=False# optional, set to True to flatten the filled PDF form)signed.write("output.pdf")
fromPyPDFFormimportPdfWrapperwithopen("sample_signature.png","rb+")assig:signed=PdfWrapper("sample_template_with_signature.pdf").fill({"signature":sig.read()},flatten=False# optional, set to True to flatten the filled PDF form)signed.write("output.pdf")
By default, PyPDFForm preserves a signature image's aspect ratio. To stretch the image to the field bounds, set the preserve_aspect_ratio property to False on the signature field:
fromPyPDFFormimportPdfWrapperfilled=PdfWrapper("sample_template_with_image_field.pdf").fill({"image_1":"sample_image.jpg"},flatten=False# optional, set to True to flatten the filled PDF form)filled.write("output.pdf")
fromPyPDFFormimportPdfWrapperwithopen("sample_image.jpg","rb+")asimg:filled=PdfWrapper("sample_template_with_image_field.pdf").fill({"image_1":img},flatten=False# optional, set to True to flatten the filled PDF form)filled.write("output.pdf")
fromPyPDFFormimportPdfWrapperwithopen("sample_image.jpg","rb+")asimg:filled=PdfWrapper("sample_template_with_image_field.pdf").fill({"image_1":img.read()},flatten=False# optional, set to True to flatten the filled PDF form)filled.write("output.pdf")
Unlike signature fields, image fields are stretched to the field bounds by default. To preserve the original image aspect ratio, set the preserve_aspect_ratio property to True on the image field: