pdf2mdtool.com — real-run series, Day 18

The Smallest File That Reproduces pypdf's Missing Radio Pick — and the Read That Recovers It

Two widgets and about thirty lines pin the gap we've seen three times now. In the minimal file, pypdf's get_fields() prints the radio field as {'/T': 'opt', '/FT': '/Btn', '/V': 'Off', '/_States_': ['/Off', '/Yes']} — no /Kids entry at all — while the same pypdf reader, walked one level lower through the page's /Annots array, shows /AS: /Yes on the selected widget. The selection exists in the file; pypdf's field summary just doesn't carry it. And the low-level walk is the recovery recipe: filtering annotations for any /AS that isn't /Off returned [('opt', '/Yes', [72.0, 80.0, 92.0, 100.0])] — group name, on-state, and the rectangle identifying which option was picked.

The fixture, built to be minimal

Installed with pip install pymupdf pypdf, no pins; versions printed inside the run's own log: pypdf 6.20.0 | PyMuPDF 1.28.2. The build uses Day 17's finding — field_value = False before add_widget, the line we separated from every other variable in one process — so file construction is deterministic on this environment: a single 612×200 page, two radio widgets named opt, then one selection written through PyMuPDF (selected: 'Yes') and saved. Nothing else is in the file, which is what makes the reads below attribution-free: one file, two readers, two layers.

One file, both layers, verbatim

ReadVerbatim output from this run
pypdf get_fields(), field layer{'opt': {'/T': 'opt', '/FT': '/Btn', '/Ff': 32768, '/V': 'Off', '/_States_': ['/Off', '/Yes']}} — dict keys ['/T', '/FT', '/Ff', '/V', '/_States_'], /Kids: None
pypdf annotation layer, same file/T: opt | /AS: /Yes | /Rect: [72, 80, 92, 100] and /T: opt | /AS: /Off | /Rect: [72, 40, 92, 60]
Recovery read (/AS walk)recovered selections via annot /AS: [('opt', '/Yes', [72.0, 80.0, 92.0, 100.0])]

One discrepancy stays exactly as printed: the two annotation rectangles (y=40..60 and y=80..100) sit lower on the page than the two construction rects (y=100..120, y=140..160). Both widgets place consistently within the file; the offset between construction coordinates and saved annotations is recorded, not explained — we change one variable at a time, and this run's variable was the read path, not the layout math.

Exactly what we ran

import pypdf, pymupdf
print("RUN VERSIONS pypdf:", pypdf.__version__, "| pymupdf:", pymupdf.__version__)
# ran: RUN VERSIONS pypdf: 6.20.0 | pymupdf: 1.28.2

# build: two kids named opt, field_value=False before add (Day 17's flip condition)
doc = pymupdf.open()
page = doc.new_page(width=612, height=200)
for y in (100, 140):
    w = pymupdf.Widget()
    w.rect = pymupdf.Rect(72, y, 92, y+20)
    w.field_type = pymupdf.PDF_WIDGET_TYPE_RADIOBUTTON
    w.field_name = "opt"
    w.field_value = False
    page.add_widget(w)
doc.save("min_repro.pdf")

# select the first kid
d = pymupdf.open("min_repro.pdf")
for wd in d[0].widgets():
    non_off = [s for s in (wd.button_states() or {}).get("normal", []) if s != "Off"]
    if non_off:
        wd.field_value = non_off[0]; wd.update()   # ran: selected: 'Yes'
        break
d.save("min_repro_filled.pdf")
# the recovery read, as run
from pypdf import PdfReader
r = PdfReader("min_repro_filled.pdf")
sel = []
for a in r.pages[0]["/Annots"]:
    ao = a.get_object()
    asv = ao.get("/AS")
    if asv not in (None, "/Off"):
        sel.append((str(ao.get("/T")), str(asv), [float(x) for x in ao.get("/Rect")]))
print("recovered selections via annot /AS:", sel)
# ran: [('opt', '/Yes', [72.0, 80.0, 92.0, 100.0])]

From three observations to one recipe

Days 15–17 recorded the gap on three different fixtures; today's page compresses it to the smallest file that still shows it, and adds the one line of code that turns the lost selection back into data. The pattern across all four runs reads the same way: PyMuPDF writes the choice into the kid annotations (/AS), pypdf's get_fields() summarizes the parent (/V, which stays Off), and the two never meet. We keep that as a repeated, unexplained — but fully reproducible — description. The recipe's limits are real: it returns rectangles rather than logical option labels, and it assumes annotation walking matches the reader version's object model.

Not run on this page, each with its reason

Both code blocks are the run itself: versions printed first, outputs inline, logs kept as day18_minrepro_log.txt and day18_recovery_log.txt.