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:
- A: 5 pages, healthy 5-level outline — A Chapter (depth 1, p1) → A Section (2, p2) → A Sub (3, p3) → A Sub-Sub (4, p4) → A Deep (5, p5)
- B: 5 pages, no outline at all — never set, not empty-but-present; the source read-back confirmed
bookmarks: 0before merging - C: 5 pages, healthy 5-level outline, same shape as A
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
| Title | Depth | Page in merged file | Expected | page_match | text_match |
|---|---|---|---|---|---|
| A Chapter | 1 | 1 | 1 (A+0) | true | true |
| A Section | 2 | 2 | 2 | true | true |
| A Sub | 3 | 3 | 3 | true | true |
| A Sub-Sub | 4 | 4 | 4 | true | true |
| A Deep | 5 | 5 | 5 | true | true |
| C Chapter | 1 | 11 | 11 (C+10) | true | true |
| C Section | 2 | 12 | 12 | true | true |
| C Sub | 3 | 13 | 13 | true | true |
| C Sub-Sub | 4 | 14 | 14 | true | true |
| C Deep | 5 | 15 | 15 | true | true |
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
- day45_results.json — source sanity read-backs (A5/B0/C5), pypdf 10-row output with per-row page+text checks, PyMuPDF 0-row read-back, the set_toc fix verification
- day45_run.py — the full archived run script
- day45_A.pdf · day45_B.pdf (the outline-less source, 2,125 bytes) · day45_C.pdf
- day45_merged_pypdf.pdf — the 10/10 pypdf output
- day45_merged_pymupdf.pdf — the 0-row PyMuPDF output
- day45_merged_pymupdf_fixed.pdf — the 10/10 manually rebuilt output
Honest limits — what we did NOT test
- Two or more outline-less sources in a row (A, B1, B2, C): untested — one empty middle is what today's frame covered.
- An outline-less source first (B, A, C) or last (A, C, B): untested; position effects on the offset math were not run.
- B having an empty-but-present /Outlines tree (as opposed to no /Outlines at all): that shape is covered by the Day43–44 line, not re-run here.
- qpdf / pdftk: untested — this page's scope is the two Python libraries.