Home > Back-end >  Ignore a CombinePdf exception
Ignore a CombinePdf exception

Time:06-16

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

CodePudding user response:

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

  • Related