JGUIMONT>COM

Words of Julien Guimont

Posts tagged pdfkit

13 notes

PDFKit and its middleware on Heroku

I run into this problem with PDFKit middleware where the PDF generation seems to hang indefinitely for no apparent reason on Heroku as well as locally when using Thin.

Wkhtmltopdf will load the HTML and then try to load all assets from it (js, css, images, etc.) On Heroku the asset is first served up by the rails server and when you have a single Dyno it will create a deadlock in the handling of the HTTP request. The user trying to access the pdf document will consume the Dynos and the PDF generator will wait for it. Deadlock.

Two ways around that:

  1. Access the static files directly from the web server and not Rails
  2. Access the static files locally

The first option requires some setup (cloudfront and such) and some actions at deployment. Overall this requires to much work.

The second option is the one I preferred. I used Rails asset_host to have all assets for a PDF generation fetch locally (file://). Here is the gist of it:

Works as intended! Hope this will save some people the time I spent on this.

(Note: You first have to follow Matt’s instructions on how to install wkhtmltopdf on Heroku )

Filed under pdfkit rails heroku