Reputation: 1719
I have a Rails app that needs to be able to combine/merge PDFs where one or more of them have optional content. I'm able to merge PDFs without any issues with the CombinePDF gem when none of them have optional content, but when one does I get the following error:
Couldn't merge PDF: Optional Content PDF files aren't supported and their pages cannot be safely extracted.
I can't find other gems that combine PDFs that have optional content. Could I maybe tell these PDFs to convert their optional content into a certain format that makes them mergeable, or something of the sort? Any help will be appreciated. Thanks.
Upvotes: 1
Views: 740
Reputation: 2763
Merging PDFs with optional content groups is not easy, so many libraries cannot do it, or do it wrongly. There are two situations:
You have two or more PDFs which happen to have OCGs, and you wish to merge them. This is not too bad.
You have two or more PDFs which have OCGs, where you want to merge the OCGs too: for example, if file 1 has "Layer 1" and "Layer 2", and file 2 has "Layer 1" and "Layer 2", and you wish there to be only one "Layer 1" and one "Layer 2" in the output, hiding and showing the content as if the file were created in one go.
For 1) any PDF merger which supports OCGs should be ok, for example:
cpdf 1.pdf 2.pdf 3.pdf -o out.pdf
For 2) you will need to postprocess the result with
cpdf -ocg-coalesce-on-name in.pdf -o out.pdf
Upvotes: 0
Reputation: 61
Try this:
CombinePDF.load("your_file.pdf", unsafe: true, allow_optional_content: true)
Upvotes: 0