Repair corrupt PDFs by API: what can and cannot be fixed
Repair damaged PDFs with one API call. Which kinds of corruption can be fixed, which cannot, how to read the issues report, and why a failed repair is free.
"Failed to load PDF document." "The file is damaged and could not be repaired." Most of these PDFs are not really destroyed. Their pages are still in the file; what is broken is the index that tells a reader where each piece is. The PDF Repair API rebuilds that index.
curl https://api.justapi.tech/v1/pdf/repair \
-H "Authorization: Bearer ak_live_..." \
-H "Content-Type: application/json" \
-d '{"source": {"url": "https://files.example.com/broken.pdf"}}'
What can be fixed
- Broken cross-reference tables. The table at the end of a PDF lists the byte offset of every object. Editing tools and bad uploads often leave it wrong. Repair scans the file and rebuilds it.
- Wrong offsets after a file was edited by a tool that did not update the table.
- Missing end markers from a download that stopped a few bytes short.
- Line ending damage from a file that went through a text-mode FTP transfer or an email gateway.
What cannot
- Pages whose bytes never arrived. If a 10 MB download stopped at 6 MB, the pages after that point are gone. Repair returns the pages it can find.
- Encrypted files with damage in the encryption data. Repair refuses encrypted PDFs with
encrypted_pdf; unlock them first. - Files that were never PDFs, like an HTML error page saved with a .pdf name. You get
unsupported_media_type, at no charge.
The issues report
The response tells you what was wrong:
"stats": {
"pages": 14,
"issues_found": 2,
"issues": ["file has corrupted xref table", "object 37 0 has wrong offset"],
"pages_with_unreadable_content": 0
}
pages_with_unreadable_content counts pages whose content stream could not be read. Those pages are kept but may show up blank. If it is above 0, ask for a fresh copy of the file if you can.
Checked before it is returned
After the rebuild, the API opens the result again and checks that the page count matches what it recovered. If it does not, you get processing_failed and no charge, not a file that looks fixed but is not.
Cost
$0.005 per call at the first tier. A file with no recoverable pages, or that is not a PDF, is free.
Try a damaged file with the free PDF repair tool. For the background, read the broken xref table explained.