PDF Radio Button Groups: pypdf vs PyMuPDF, Run for Real
The Day 14 checkbox trap, back one level deeper. In our run, pypdf accepted a radio assignment ({"plan": "/B"}) with no error, but the saved file still read back /Off — the selection did not persist. PyMuPDF did persist it — but at the kid (widget) level: its output reads back 'B' in PyMuPDF, while pypdf's get_fields() still shows the parent field as /Off and the kid as None, so a pypdf-based pipeline cannot see the selection at all. Also: PyMuPDF could not even create radio widgets from scratch in our environment (ValueError: bad xref on every add_widget()); we had to hand-build the fixture. Neither library gave a clean end-to-end radio story.
What we ran
Environment: Python 3.12, pypdf 6.20.0, PyMuPDF 1.26.x. Fixture: a hand-built minimal AcroForm PDF (radio_orig.pdf, 2063 bytes) with one radio group plan and 3 kids (states A/B/C, initial /V /Off). Why hand-built: see the "widget creation" row below. Every value below is a verbatim return from our run.
Step-by-step comparison (verbatim)
| Step | pypdf 6.20.0 | PyMuPDF |
|---|---|---|
| Widget creation (from scratch) | PyMuPDF add_widget() for radio buttons failed 3/3 times with ValueError: bad xref (inside _validate → Parent/Kids lookup). We hand-built the PDF instead; pypdf creation was not attempted separately. | |
| Enumerate fields | get_fields() → {'plan': ('/Btn', '/Off'), 'plan.plan': ('/Btn', 'None')} | page.widgets() → named plan.plan, button_states() → {'normal': ['Off', 'A'/'B'], 'down': None} |
| Radio fill (select B) | w.update_page_form_field_values(pages[0], {"plan": "/B"}, auto_regenerate=False) → "set OK", no exception | widget.field_value = "B"; widget.update() → OK |
| Re-read own output | plan: '/Off' — selection NOT persisted | kid values 'Off' / 'B' — persisted at widget level |
| Cross-read (pypdf reads the other's output) | pypdf reading PyMuPDF's filled file: {'plan': '/Off', 'plan.plan': 'None'} — pypdf cannot see PyMuPDF's selection. PyMuPDF reading pypdf's filled file: [('plan.plan', 'Off'), ('plan.plan', 'Off')] — consistent with pypdf's silent loss. | |
| Side effect | — | repeated MuPDF error: cannot create appearance stream for annotations on our hand-built fixture (cosmetic rendering warning; values still wrote) |
The exact calls we used
pypdf:
from pypdf import PdfReader, PdfWriter
w = PdfWriter(clone_from="radio_orig.pdf")
w.update_page_form_field_values(
w.pages[0], {"plan": "/B"}, auto_regenerate=False)
w.write("radio_pypdf_filled.pdf")
r = PdfReader("radio_pypdf_filled.pdf")
print({k: str(v.get('/V')) for k, v in r.get_fields().items()})
# ran: {'plan': '/Off', 'plan.plan': 'None'} <- silent loss
PyMuPDF:
import pymupdf
doc = pymupdf.open("radio_orig.pdf")
for wd in doc[0].widgets():
if "B" in (wd.button_states() or {}).get("normal", []):
wd.field_value = "B"
wd.update()
doc.save("radio_mupdf_filled.pdf")
d2 = pymupdf.open("radio_mupdf_filled.pdf")
print([(w.field_name, w.field_value) for w in d2[0].widgets()])
# ran: [('plan.plan', 'Off'), ('plan.plan', 'B')] <- persisted, kid-level
Three silent traps, one series
Day 13: wrong passwords return 0 instead of raising. Day 14: pypdf checkbox True doesn't persist. Day 15: pypdf radio fill doesn't persist — and even the library that does persist (PyMuPDF) hides the value from pypdf by writing at the kid level. The rule stands: never trust a no-exception write; re-read the saved file with the same library your downstream pipeline uses — cross-library reads disagreed in both directions in our run.
What we did NOT run
- XFA forms — not run (carried from Day 14's not-run list); no offline XFA fixture available, and XFA is a different spec path entirely.
- pypdf radio persistence root cause (appearance-stream /
/Vpropagation) — not run; we recorded symptom + cross-read evidence, not the code-level cause. - pypdf radio workarounds (writing
/VviaNameObjectdirectly on the field dict) — not run; we kept the failure in the table rather than patching mid-run. - PyMuPDF radio widget creation workaround (constructing
Parent/Kidsmanually beforeadd_widget) — not run; hand-building the raw PDF was faster and also produced the fixture we needed. - Visually rendered check (screenshot of the filled radio) — not run; our fixture's appearance streams were minimal, so rendering is not representative of real-world radio UIs.
Decision rule
Radio groups: use PyMuPDF to select, and if any pypdf-based consumer needs the value, don't rely on get_fields() — it read /Off on a file where the selection was real. Creating radio forms programmatically: neither library gave a one-call path in our environment; use a form designer tool or hand-authored template. As always: verify the saved file, with the reader your pipeline actually uses.
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.