pdf2mdtool.com — real-run series, Day 16

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

StepVerbatim from this run
Field before editfield before: plan /Off
Direct /V write + kid /ASkid /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 effectMuPDF error: unsupported error: cannot create appearance stream for annotations (×2, cosmetic; values intact)
PyMuPDF radio widget creation, fresh envsingle 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

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.