PDF Generation hangs using PDFKit and wkhtmotopdf
Turns out it had nothing to do with the cli and it was actually due to Jammit running in development. Disabling Jammit fixes the PDFkit hanging issue.
generating pdf hangs on rails 4 using PDFkit gem
The issue was due to stylesheet_link_tag
and javascript_include_tag
using relative URLs, which often causes wkhtmltopdf
to hang when loading assets from the same server that wkhtmltopdf
is running on.
Using absolute URLs for assets solved the problem.
Set asset_host
in Rails' config, which also affects stylesheet_link_tag
and javascript_include_tag
:
# Modify asset host config setting in `config/application.rb`
# Or create a new initializer: `config/initializers/wkhtmltopdf.rb`
config.action_controller.asset_host = "http://mysite.com"
# Or you can have different hosts for development (local) and production (CDN):
# In `config/environments/development.rb`
config.action_controller.asset_host = "http://localhost"
# In `config/environments/production.rb`
config.action_controller.asset_host = "http://d111111abcdef8.cloudfront.net"
PDFKIT hangs while generating PDF in controller but not in console
Check https://github.com/pdfkit/pdfkit#troubleshooting
If you have some assets in that template that should be served by your local server, then in development in may hang depending on your server configuration. Your current request to render PDF "blocks" the server and it cannot respond to subsequent requests for assets (images, css, js). If possible, serve them from CDN, use multi-process server config in development etc.
PDFkit hangs when generating a pdf with an image
This is a notorious issue, and you're running into it because your probably have relatively linked assets in your HTML (i.e. images, CSS, JS, fonts, etc), and your web server is only capable of handling one request/thread at a time (like WEBrick
).
So what happens? The server begins generating the PDF when you request its URL. PDFkit finds a linked asset, so it tries to load this asset from the server, which happens to be the same server that PDFkit is running on. However, the server's single thread is already busy running PDFkit, so it cannot "free up" to serve the requested asset. In conclusion, it's a deadlock -- PDFkit is awaiting on an asset on the same server that is waiting for PDFkit to finish up processing, so that it can serve the asset to PDFkit...
Solution: either Base64-embed your assets in the HTML so that PDFkit doesn't need to make any additional requests (my personally preferred solution), or temporarily offload the assets to another server (e.g. a temporary AWS bucket). You can also try using the unicorn
or Thin
webserver with multi-threading enabled, or adding config.threadsafe!
in in application.rb
, but there is no guarantee that these methods will work.
Of course, these hacks (embedding assets or hosting elsewhere) should only be used in the dev environment -- you shouldn't be running into these kinds of issues in production, as the live server should (hopefully) be able to handle multiple GET requests.
Why does PDFKit/wkhtmltopdf hang but renders PDF as expected when Rails app is killed?
The problem is with wkhtmltopdf itself, specifically, any version after 0.9.9. wkhtmltopdf hangs when run directly from the command-line.
Steps to correct:
brew uninstall wkhtmltopdf
cd /usr/local/Library/Formula/
git checkout 6e2d550 /usr/local/Library/Formula/wkhtmltopdf.rb
brew install wkhtmltopdf
Then verify the correct version is installed wkhtmltopdf --version
which should yield wkhtmltopdf 0.9.9
Citations:
- https://github.com/mileszs/wicked_pdf/issues/110
- http://wearepandr.com/blog/article/homebrew-and-installing-old-package-versions#blog_nav
Related Topics
Rails 3 + Daemons Gem: Exception When Querying Model
Rails - How to Add Contacts to Sendgrid Marketing Campaigns via API
Routing Error During "Ruby on Rails-Tutorial"
Rspec Error 'Report_Activate_Error': Could Not Find Rubygem Rspec-Core (>=0) (Gem:Loaderror)
Can't Find Ffi.H When Installing Ffi Ruby Gem
Ruby Devise, Sessionscontroller.Create, JSON - Getting Nameerror: Undefined 'Build_Resource'
With Nokogiri I am Getting Error "Initialize': Getaddrinfo: No Such Host Is Known. (Socketerror)"
Don't the Ruby Methods Instance_Eval() and Send() Negate the Benefits of Private Visibility
No Such File to Load -- Soap4R -- Why
Sorting of 2D Array by Its Amount of in the Inner Elements
Rails Migration Complains About Undefined Method 'Attachment' Using Paperclip
Differencebetween Def Func(Var) and Def Func=(Var)
Issue with Precision of Ruby Math Operations
How to Ignore Irrelevant Methods When Profiling Ruby Applications