0

Tech Stack used

Ruby 2.5.8, Rails 5.0.7, Ruby Gems: combine_pdf

Here is my sample code that is working for most of the PDF Urls hosted in S3, except one PDF.

@pdf = CombinePDF.new
@pdf << CombinePDF.parse(Net::HTTP.get_response(URI.parse(s3_pdf_url)).body)
@pdf = @pdf.to_pdf

Above code is working as expected for 90% of the PDF files, there is one file that is not working for me. I don't see any exceptions generated and not sure how to debug it.

CombinePDF do generates the final pdf and it is blank as shown in this screenshot_link.

Any feedback would be appreciated.

Here is my another example code. I'm writing original S3 PDF into the Tempfile and using CombinePDF.load() to combine the PDF which is there inside tempfile. But still this PDF is not combined and no exception generated. When I opened tempfile(MyPDF20220921-21580-1qzgy1u.pdf) I can see and access PDF.

path = s3_pdf_url
t = Tempfile.new(["MyPDF", '.pdf'])
t.write(open(path).read.force_encoding(Encoding::UTF_8))
t.close
@pdf = CombinePDF.load t.path
@pdf = @pdf.to_pdf
siv rj
  • 1,451
  • 1
  • 14
  • 31
  • Yes. File seems corrupted although it is accessible on browser but not valid to merge. – siv rj Sep 21 '22 at 17:00
  • I already tried by passing argument `allow_optional_content: true`. But that so called corrupted file is not seen in the final combined PDF output. However other 2 PDF files are seen in the final output. – siv rj Sep 22 '22 at 04:20
  • `Encoding::UndefinedConversionError "\xE2" from ASCII-8BIT to UTF-8` this err is throwing while file write, I guess Ruby tries to apply default character encoding while`file.write` so i used it to write file without error. Also I have tried by `File.open(t.path, 'wb')` opening the temp file in `wb` mode and then I write the file. But still pdf is not merged, so, this later approach (open the file in binary mode and write it with original file) also corrupted the file? – siv rj Sep 22 '22 at 11:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248263/discussion-between-siv-rj-and-k-j). – siv rj Sep 22 '22 at 14:36

0 Answers0