PDF fails to load? The broken xref table explained
Why "Failed to load PDF document" usually means a broken cross-reference table, how it happens, how to spot it, and how to rebuild it without losing pages.
Chrome says "Failed to load PDF document". Acrobat says the file is damaged. Yet the file is the right size, and another viewer opens it fine. The usual cause is the cross-reference table.
What the xref table is
A PDF is a set of numbered objects: pages, fonts, images, text streams. At the end of the file there is a table, the xref, with one line per object giving its byte offset:
xref
0 6
0000000000 65535 f
0000000017 00000 n
0000000081 00000 n
0000000138 00000 n
0000000341 00000 n
0000004170 00000 n
trailer
<< /Size 6 /Root 1 0 R >>
startxref
4302
%%EOF
A reader jumps to startxref, reads the table, and fetches each object directly. It is fast, and it is fragile: if one offset is wrong by a single byte, the reader fetches garbage.
How it breaks
- Text-mode transfers change line endings from
\nto\r\n. Every offset after the first change is now wrong. - Truncated downloads lose the table entirely, because it is at the end.
- Careless editing tools add objects but write the old table.
- Concatenated uploads where a retry appended a second copy to the first.
Why some viewers still open it
Lenient readers, like many on Linux and some mobile apps, notice the table is wrong and scan the whole file for obj markers instead. Strict ones give up. That is why the same file "works on my phone".
How to fix it
Rebuilding the table is exactly the lenient reader's trick, written to disk: scan for every object, record where it really is, and write a correct table. The PDF Repair API does that, then reopens the result and checks the page count before returning it. Its response lists what it found in stats.issues.
Prevent it
- Transfer PDFs in binary mode, and never through tools that touch line endings.
- Check
Content-Lengthon download, and compare the size before you store a file. - Validate a PDF when it enters your system. A failing compression or split call is free and tells you early.
The free PDF repair tool rebuilds one file at a time in the browser.