If you’re a Linux user and you’ve ever tried to open a PDF only to be greeted by an error message, you know how frustrating it can be. Maybe the file downloaded incompletely, or a cloud sync messed up the structure. Whatever the cause, don’t panic. This guide is for anyone who needs to repair a PDF on Linux—whether you’re a developer, sysadmin, or just someone who prefers open-source tools. By the end, you’ll know how to fix common PDF corruptions using free command-line utilities like Ghostscript and qpdf, and you’ll have a handy workflow for tackling any broken PDF.
We’ll cover everything from a quick fix with a built-in PDF repair checker to deep-level surgery on xref tables. You don’t need to be a terminal wizard—I’ll walk you through each command. So open a terminal, grab a coffee, and let’s get that PDF working again.
What You’ll Need
- A Linux system (Ubuntu, Debian, Fedora, Arch, etc.) with sudo access
- A terminal emulator (like GNOME Terminal or Konsole)
- The corrupted PDF file you want to repair
- Ghostscript (gs) – install via your package manager
- qpdf – install via your package manager
- Optionally, a hex editor (like ‘hexdump’ or ‘xxd’) for advanced debugging
Step 1: Install Ghostscript and qpdf
First, make sure you have the essential tools. On Debian/Ubuntu, run:
sudo apt update && sudo apt install ghostscript qpdf
For Fedora, use dnf: sudo dnf install ghostscript qpdf. On Arch, it’s pacman -S ghostscript qpdf. Once installed, you’re ready to fix files.

Step 2: Try a Simple Ghostscript Rebuild
Ghostscript can often repair a corrupted PDF by reinterpreting its contents. Run this command, replacing ‘broken.pdf’ with your file:
gs -o repaired.pdf -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress broken.pdf
This tells Ghostscript to output a new PDF using the pdfwrite device. The -dPDFSETTINGS=/prepress flag preserves high quality. If the file is only mildly corrupt, this alone may fix it. Open repaired.pdf in your viewer—if it looks good, you’re done!

Step 3: Use qpdf to Linearize or Rebuild the PDF
qpdf is another powerhouse for fixing PDF structural issues. It can linearize (optimize for web) or just copy and rebuild the file. Try:
qpdf –linearize broken.pdf repaired.pdf
If that fails, use the –replace-input option to overwrite the original after a backup: cp broken.pdf broken.pdf.bak && qpdf –linearize broken.pdf broken.pdf. The linearization process often fixes common corruptions like lost cross-reference data. For more stubborn issues, refer to our dedicated pdf xref stream repair guide.

Step 4: Fix an Invalid PDF Header
Sometimes the PDF file starts with garbage. Use a hex viewer to check the first line: head -c 20 broken.pdf | xxd. A valid PDF starts with ‘%PDF-1.x’ (e.g., %PDF-1.4). If not, you can manually prepend the header: echo -n ‘%PDF-1.4’ > header && cat header broken.pdf > fixed.pdf. But a cleaner way is to use qpdf’s –check option, which can detect header issues. If you see an ‘invalid header’ error, run: qpdf –check broken.pdf and then repair with the linearize flag as above. For more details, see our article on how to fix corrupted pdf.

Step 5: Handle End-of-File Errors
A common error is ‘PDF end-of-file’ or ‘trailer not found’. This usually means the file is truncated. First, check if you have a complete copy—maybe re-download it. If not, you can try to recover as much as possible. Ghostscript can often produce a valid PDF even from a truncated file by ignoring the missing trailer. Run the same gs command from Step 2. If it fails, try qpdf with the –no-original-objects flag: qpdf –no-original-objects –linearize broken.pdf repaired.pdf. This tells qpdf to rebuild object references from scratch. For a deeper dive, check our pdf end of file error repair guide.

Step 6: Advanced – Manually Patch the Xref Table
If nothing works, the xref table (cross-reference) is likely broken. Use qpdf to inspect: qpdf –check broken.pdf. It will list errors like ‘offset mismatch’. You can then use a hex editor to manually fix offsets, but that’s tedious. Instead, try qpdf’s –warning-exit-0 flag to suppress errors and force a rebuild: qpdf –warning-exit-0 –linearize broken.pdf repaired.pdf. If you’re really stuck, our pdf repair checker tool can analyze the file. For a complete walkthrough of xref repairs, see the pdf xref stream repair article.
Common Pitfalls
- Running out of disk space during repair – Ghostscript can create large temporary files. Always check ‘df -h’ before processing big PDFs.
- Not backing up the original – If the repair command overwrites your file and fails, you lose everything. Always cp broken.pdf broken.pdf.bak first.
- Ignoring permissions – If the PDF is owned by root, run commands with sudo if needed, but be careful. Use ‘chmod’ to fix user permissions after repair.
Where to Next
Now you’ve got a solid workflow for repairing PDFs on Linux. If you encounter specific errors like form field corruption or cloud sync problems, check out our guides on fix pdf forms and repair pdf after cloud sync. And if you prefer a graphical interface, many PDF viewers like Okular have built-in repair options. Happy fixing!