Why does PDFKit/wkhtmltopdf hang but renders PDF as expected when Rails app is killed?

二次信任 提交于 2019-11-27 17:31:21
scarver2

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:

  1. https://github.com/mileszs/wicked_pdf/issues/110
  2. http://wearepandr.com/blog/article/homebrew-and-installing-old-package-versions#blog_nav

Try the last version. The easy way install on MacOS:

brew install Caskroom/cask/wkhtmltopdf
user778174

The fix by scarver2 worked for me as advertised. But I needed a more recent version of wkhtmltopdf. Since the homebrew version still seems outdated (it still hangs on the command line), and since there isn't a recent precompiled binary available, I used the os x build script to compile one myself:

$ git clone git@github.com:wkhtmltopdf/wkhtmltopdf.git
$ cd wkhtmltopdf
$ ./build_osx.sh     # i'm running 10.9.2
$ bin/wkhtmltopdf --version                                                                                  
Name:
  wkhtmltopdf 0.12.1-72a9f2066fe9ffd162dec007a4d9b6a5cd63b670
$ curl www.example.com | bin/wkhtmltopdf - test.pdf  # render pdf of example.com
$ open test.pdf   # to confirm pdf 

I'm using pdfkit 0.6.2 with Rails 3.2.17. I put the binary in /vendor and, in a pdfkit initializer, pointed to it with config.wkhtmltopdf. So far, so good.

Jacky

I got the same issue. It works when I added: 'config.threadsafe!' in application.rb as the answer in the stack. Hope this help.

Mine was also hanging and opening the wkhtmltopdf icon in the dock.

I actually found the problem for me was I only had 1 unicorn worker process running. Once I added more than 1 it worked fine.

I'm running wkhtmltopdf 0.9.9 on OS X with pdfkit 0.6.2

Zorak

Exact same symptoms but using WickedPdf currently. At this point, I believe the issue lies with wkhtmltopdf as well.

Unfortunately, neither of the recommendations I've been able to find in Stack/Google worked for me. Instead, I needed to combine several suggestions, including some found in this post.

Solution was:

  1. brew uninstall wkhtmltopdf
  2. find and delete any copies of wkhtmltopdf in /usr/bin
  3. comment WickedPdf.config line in config/initializers
  4. add config.threadsafe! to development.rb
  5. remove middleware and allow 'show' action of main controller to handle pdf requests (may not be necessary)
  6. add wkhtmltopdf-binary to gemfile
  7. bundle
  8. restart server
  9. You may also need to add Mime::Type.register_alias "application/pdf", :pdf to config/initializers/mime_types.rb (for me, this causes 'warning: already initialized constant PDF')

My set-up was: Mac OSX Mountain Lion with Rails 3.2.1, Webrick, Postgres and wkhtmltopdf-binary (0.9.9.1).

Jigar Bhatt

Try editing config/initializer/pdfkit.rb in the following way:

PDFKit.configure do |config|

config.wkhtmltopdf = Rails.root.join('bin', 'wkhtmltopdf-i386').to_s

config.default_options = {

  :encoding=>"UTF-8",
  :page_size=>"A4",
  :margin_top=>"0.25in",
  :margin_right=>"0.1in",
  :margin_bottom=>"0.25in",
  :margin_left=>"0.1in",
  :disable_smart_shrinking=> false
}
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!