| name | pdf-form-filling |
| description | Methods for programmatically filling PDF forms (AcroForms) and saving the results using pypdf. |
PDF Form Filling Skill
This skill covers how to fill out AcroForms in a PDF document using pypdf.
Prerequisites
- Python 3.x
pypdf library (pip install pypdf)
Usage Patterns
Filling Text Fields and Checkboxes
from pypdf import PdfReader, PdfWriter
def fill_pdf(input_pdf_path, output_pdf_path, data_dict):
reader = PdfReader(input_pdf_path)
writer = PdfWriter()
for page in reader.pages:
writer.add_page(page)
writer.update_page_form_field_values(writer.pages[0], data_dict)
with open(output_pdf_path, "wb") as output_stream:
writer.write(output_stream)
data = {
"full_name": "John Doe",
"is_citizen": "/Yes"
}
fill_pdf("blank.pdf", "filled.pdf", data)
Important Considerations
- Field Naming: Use the full hierarchical name found during inspection (e.g.,
SC-100[0].Page1[0].Field[0]).
- Appearance Streams: Some PDF readers might not show the filled value until you click on the field unless
/NeedAppearances is set to true in the PDF's Interactive Form Dictionary. pypdf handles basic filling, but complex forms might need flattening or specific appearance settings.
- Checkboxes: Checkbox values are typically
/Yes (or other export value) and /Off.