Fill PDF forms⚓︎
PyPDFForm fills PDF forms from a mapping of field names to values. The library accepts this mapping as a Python dictionary. The CLI fill command accepts a YAML or JSON file or a set of field options.
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 the library's fill method, or add --flatten to the CLI fill command, to prevent further edits to the filled fields.
CLI input methods⚓︎
The fill command can read field values from a YAML or JSON file supplied with --file / -f:
Alternatively, pass the mapping directly by using each field name as an option:
pypdfform fill sample_template.pdf \
--test test_1 \
--check true \
--test_2 test_2 \
--check_2 false \
--test_3 test_3 \
--check_3 true \
-o output.pdf
When --file and field options are supplied together, --file takes precedence.
Fill text field and checkbox⚓︎
Use string values for text fields and boolean values for checkboxes. The following example fills this PDF:
Fill radio button group⚓︎
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:
Fill dropdown field⚓︎
A dropdown can be filled with either the zero-based option index or the option text. For example, to fill this PDF:
Note
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.
Fill signature field⚓︎
A signature field can be filled with a handwritten signature image.
The examples below use this PDF and this signature image:
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:
Fill image field⚓︎
An image field accepts the same input types as a signature field: a file path, an open file object, or a bytes stream.
The examples below use this PDF and this image:
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: