Steph
Steph

Reputation: 27

Ignore a CombinePdf exception

I want to only skip the CombinePdf::ParsingError (Optional Content PDF files aren't supported and their pages cannot be safely extracted.) when i upload a pdf file inside my begin/rescue but it's not working, how can i do ?

begin
  FileManager::PdfValidation.new(uploaded_files)
rescue ParsingError => e
end

Upvotes: 0

Views: 780

Answers (2)

Bruno De Freitas Barros
Bruno De Freitas Barros

Reputation: 2409

Try this:

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

Source: https://github.com/boazsegev/combine_pdf/issues/28#issuecomment-1376413479

Upvotes: 3

gettalong
gettalong

Reputation: 830

If you look at the code of combine_pdf you see that it is possible to parse files with optional content.

Also, the README states:

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

You could try that.

Alternatively, depending on what your use case is, you can try pdf-reader (if you just wanna read something from the PDF) or HexaPDF (which is a fully-featured PDF library; n.b. I'm the author of HexaPDF).

Upvotes: 2

Related Questions