Cookbook

Repair PDF documents

Working with PDF methods such as mergePdf, watermarkPdf, or sign (PDF), requires the documents to be internally correct, that means well-formed, uncorrupted and unprotected.

To repair a PDF document that is not well-formed, the easiest approach is using ghostscript, available for Windows, Linux and macOS.

After installing ghostscript, the command to repair a PDF is:

This command can be called from PHP using passthru or exec functions.

PDF documents that are not well-formed throw PHP Warnings when calling phpdocx PDF methods. These warnings can be catched using a custom error handler, repair the PDF using ghostscript and call again the phpdocx PDF method with the repaired PDF: