FillPDF

0.7.3 · active · verified Thu Apr 16

fillpdf is a Python library designed to simplify filling and flattening PDF forms. It leverages `pdfrw` (specifically building on `pdfrw2`), `pdf2image`, `Pillow`, and `PyMuPDF` to provide functionalities such as listing form fields, writing data to fields, flattening PDFs (making them non-editable), inserting images and text, and rotating pages. The library is currently at version 0.7.3 and sees active, though somewhat sporadic, development and releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to list the fields in a PDF, fill those fields with a dictionary of data, and then optionally flatten the resulting PDF to make the fields non-editable. Ensure you have a fillable PDF (`template.pdf`) with known field names. For checkboxes, use specific string values like 'Yes' or 'Off' as dictated by your PDF form.

import os
from fillpdf import fillpdfs

# Create a dummy PDF (replace with your actual fillable PDF path)
# For demonstration, assume 'template.pdf' exists with fields like 'name', 'age', 'is_active'
# You would typically create this PDF using a PDF editor.

# Example of getting form fields
# This step is crucial to know the exact field names in your PDF
try:
    form_fields = fillpdfs.get_form_fields('template.pdf')
    print("Available form fields:", form_fields)
except FileNotFoundError:
    print("Error: template.pdf not found. Please provide a valid fillable PDF.")
    # Create a dummy fillable PDF for testing if not present, e.g., using another library or manually
    # For a real scenario, 'template.pdf' needs to be a pre-existing fillable PDF.
    exit()

# Prepare data to fill the PDF
data_to_fill = {
    'name': 'John Doe',
    'age': '30',
    'is_active': 'Yes' # For checkboxes, typically 'Yes'/'Off' or similar strings
}

input_pdf_path = 'template.pdf'
output_filled_pdf_path = 'filled_form.pdf'
output_flattened_pdf_path = 'flattened_form.pdf'

# Fill the PDF
fillpdfs.write_fillable_pdf(
    input_pdf_path,
    output_filled_pdf_path,
    data_to_fill,
    flatten=False # Set to True to flatten immediately after filling
)
print(f"Filled PDF saved to {output_filled_pdf_path}")

# Flatten the filled PDF (make fields uneditable)
fillpdfs.flatten_pdf(
    output_filled_pdf_path,
    output_flattened_pdf_path
)
print(f"Flattened PDF saved to {output_flattened_pdf_path}")

# You can also insert images or text at specific coordinates if needed
# fillpdfs.place_image('image.png', 100, 100, output_flattened_pdf_path, 'output_with_image.pdf', 1)
# fillpdfs.place_text_box('my_text_field', 'Some extra text', 200, 200, output_flattened_pdf_path, 'output_with_text.pdf', 1)

view raw JSON →