Merging PDFs Whose Source Outlines Are Intact: pypdf Carried All 15, PyMuPDF's insert_pdf Carried Zero
Run 2026-11-30 · PyMuPDF source build · two merge tools · 3 sources × 5 pages × 5 outline levels · 0 guessed numbers · every number below read back from the saved files, not asserted.
This is the eighth article in our outline-series. Every test before this one started from a damaged outline — a "small PDF merging a large one got us zero", "a broken level-2 chain lost its row", "cycles and chain breaks vs a 15-row merge". The obvious next question was never directly answered: what happens when the source outlines are perfect? If the files you shuffle already carry a healthy outline, does it arrive in the merged file — or is a healthy source no protection either?
Construction
Same benchmark frame as the previous tests, deliberately: three sources A/B/C, each 5 pages, each with a healthy 5-level outline:
- A: A Chapter (depth 1, p1) → A Section (2, p2) → A Sub (3, p3) → A Sub-Sub (4, p4) → A Deep (5, p5)
- B: same five levels on pages 6–10
- C: same five levels on pages 11–15
The clean merged benchmark is 15 rows, five levels, every row on its own page (offsets A+0 / B+5 / C+10). The critical difference from yesterday: the sources were not broken. Each source outline was built directly with PyMuPDF's set_toc() and read back from the saved file before any merging happened — 5 rows per source, all depth+page readings confirmed from the on-disk files. No broken tree, no NullObject, no cycle, nothing to excuse.
Result 1: pypdf append — 15/15, nothing to rebuild
First sanity: all three source outlines confirmed intact from disk (5 rows each, correct depths and pages) with pypdf before merge.
| Tool (as run) | Rows in merged file (read back) | All pages on offset | What the run shows |
|---|---|---|---|
pypdf: PdfWriter().append(A); append(B); append(C) | 15/15 | true | the intact outline passed through whole; depth structure and titles identical |
PyMuPDF: A.insert_pdf(B, C, links=True) | 0 | — | insert_pdf never carries /Outlines from the sources; links=True made no difference to the outline |
PyMuPDF + manual fix: get_toc() per source, add offset, set_toc() after inserts | 15/15 | true | the rows come back, on the offset pages, only because we rebuilt them by hand |
The pypdf result read back from the saved merged file:
| Title | Depth | Page | Expected | page_match |
|---|---|---|---|---|
| A Chapter | 1 | 1 | 1 (A+0) | true |
| A Section | 2 | 2 | 2 | true |
| A Sub | 3 | 3 | 3 | true |
| A Sub-Sub | 4 | 4 | 4 | true |
| A Deep | 5 | 5 | 5 | true |
| B Chapter | 1 | 6 | 6 (B+5) | true |
| B Section | 2 | 7 | 7 | true |
| B Sub | 3 | 8 | 8 | true |
| B Sub-Sub | 4 | 9 | 9 | true |
| B Deep | 5 | 10 | 10 | true |
| C Chapter | 1 | 11 | 11 (C+10) | true |
| C Section | 2 | 12 | 12 | true |
| C Sub | 3 | 13 | 13 | true |
| C Sub-Sub | 4 | 14 | 14 | true |
| C Deep | 5 | 15 | 15 | true |
15/15 rows, all 15 page_match checks true, all_pages_match: true. Not 14 correct and one off. The intact outline went through pypdf's append exactly as it went in — structure, nesting, titles, page targets at the new offsets, nothing repaired afterwards.
Result 2: PyMuPDF insert_pdf — 0 rows, twice
PyMuPDF's insert_pdf() merged the same three intact sources and produced a merged file with exactly 0 outline rows. We ran it twice: the plain call (saved from the fix pipeline before any reconstruction touched it) and a second run with links=True (the flag that does carry link annotations). Both merged outputs are on disk; both read back at bookmarks_out: 0, rows: []. This is not a damaged-source artifact — the sources were verified healthy before the merge. insert_pdf simply does not transport /Outlines from source documents. A user who assumes PyMuPDF carries outlines the way it carries pages will get a merged file with a perfect page flow and a bare sidebar.
Result 3: the manual rebuild that brings the rows back
The fix is 15 minutes of code, not a flag: before inserting, call get_toc(simple=True) on each source, add the destination offset to each stored page number, and set_toc() the assembled list onto the destination after all inserts (using offsets 0/5/10 in this run). Read back from the saved fixed file: 15/15 rows, all_on_correct_pages: true. The outline you get out is the one you put in — but you carried it yourself.
The one-line verdict
On our files, with outlines intact at merge time: pypdf passes the outline through unchanged; PyMuPDF insert_pdf does not carry outlines at all, and only a manual get_toc + offset + set_toc rebuild brings them back. A clean source is not a protection — it just means the loss, when it happens, is total rather than partial.
Quick FAQ
Both my files have bookmarks — why is the merged PDF's sidebar empty?
If you merged with PyMuPDF insert_pdf: measured outcome, no flag fixes it (we tried links=True on these exact sources; still 0 rows). If you merged with pypdf append, the intact-outline case carried through 15/15 on our files — an empty sidebar usually means a broken-outline shape in a source, which is what these tests catalog.
How do I keep the outline when merging with PyMuPDF?
Do not rely on insert_pdf. Collect each source's get_toc() before inserting, add the offset of that source in the destination, and set_toc() the assembled list after all inserts. Our fixed file read back 15/15 with every row on its offset page — but that is a manual reconstruction, not a passthrough.
Does pypdf transfer outlines automatically?
In the healthy-source case, measured yes: 15/15 rows, pages correct at the merge offsets, zero manual work. For sources with damaged outlines (breaks and cycles), see the companion test — that is where rows start going missing.
Files on disk
- day44_results.json — source sanity read-backs, pypdf 15-row output, PyMuPDF 0-row outputs, the set_toc fix verification
- day44_run.py — the full archived run script
- day44_A.pdf · day44_B.pdf · day44_C.pdf — the three intact sources (2,902 bytes each)
- day44_merged_pypdf.pdf — the 15/15 pypdf output
- day44_merged_links.pdf · day44_merged_pymupdf.pdf — the 0-row PyMuPDF outputs
- day44_merged_pymupdf_fixed.pdf — the 15/15 manually rebuilt output
Honest limits — what we did NOT test
- Mixed sources (outlines present in A, absent in B): untested in this frame — all three sources here carry healthy outlines.
- qpdf / pdftk command-line merges: untested — this page's scope is the two Python libraries.
- PyMuPDF insert_pdf with per-insert separate calls (B and C as separate insert_pdf invocations): read back identically at 0 rows on the plain and links=True outputs; a dedicated third variant was not archived separately.