amorimluc
amorimluc

Reputation: 1719

How to combine/merge PDFs where one or more have optional content?

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

Answers (3)

johnwhitington
johnwhitington

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:

  1. You have two or more PDFs which happen to have OCGs, and you wish to merge them. This is not too bad.

  2. 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

Caio Agiani
Caio Agiani

Reputation: 61

Try this:

CombinePDF.load("your_file.pdf", unsafe: true, allow_optional_content: true)

Upvotes: 0

B Liu
B Liu

Reputation: 123

With the CombinePDF gem you can add option allow_optional_content although the result can be weird as the one layer pdf format may not know how to show all optional contents. More info can be found here

CombinePDF.load(about_company_pdf_path, :allow_optional_content)

Upvotes: 1

Related Questions