Fixing pypdf's Silent Radio Loss — and a Widget-Creation Reversal
It works. Writing /V directly on the parent field dict with NameObject — plus setting the chosen kid's /AS — made pypdf's radio selection persist: the saved file re-read as 'plan': '/B', and PyMuPDF's cross-read saw the same 'B' that Day 15's plain update_page_form_field_values path lost. Second finding, and it cuts against our own Day 15 page: PyMuPDF's add_widget() for a radio button succeeded this time (saved file shows ('plan', 'RadioButton')) where Day 15 logged 3/3 ValueError: bad xref failures. Same fixture shape, but the environment between the two runs is not the same: the sandbox was wiped, packages were reinstalled, and PyMuPDF reads as 1.28.2 in this run's log while the Day 15 page recorded 1.26.x. We record the flip as measured and do not attribute it — the unisolated variables are listed in Not run, item 1.
Where this run comes from
This is the workaround branch we explicitly refused to patch mid-run on Day 15, now run for real. Environment: a fresh sandbox (our previous one was wiped between sessions — we say so because it changes one result below). Installed with pip install pymupdf pypdf, no version pins; the versions printed by the run itself, in its own log: pypdf 6.20.0, PyMuPDF 1.28.2. Fixture: rebuilt by hand to the same Day 15 spec — one radio group plan, 3 kids (A/B/C), initial /V /Off, 2063 bytes. Every value below is a verbatim line from this run's logs.
Run results, line by line
| Step | Verbatim from this run |
|---|---|
| Field before edit | field before: plan /Off |
Direct /V write + kid /AS | kid /AS set to /B (no exception) |
| pypdf re-read of saved file | {'plan': '/B', 'plan.plan': 'None'} — selection persisted, unlike Day 15's /Off |
| PyMuPDF cross-read of pypdf's output | [('plan.plan', 'Off'), ('plan.plan', 'B')] — the 'B' is visible cross-library |
| Rendering side effect | MuPDF error: unsupported error: cannot create appearance stream for annotations (×2, cosmetic; values intact) |
| PyMuPDF radio widget creation, fresh env | single radio widget added OK → saved file: [('plan', 'RadioButton')] — reverses Day 15's 3/3 ValueError: bad xref |
The workaround, exactly as run
import pypdf, pymupdf
print("RUN VERSIONS pypdf:", pypdf.__version__, "| pymupdf:", pymupdf.__version__)
# ran: RUN VERSIONS pypdf: 6.20.0 | pymupdf: 1.28.2
from pypdf import PdfReader, PdfWriter
from pypdf.generic import NameObject
w = PdfWriter(clone_from="radio_orig.pdf")
acro = w._root_object["/AcroForm"]
fld = acro["/Fields"][0].get_object()
fld[NameObject("/V")] = NameObject("/B") # the piece update_page_form_field_values missed
for kref in fld["/Kids"]:
k = kref.get_object()
n = k.get("/AP").get_object().get("/N")
if n and "/B" in n.get_object():
k[NameObject("/AS")] = NameObject("/B") # mark the selected kid
w.write("radio_pypdf_fixed.pdf")
print({k: str(v.get('/V')) for k, v in PdfReader("radio_pypdf_fixed.pdf").get_fields().items()})
# ran: {'plan': '/B', 'plan.plan': 'None'}
Why this matters beyond radio
Day 15's silent loss and today's fix point at the same mechanism: the convenience API updated part of the field state and skipped the rest. The general rule that survived both runs: when a form write matters, drop to the object layer, set every state slot the spec requires (/V on the parent, /AS on the kid), save, and re-read with both libraries. And when a library "can't" do something — like Day 15's widget-creation failures — re-test after any environment change before you write it off; our own page needed correcting on exactly that point.
Still not run, with reasons
- Whether Day 15's bad-xref failure was version-caused or sandbox-caused — not run. Between the two runs the sandbox was wiped, packages were reinstalled, and the PyMuPDF version moved from the Day 15 page's recorded 1.26.x to this run's logged 1.28.2 — two variables changed at once with no isolation, so the cause of Day 15's failure stays unattributed.
- Full multi-kid radio creation from scratch with PyMuPDF (a 3-option group built purely via
add_widget) — not run; today we only verified a single radio widget, and group semantics need the kid-linking path we haven't exercised. - Appearance streams for the fixed file — not run; MuPDF still refused to render our hand-built annotation appearances, and visual verification of hand-authored streams needs a real form-designed PDF.
- XFA forms — still not run; no offline XFA fixture, carried unchanged from Day 14.
- pdf.js / browser-side reading of the fixed file — not run; our verification stopped at pypdf + PyMuPDF cross-reads.
Every line quoted above comes from this run's saved logs, versions included. Where we corrected our own Day 15 page, we say so in place instead of quietly rewriting it.