mirror of
https://github.com/hanzoskill/pdf-form-filler.git
synced 2026-10-30 21:08:02 +00:00
5.4 KiB
5.4 KiB
PDF Form Filler Examples
Quick Examples
Example 1: Simple Application Form
from fill_pdf_form import fill_pdf_form
fill_pdf_form(
"application.pdf",
"application_filled.pdf",
{
"Full Name": "Jane Smith",
"Email": "[email protected]",
"Phone": "555-1234",
"I agree to terms": True,
"Subscribe to newsletter": False,
}
)
Example 2: Event Registration Form
fill_pdf_form(
"event_registration.pdf",
"event_registration_filled.pdf",
{
"First Name": "Alice",
"Last Name": "Johnson",
"Email": "[email protected]",
"Company": "Tech Corp",
"Dietary Restrictions": "Vegetarian",
# Checkboxes
"Attending dinner": True,
"Need accessibility accommodation": False,
"Agree to photography": True,
}
)
Example 3: Batch Processing
from fill_pdf_form import fill_pdf_form
import os
from pathlib import Path
# Template data
person_data = {
"Alice": {"Name": "Alice Johnson", "ID": "001", "Manager Approve": True},
"Bob": {"Name": "Bob Williams", "ID": "002", "Manager Approve": False},
"Carol": {"Name": "Carol Davis", "ID": "003", "Manager Approve": True},
}
# Process each person
for name, data in person_data.items():
fill_pdf_form(
"template.pdf",
f"output/{name}_form.pdf",
data
)
Example 4: List Fields Before Filling
from fill_pdf_form import list_pdf_fields, fill_pdf_form
# See what fields exist
fields = list_pdf_fields("form.pdf")
print(f"Found {len(fields)} fields:")
for field_name, field_type in fields:
print(f" '{field_name}' ({field_type})")
# Now fill with confidence that fields exist
fill_pdf_form("form.pdf", "form_filled.pdf", {
"FirstName": "John",
"LastName": "Doe",
"Verified": True,
})
Example 5: Partial Fill (leave some fields blank)
from fill_pdf_form import fill_pdf_form
# Only fill some fields; others remain blank
fill_pdf_form(
"full_form.pdf",
"partial_form.pdf",
{
"Name": "Jane Doe",
# Email, Phone, etc. left blank
}
)
Common Patterns
Pattern 1: Read CSV, Fill Multiple PDFs
import csv
from fill_pdf_form import fill_pdf_form
with open("applicants.csv") as f:
for row in csv.DictReader(f):
fill_pdf_form(
"application_template.pdf",
f"output/{row['Name']}.pdf",
{
"Full Name": row["Name"],
"Email": row["Email"],
"Date": row["Date"],
"Reviewed": row["Reviewed"].lower() == "yes",
}
)
Pattern 2: Interactive Form
from fill_pdf_form import list_pdf_fields, fill_pdf_form
pdf_path = "form.pdf"
fields = list_pdf_fields(pdf_path)
data = {}
for field_name, field_type in fields:
if field_type == "text":
value = input(f"Enter {field_name}: ")
if value:
data[field_name] = value
elif field_type == "checkbox":
value = input(f"Check {field_name}? (y/n): ").lower() == 'y'
data[field_name] = value
fill_pdf_form(pdf_path, "form_filled.pdf", data)
Pattern 3: Date Formatting
from datetime import datetime
from fill_pdf_form import fill_pdf_form
today = datetime.today()
fill_pdf_form(
"form.pdf",
"form_filled.pdf",
{
"Name": "John Doe",
"Date": today.strftime("%d.%m.%Y"), # German format
"Signature": "J. Doe",
}
)
Pattern 4: Conditional Filling
from fill_pdf_form import fill_pdf_form
# Build data conditionally
user = {"name": "Jane", "department": "Sales", "is_manager": True}
data = {
"Name": user["name"],
"Department": user["department"],
"Manager": user["is_manager"],
"Approval Authority": "VP Sales" if user["is_manager"] else "Manager",
}
fill_pdf_form("form.pdf", "form_filled.pdf", data)
Troubleshooting Examples
Issue: Field not found
from fill_pdf_form import list_pdf_fields
# See what fields actually exist
fields = list_pdf_fields("problem_form.pdf")
print("Available fields:")
for name, _ in fields:
print(f" '{name}'")
# Use the exact names from the list above
Issue: Checkbox not rendering visually
Checkbox value is set correctly, but PDF viewer doesn't show it.
Solution: The value is correct (/On). Try opening with:
- Adobe Reader (renders automatically)
- Firefox
- evince (Linux)
The data is preserved; just the visual appearance stream isn't regenerated by pdfrw.
Issue: Text too long for field
Some PDF fields have narrow character limits.
Solution:
- Use shorter values
- Abbreviate fields (e.g., "Sci" instead of "Science")
- Edit the PDF template to increase field size
# Example: abbreviating titles
title_map = {
"Manager": "Mgr",
"Director": "Dir",
"Vice President": "VP",
}
data = {
"Title": title_map.get(full_title, full_title)
}
CLI Usage
Basic fill from JSON
python fill_pdf_form.py input.pdf output.pdf data.json
Where data.json contains:
{
"Full Name": "John Doe",
"Email": "[email protected]",
"Approved": true
}
Inline JSON
python fill_pdf_form.py input.pdf output.pdf '{"Name":"Jane","Approved":true}'
List fields first
python -c "from fill_pdf_form import list_pdf_fields;
fields = list_pdf_fields('form.pdf')
for name, _ in fields:
print(name)"