Skip to main content

An Apache-licensed package for extracting, creating, filling, and flattening PDF Forms.

Project description

formalpdf

formalpdf is an Apache-licensed python library for PDF forms. It's a unified API for extracting, creating, filling, and flattening forms. It has a similar, but not drop-in, high-level API to PyMuPDF. All of this is possible by wrapping pdfium, thanks to the low-level bindings made available through pypdfium2.

Installation

Using uv

uv pip install formalpdf

Using pip

pip install formalpdf

CLI Usage

🚧 Soon you can use formalpdf to fill a PDF from an FDF document using:

formalpdf fill <input.pdf> <input.fdf> <output.pdf>

Programmatic Usage

Get All Widget Objects in a PDF

from formalpdf import Document

doc = Document("/path/to/your.pdf")

for page in doc:
    widgets = page.widgets()

A Widget object has information about the location, type, and contents of form fields/wdigets in the PDF. For isntance, a widget might look like:

Widget(
    # name of the widget
    field_name='Text110',
    # label/alternate name, if provided
    field_label='Date (MM/DD/YYYY)',
    # current value (always a string, even if checkbox or combobox)
    field_value='',
    # widget type enum value
    field_type=6,
    # widget type string value 
    field_type_string='Text',
    # widget location Rect
    rect=Rect(
        top=36.95610046386719,
        left=473.5320129394531,
        bottom=24.171100616455078,
        right=587.177978515625
    )
)

Filling out Forms: Updating Widget Values

Let's say we had some textbox widget:

w = doc[0].widgets()[0]

We can update it with:

w.update("New Value")

doc.save("new_doc.pdf")

And when we open new_doc.pdf we'll find a the value filled out!

Creating Forms

🚧 Work in Progress

Adding Annotations (Highlights, Links, etc.)

🚧 Work in Progress

Rendering, Extracting Text, Extracting Images

🚧 Work in Progress

Navigating Unsupported Operations

You can access the raw PdfDocument from pypdfium2 using by calling:

from formalpdf import Document

doc = Document("/path/to/your.pdf")

pdfium_doc = doc.document

You can use this to do lower-level operations that aren't yet supported by formalpdf. For instance, if you want to render the first page of the document (currently an unsupported option):

from formalpdf import Document

doc = Document("/path/to/your.pdf")

pdfium_doc = doc.document
bmp = pdfium_doc[0].render(scale=scale)
pil = bmp.to_pil()

Testing

uv run pytest

There are a large number of test PDFs found in tests/data.

Roadmap

  • create PyPI package
  • finish widget extraction
  • widget updating
    • textbox
    • checkbox
    • combobox
  • widget creation
  • tests

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

formalpdf-0.1.4.tar.gz (1.9 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

formalpdf-0.1.4-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file formalpdf-0.1.4.tar.gz.

File metadata

  • Download URL: formalpdf-0.1.4.tar.gz
  • Upload date:
  • Size: 1.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.16

File hashes

Hashes for formalpdf-0.1.4.tar.gz
Algorithm Hash digest
SHA256 eb1a586b63a1cea8c3af8dedf0cba4e088d2f454010f8f5410f38eab05729e89
MD5 2fa690771a513393baeb59d46e84f3f4
BLAKE2b-256 617695149370a001e4963d5f464e4bd64f9bb0351528884451fa6a57e05903f4

See more details on using hashes here.

File details

Details for the file formalpdf-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: formalpdf-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.16

File hashes

Hashes for formalpdf-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b286f3320a60374e3faa8416d24c63d46f1acd99e4da1ebd25a9c63af5595ed0
MD5 151d8b310a200c439bbeb69ecf6d851a
BLAKE2b-256 d2c50e110b9e106062b5fc5d6b3987f85c48d837211a2e4b752ccd08f3a26e22

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page