If you’re staring at a PDF that refuses to open and all you get is a cryptic error, the problem might be a corrupted trailer. The trailer is the last part of a PDF file and contains critical pointers to the cross-reference table and the root object. When it goes bad, your PDF becomes unreadable. This guide is for anyone who’s comfortable using a hex editor or a command line and wants to fix the trailer themselves. By the end, you’ll have a working PDF (or at least a clear diagnosis of the issue).
We’ll cover two approaches: manual hex editing for precise control, and using qpdf for an automated fix. You don’t need to be a PDF expert — just follow the steps carefully. We assume you’ve already backed up your file because, well, you should always back up before tinkering.
What You’ll Need
- A backup copy of the corrupted PDF (seriously, do this first)
- A hex editor (HxD for Windows, 0xED for Mac, or `xxd` on Linux)
- Basic knowledge of decimal and hexadecimal numbering (or a converter handy)
- Optional: qpdf (cross-platform) or pdftk (older). We’ll use qpdf here.
- A second PDF that opens fine (for reference)
Step 1: Confirm the Trailer Is the Culprit
Before diving in, check if the trailer is actually the issue. Open the PDF in a text editor (like Notepad) or use `hexdump -C` on Linux. Scroll to the very end. You should see the word “trailer” followed by a dictionary, then a line that says `startxref` and a number, and finally `%%EOF`. If any of those are missing or look garbled, the trailer is broken. If you’re dealing with a PDF that won’t open at all, you might need to start with our broader guide on how to repair PDF.

Step 2: Open the PDF in a Hex Editor
Launch your hex editor and open the corrupted PDF. Look at the last 50–100 bytes of the file. A normal trailer looks like this (in ASCII): `trailer<>startxref 789%%EOF`. The numbers vary, but the structure is fixed. The `startxref` number should be the byte offset (0-based) where the cross-reference table begins. While we focus on the trailer here, the cross-reference table often gets corrupted too. See our guide on PDF xref stream repair for that.

Step 3: Locate and Verify the Cross-Reference Offset
The `startxref` value is critical. Use your hex editor’s “Go To” feature to jump to that offset. You should see the beginning of the cross-reference table — either an `xref` keyword or a stream object. If the offset points to garbage or is way past the end of the file, the trailer is lying. In that case, you need to find the real cross-reference table. Search for `xref` in the file. Once you find it, note its byte offset (in decimal). Then go back to the trailer and correct the `startxref` value. Sometimes the trailer itself is fine but the objects it points to are broken. Check our guide on repair PDF objects to fix them.

Step 4: Fix a Missing or Truncated Trailer
If the entire trailer is missing, you can rebuild it from scratch. Find the Root object (usually an `obj` with `Type /Catalog`), note its object number (e.g., `1 0 obj`), and the size of the cross-reference table (count the entries). Then append this at the end of the file: `trailer<>startxref [offset of xref table]%%EOF`. All values are decimal. For example: `trailer<>startxref 12345%%EOF`. Save and try opening. A common symptom is a PDF that suddenly ends without a proper trailer, similar to a PDF end-of-file error.

Step 5: Use qpdf for Automatic Repair
If manual editing feels risky, let qpdf handle it. Install qpdf (available on all platforms). Run `qpdf –check input.pdf` to get a diagnosis. Then try one of these: `qpdf –linearize input.pdf output.pdf` (rebuilds the file, often fixing the trailer), or `qpdf –repair-xref input.pdf output.pdf` (specifically fixes cross-references and trailer). If qpdf reports “no syntax errors” but the file still won’t open, you may have deeper corruption. qpdf is a solid tool for general repair PDF tasks.

Step 6: Verify and Test
Open the repaired PDF in your favorite reader. If it opens without errors, you’re golden. If not, double-check the trailer syntax: ensure no extra spaces, the `/Size` value matches the number of entries in the xref table, and the `startxref` offset is exact. You can also run `qpdf –check` again to see if it passes. Remember: one misplaced byte can break the whole file.
Common Pitfalls
- Forgetting to back up the original — if you mess up, you lose the file.
- Using the wrong numbering system — you want decimal for the `startxref` offset and object numbers.
- Assuming the trailer is the only problem — sometimes the xref or objects are also corrupted, so check those too.
Where to Next
Now that you’ve mastered trailer repair, you might want to explore other PDF fix techniques. Check out our step-by-step guides on repairing PDF objects, fixing cross-references, or recovering data from damaged files. Each builds on the skills you used here. Happy fixing!