How to Repair a PDF XREF Table (Step-by-Step Guide)

Ever opened a PDF only to get a cryptic ‘XREF table is corrupted’ or ‘Invalid cross-reference stream’ error? You’re not alone. The XREF (cross-reference) table tells your PDF reader where each object is located in the file. When it gets damaged—whether from a sudden crash, incomplete download, or buggy software—the whole document can seem dead. This guide is for anyone who’s comfortable with a command line and wants to fix that error themselves. By the end, you’ll have a working PDF with a clean XREF table, no expensive software needed.


We’re going to use the free, open-source tool qpdf, which can automatically rebuild the XREF structure. If qpdf can’t fully fix it, I’ll also show you how to do a manual repair with a hex editor. The steps are straightforward—just copy-paste some commands and let the tools do the heavy lifting.


What You’ll Need


  • Your corrupted PDF file (make a backup first!)
  • qpdf installed (download from sourceforge.net/projects/qpdf/ or use a package manager like apt, brew, or choco)
  • A terminal or command prompt
  • PDF viewer to test the result (e.g., Adobe Acrobat, Chrome, or Evince)
  • (Optional) A hex editor like HxD (Windows) or Hex Fiend (Mac) for manual repair


Step 1: Diagnose the Problem


First, confirm that the XREF table is indeed the issue. Open a terminal and run pdfinfo (comes with poppler-utils) on your file:


If you see errors like ‘Cross-reference stream not found’ or ‘Invalid XREF entry’, you’ve got an XREF problem. Also note the file size and number of pages if it reports any—useful info later. If pdfinfo is not installed, try opening the PDF in a viewer and check the error dialog.


how to repair pdf xref terminal window showing pdfinfo output with corrupted PDF XREF error

Step 2: Backup Your File


Always work on a copy. Run:


Now if something goes wrong, you’ve got a fallback. Keep the original untouched.


how to repair pdf xref file manager showing duplicate PDF file named backup with copy suffix

Step 3: Use qpdf to Rebuild the PDF


The simplest way to fix an XREF table is to have qpdf rewrite the entire file with a fresh cross-reference structure. In the terminal, run:


The –linearize flag tells qpdf to produce a linearized PDF (which includes a valid XREF table), and –object-streams=disable ensures objects are stored in the traditional way. If that fails due to severe corruption, try just:


This modifies the file in-place and often recovers broken XREF data. After running, check fixed.pdf (or the original if using –replace-input) in a viewer. If it opens without errors, you’re done.


how to repair pdf xref command line showing qpdf -linearize command execution with progress output

Step 4: Manual XREF Repair (if qpdf failed)


If qpdf can’t fix it, the XREF table may be pointing to a wrong offset. You can manually correct it using a hex editor. Open the original backup (not the qpdf output) in your hex editor. The XREF table usually starts with the bytes ‘xref’ near the end of the file. Search for ‘xref’ and check the entries. Each line is a byte offset. Compare with the actual objects by searching for ‘obj’ strings. If an offset is off, correct it by editing the hex values. This is advanced—only attempt if you understand PDF structure. Save and test.


how to repair pdf xref hex editor window showing PDF file with 'xref' highlighted and object offsets

Step 5: Verify the Fixed PDF


Open your fixed PDF in multiple viewers to make sure it renders correctly. Also run pdfinfo again and check for any warnings. If all looks good, you’ve successfully repaired the XREF table. If not, try the online tools listed in our best online pdf repair guide as a last resort.


how to repair pdf xref PDF viewer window displaying successfully opened document with no error messages

Common Pitfalls


  • Forgetting to backup: If the repair goes wrong, you lose the original. Always make a copy.
  • Using the wrong tool: Some online tools may add watermarks or fail on large files. Stick with qpdf or manual hex editing for control.
  • Not checking dependencies: qpdf requires a proper install. On Windows, add it to PATH; on Linux, use sudo apt install qpdf. Without it, commands won’t run.


Where to Next


Now that you’ve fixed the XREF, you might be interested in other common PDF issues. Check out our pdf repair tips for troubleshooting random errors. If you need to fix missing images after repair, our guide on repair pdf and restore images can help. And for fillable forms that broke, see repair fillable pdf.

Leave a Reply

Your email address will not be published. Required fields are marked *