pdf2mdtool.com — real-run series, Day 14

PDF Form Filling: pypdf vs PyMuPDF, Run for Real

Text fields and comboboxes filled fine in both libraries during our run. The differences that matter: pypdf accepted a checkbox assignment (True) but the saved file still read back /Off — the checkmark did not persist, while PyMuPDF's checkbox round-tripped to 'Yes'. And pypdf 6.20.0 has no flatten API at all (AttributeError on both page and writer level); PyMuPDF's doc.bake() flattens in one call and leaves zero fields behind. If your pipeline needs reliable checkbox fills or flattening, PyMuPDF won both head-to-heads.

What we ran

Environment: Python 3.12, pypdf 6.20.0, PyMuPDF 1.26.x (pip wheels). Test file: a generated 1-page AcroForm PDF (form_orig.pdf) with 4 fields: two text fields (full_name, email), one checkbox (subscribe), one combobox (country with China/USA/Germany). Every value below is a verbatim return from our run, not a rewording of docs.

Field-by-field comparison (verbatim)

Steppypdf 6.20.0PyMuPDF
Enumerate fieldsget_fields() → ['full_name', 'email', 'subscribe', 'country']iterate page.widgets() → 4 widgets, types Text/Text/CheckBox/ComboBox
Text field fillw.update_page_form_field_values(pages[0], {"full_name": "Zhang San"}) → no exceptionwidget.field_value = "Zhang San"; widget.update() → no exception
Combobox fill{"country": "USA"} → OKfield_value = "USA" → OK
Checkbox = Trueno exception, but re-read of saved file: 'subscribe': '/Off' — not persistedTrue accepted; re-read: 'Yes'
Re-read saved file{'full_name': 'Zhang San', 'email': '[email protected]', 'subscribe': '/Off', 'country': 'USA'}{'full_name': 'Zhang San', 'email': '[email protected]', 'subscribe': 'Yes', 'country': 'USA'}
Flattenno API: page.flatten → AttributeError; writer.flatten() → AttributeError: 'PdfWriter' object has no attribute 'flatten'doc.bake() → OK; flattened file: 0 widgets, get_fields() → None

The exact calls we used

pypdf:

from pypdf import PdfReader, PdfWriter

r = PdfReader("form_orig.pdf")
print(list(r.get_fields().keys()))

w = PdfWriter(clone_from="form_orig.pdf")
w.update_page_form_field_values(w.pages[0], {"full_name": "Zhang San"})
w.update_page_form_field_values(w.pages[0], {"email": "[email protected]"})
w.update_page_form_field_values(w.pages[0], {"subscribe": True})   # ran OK — did NOT persist
w.update_page_form_field_values(w.pages[0], {"country": "USA"})
w.write("form_pypdf_filled.pdf")

PyMuPDF:

import pymupdf

doc = pymupdf.open("form_orig.pdf")
for widget in doc[0].widgets():
    if widget.field_name == "full_name":
        widget.field_value = "Zhang San"; widget.update()
    elif widget.field_name == "subscribe":
        widget.field_value = True; widget.update()   # re-reads as 'Yes'
    ...
doc.save("form_mupdf_filled.pdf")

doc2 = pymupdf.open("form_mupdf_filled.pdf")
doc2.bake()          # flatten widgets into page content
doc2.save("form_mupdf_flat.pdf")

The checkbox trap, in one line

pypdf drew no error when we set True — the failure was silent, exactly like the wrong-password silent failures we measured on Day 13. If you fill checkboxes with pypdf in production, verify the saved file (get_fields() should show /Yes, not /Off) before shipping; we stopped at verbatim observation and did not chase the underlying appearance-stream cause.

What we did NOT run

Decision rule

Filling text/choice fields: either library works — PyMuPDF's per-widget loop is more discoverable, pypdf's dict-per-page call is terser. Checkboxes: use PyMuPDF, or verify pypdf's output after writing. Flatten: PyMuPDF bake(); with pypdf 6.20.0 there is no one-call equivalent in our environment, so don't plan your pipeline around one.

Every return value above is from our own run on this exact environment. We list what we didn't run, with reasons, instead of padding the table with numbers we didn't produce.