Search code examples
ruby-on-railsamazon-s3wkhtmltopdfpdfkit

UnknownNetworkError when retreiving many SVG images from S3 when using wkhtmltopdf with Rails


For a project I am working on, I am using pdfkit/wkhtmltopdf in a rails application to generate a PDF document containing many images. "Many" in this case means 750+. The image assets are relatively small, around 4kb each.

A contrived version of the code looks like this.

- (1..750).each do |i|
  img src="https://bucket.s3.com/prefix/to/image-#{i}/image.jpeg"

This works fine with a set of JPEG images (S3 allows quite a heavy amount of reads). I also have a set of identically named images that are SVG. I'm trying to use SVG in order to reduce the size of the image assets.

Running the same PDF conversion, I run into the following error.

Exit with code 1 due to network error: UnknownNetworkError
[633b88d9-25e7-41a5-a6f6-325829cf2be8]    (0.7ms)  ROLLBACK  ...
[633b88d9-25e7-41a5-a6f6-325829cf2be8] Command failed (exitstatus=1): /Users/harrylewis/.rbenv/versions/2.6.6/bin/wkhtmltopdf --quiet --page-size Letter --margin-top 0.5in --margin-right 0.5in --margin-bottom 0.5in --margin-left 0.5in --encoding UTF-8 --dpi 1000 --orientation Landscape --print-media-type --zoom 1.3 - report.pdf
[633b88d9-25e7-41a5-a6f6-325829cf2be8] method=GET path=/report format=json controller=ReportsController action=report status=500 error='PDFKit::ImproperWkhtmltopdfExitStatus: Command failed (exitstatus=1): /Users/harrylewis/.rbenv/versions/2.6.6/bin/wkhtmltopdf --quiet --page-size Letter --margin-top 0.5in --margin-right 0.5in --margin-bottom 0.5in --margin-left 0.5in --encoding UTF-8 --dpi 1000 --orientation Landscape --print-media-type --zoom 1.3 - document.pdf' duration=9034.00 view=0.00 db=8.59
[633b88d9-25e7-41a5-a6f6-325829cf2be8]
PDFKit::ImproperWkhtmltopdfExitStatus - Command failed (exitstatus=1): /Users/harrylewis/.rbenv/versions/2.6.6/bin/wkhtmltopdf --quiet --page-size Letter --margin-top 0.5in --margin-right 0.5in --margin-bottom 0.5in --margin-left 0.5in --encoding UTF-8 --dpi 1000 --orientation Landscape --print-media-type --zoom 1.3 - document.pdf

However, I am able to get the PDF generation to run successfully with an image count less than about 600.

Is anyone able to provide insight as to what might be happening here?

Some troubleshooting steps I have tried.

  • Ensuring all images I am trying to access are in fact accessible and exist by displaying them on a web page of the application.

Some version information.

  • pdfkit - 0.8.4.2
  • rails - 5.2.4.4
  • wkhtmltopdf - 0.12.5 (with patched qt)

Please let me know if I can provide more information.


Solution

  • I managed to figure out the issue here.

    It turns out there is a known bug in wkhtmltopdf, version 0.12.5, which is mentioned an discussed on this GitHub thread.

    It turns out the issue affects all image assets, not just SVG images. The reason that I only noticed it when trying to use SVG images is because JPEG image errors are ignored/silenced. I was able to further investigate this nuance when running the tool with the --debug-javascript flag.

    According to the GitHub thread, this issue also exists in the latest version of wkhtmltopdf, version 0.12.6.

    Workaround

    One workaround mentioned in the GitHub thread, which worked in my case, was to ise the HTTP protocol in place of HTTPS in the image URL. This introduces some security concerns, so you may or may not be able to do this.