PDF TO MD EXECUTED TEST

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:

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 offsetWhat the run shows
pypdf: PdfWriter().append(A); append(B); append(C)15/15truethe 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 inserts15/15truethe rows come back, on the offset pages, only because we rebuilt them by hand

The pypdf result read back from the saved merged file:

TitleDepthPageExpectedpage_match
A Chapter111 (A+0)true
A Section222true
A Sub333true
A Sub-Sub444true
A Deep555true
B Chapter166 (B+5)true
B Section277true
B Sub388true
B Sub-Sub499true
B Deep51010true
C Chapter11111 (C+10)true
C Section21212true
C Sub31313true
C Sub-Sub41414true
C Deep51515true

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

Honest limits — what we did NOT test