boazsegev / combine_pdf

A Pure ruby library to merge PDF files, number pages and maybe more...
MIT License
733 stars 154 forks source link

General PDF error - Combine PDF fails on specific PDF file #207

Closed egibney closed 2 years ago

egibney commented 2 years ago

updated_pdf = CombinePDF.new(pdf_path)

When I run the above code I'm getting the follow error for one specific PDF file only:

RuntimeError (General PDF error - Use CombinePDF.load or CombinePDF.parse for a non-general error message (the requested file was not found OR the string received is not a valid PDF stream OR the file was found but not valid)

I can't share the PDF file here because of its copyright status, but happy to share privately with anyone who may be able to help.

If I open the PDF file in Preview on my Mac and "export as PDF" the resulting file works fine.

The "encoding software" of the offending file is Mac OS X 10.10.2 Quartz PDFContext The "content creator" of the offending file is Final Draft 9

egibney commented 2 years ago

I managed to get things working by changing this:

updated_pdf = CombinePDF.new(pdf_path)

to this:

updated_pdf = CombinePDF.load(pdf_path, allow_optional_content: true)

I made this change after reading this comment from the gem's readme

Sometimes the CombinePDF will raise an exception even if the PDF could be parsed (i.e., when PDF optional content exists)... I find it better to err on the side of caution, although for optional content PDFs an exception is avoidable using CombinePDF.load(pdf_file, allow_optional_content: true).