Skip to content

Fill PDF forms⚓︎

PyPDFForm fills a PDF form using a flat, non-nested dictionary. The filled form is editable by default. When you call the fill method, you can set the optional parameter flatten to True to flatten the filled form, making it uneditable.

Fill text field and checkbox⚓︎

To fill a text field, provide a string value, and for a checkbox, use a boolean value. The following example demonstrates how to fill this PDF:

from PyPDFForm import PdfWrapper

filled = 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")

Fill radio button group⚓︎

A radio button group is a collection of radio buttons sharing the same name on a PDF form.

A PDF form with radio button groups can be filled using integer values, where the value indicates which radio button to select within each group:

from PyPDFForm import PdfWrapper

filled = 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")

Fill dropdown field⚓︎

As with radio buttons, a dropdown choice can be selected by specifying its integer value. For example, to fill this PDF:

from PyPDFForm import PdfWrapper

filled = 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:

from PyPDFForm import PdfWrapper

filled = 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")
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 enables signing a PDF form with a handwritten signature image.

To fill a signature field, consider this PDF and this signature image:

from PyPDFForm import PdfWrapper

signed = 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")
from PyPDFForm import PdfWrapper

with open("sample_signature.png", "rb+") as sig:
    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")
from PyPDFForm import PdfWrapper

with open("sample_signature.png", "rb+") as sig:
    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, the library preserves the aspect ratio of the signature image when filling it. You can disable this by setting the preserve_aspect_ratio property to False on the signature field:

from PyPDFForm import PdfWrapper

pdf = PdfWrapper("sample_template_with_signature.pdf")
pdf.widgets["signature"].preserve_aspect_ratio = False
pdf.fill(
    {
        "signature": "sample_signature.png"
    },
)

pdf.write("output.pdf")

Fill image field⚓︎

Fill an image field similarly to a signature field, using a file path, file object, or file stream.

To fill an image field, consider this PDF and this image:

from PyPDFForm import PdfWrapper

filled = 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")
from PyPDFForm import PdfWrapper

with open("sample_image.jpg", "rb+") as img:
    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")
from PyPDFForm import PdfWrapper

with open("sample_image.jpg", "rb+") as img:
    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 the signature field, the library does not preserve the aspect ratio of a regular image by default. You can enable this by setting the preserve_aspect_ratio property to True on the image field:

from PyPDFForm import PdfWrapper

pdf = PdfWrapper("sample_template_with_image_field.pdf")
pdf.widgets["image_1"].preserve_aspect_ratio = True
pdf.fill(
    {
        "image_1": "sample_image.jpg"
    },
)

pdf.write("output.pdf")