PDF TO MD EXECUTED TEST

Merging a PDF With No Outline Between Two That Have One: pypdf Kept 10/10 and Sailed Clean Past the Empty Middle

Run 2026-12-01 · PyMuPDF source build · pypdf merge + PyMuPDF merge · 3 sources × 5 pages · benchmark 10 rows over 15 pages · 0 guessed numbers · every number below read back from the saved files, not asserted.

This is the ninth article in our outline-series, and it answers the exact question yesterday's test left open in its honest-limits list: Day 44 proved a healthy outline passes through pypdf's append 15/15 when all three sources have one. But real folders are not uniform. The mixed case — some files with bookmarks, some without — was explicitly listed as untested. Today we ran it.

Construction

Same benchmark frame, one deliberate hole:

Merged A+B+C = 15 pages. The correct outcome is 10 rows: A's five on pages 1–5, C's five on pages 11–15 (C's source offset is +10 because B's five outline-less pages still occupy pages 6–10). Both source outlines were read back from disk before any merging — nothing repaired, nothing assumed.

Result 1: pypdf append — 10/10, and the offset math handled the gap

TitleDepthPage in merged fileExpectedpage_matchtext_match
A Chapter111 (A+0)truetrue
A Section222truetrue
A Sub333truetrue
A Sub-Sub444truetrue
A Deep555truetrue
C Chapter11111 (C+10)truetrue
C Section21212truetrue
C Sub31313truetrue
C Sub-Sub41414truetrue
C Deep51515truetrue

10/10 rows, all 10 page checks and all 10 text checks true, all_pages_match: true. The empty middle source cost zero rows: pypdf computed C's page targets from B's actual page count (10 + 5), not from B's bookmark count, and every C row landed on the page whose text matches the source marker. Compare the four-source test where every source lacked an outline and the formula held at 0 rows kept — here, only the sources that have rows contribute rows, and the ones that don't, don't drag anybody down.

Result 2: PyMuPDF insert_pdf — still 0 rows

The same three mixed sources through insert_pdf() read back at bookmarks_out: 0 — identical to the all-healthy case in Day 44. The absence of outlines in B changed nothing because insert_pdf never transports /Outlines from any source, healthy or not. Same manual fix as yesterday closes the gap: collect get_toc() per outline-carrying source, add that source's page offset, set_toc() after inserts. Our fixed file read back 10/10 rows on pages 1–5 and 11–15, all on correct pages (the fix script simply had no B rows to carry, and that absence is correct, not a loss).

The one-line verdict

A source with no outline is harmless to pypdf's append — it contributes zero rows and disturbs nobody else's; the page-offset math handled the 5-page gap exactly. The only way to lose the outline entirely is PyMuPDF insert_pdf, which drops it regardless of what the sources carry. Mixed folders are safe on pypdf; on PyMuPDF you rebuild by hand, from the sources that actually have rows.

Quick FAQ

One of my files has no bookmarks — will merging it delete the other file's bookmarks?

Measured no, on pypdf: the outline-less middle source in our run cost zero rows and both healthy sources came through whole (10/10, every row on the right page). Watch out instead for the damaged-outline shapes — a broken chain or a cycle in a with-outline source is what actually eats rows; see the break tests.

Why do the last source's bookmarks point at pages 11–15 when it's the third file?

Because page offsets come from page counts, not bookmark counts: the middle file has no bookmarks but still contributes 5 pages, so the third file's content starts at page 11. pypdf got this right automatically (pages 11–15, all text-verified). If you rebuild the outline manually with PyMuPDF's set_toc(), add the same page-count offset — bookmark counts will mislead you.

Files on disk

Honest limits — what we did NOT test