37

Background

After reading around it seemed to me that Prawn is out and wkhtmltopdf is in. It also seems like the PDFKit and wicked_pdf gems for Rails are the new cool. So I found a screencast by Ryan on how to use PDFKit. I installed everything, tested wkhtmltopdf on the CLI with no problems, fiddled around with Rails settingsto run multiple processes so the asset pipeline works, and all seemed good, except I'm still stuck at the very end of the process (actually getting the PDF response from the server).

Question

When I request a .pdf version of my view (I'm using the PDFKit Middleware option) my browser just sits there waiting for a response, but as soon as I kill the Rails process the PDF I expected to get only then pops up in my browser window. What gives?

What I'm Using

  • OS: OSX 10.8.1
  • Rails: 3.2.8
  • Ruby: 1.9.3
  • wkhtmltopdf: 0.11.0_rc1 (although when I run wkhtmltopdf -V it says 0.10.0_rc2)
  • qt: 4.8.2

What I've Done

  • used the PDFKit middleware by loading config.middleware.use "PDFKit::Middleware" in my application.rb file.
  • included gem 'pdfkit' in my Gemfile and installed it with Bundler
  • set the .pdf mime-type in my mime_types.rb initializer with Mime::Type.register_alias "application/pdf", :pdf
  • added config.threadsafe! to config/environments/development.rb for multiple threads so asset pipeline doesn't conflict with PDF engine
  • tested wkhtmltopdf http://www.google.com google.pdf and it generated a PDF of the Google homepage as expected
  • tried swapping PDFKit for wicked_pdf and encountered the same problem (hanging, but when Rails process is killed the PDF renders as expected)

What it Looks Like

This is the regular html page rendered by Rails (I've blurred the client details): enter image description here

This is the CLI output by Rails when I try to navigate to localhost:3000/some/path.pdf. (the app hangs while waiting for a response): enter image description here

When I finally kill the Rails process with ctrl-c the PDF finally shows up in the browser as I expected to see it (CSS and HTML rendered properly, so assets seem to load fine): enter image description here

Conclusions So Far

Swapping PDFKit for wicked_pdf and getting the same results seems to make me think the problem isn't with those libraries, but something to do with my development environment. But wkhtmltopdf runs fine off the command line, so that makes me think that it and QT are doing their job. The problem must be in Rails. Maybe I'm not configuring something properly?

Plea for Help

How do I determine what exactly the problem is and how do I fix it?

I'll love you if you can help me <3

Update

I've also tried using an alternative method of rendering the PDF (with .to_pdf) without the middleware option as follows (doing this I commented out config.middleware.use "PDFKit::Middleware" from my application.rb file):

respond_to do |format|
    format.html
    format.pdf do
        html = '<html><body>This is a test.</body></html>'
        @pdf = PDFKit.new(html)

        send_data @pdf.to_pdf, 
            :filename => 'whatever.pdf', 
            :type => 'application/pdf', 
            :disposition => 'attachment'
    end
end
robmclarty
  • 2,215
  • 2
  • 20
  • 21
  • I wonder if this might have something to do with OSX 10.8? I'm waiting on my client to setup a production server on which I'll test things out again. Maybe a combination of using a multi-threaded server + a different *nix environment will help. Regardless this won't help me in development. – robmclarty Sep 24 '12 at 13:50

7 Answers7

70

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
Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
scarver2
  • 7,887
  • 2
  • 53
  • 61
  • 6
    Steps 2-4 in a single command: `brew install https://raw.github.com/mxcl/homebrew/6e2d550cf4961129a790bfb0973f8e88704f439d/Library/Formula/wkhtmltopdf.rb` – mfilej May 02 '13 at 11:16
  • 3
    If you have Ruby installed, you can bypass all of this Homebrew manipulation and just `gem install wkhtmltopdf`. Delete the existing Homebrew symlink: `rm /usr/local/bin/wkhtmltopdf`. Then link to the gem's binary e.g. `ln -s ~/.rvm/gems/ruby-2.0.0-p0/bin/wkhtmltopdf /usr/local/bin/wkhtmltopdf`. Use `which wkhtmltopdf` command to find the binary. Add `gem 'wkhtmltopdf'` to the GemFile. If using the `wicked_pdf` gem, set its configuration to `WickedPdf.config={exe_path: '/usr/local/bin/wkhtmltopdf'}`. References: wkhtmltopdf gem (0.1.2) installs wkhtmltopdf binary version 0.8.3 patched qt. – scarver2 May 12 '13 at 14:45
  • 4
    The gemfile ```gem 'wkhtmltopdf-binary'``` currently installs 0.9.9 for those interested in using a gem instead of brew – genkilabs Feb 11 '14 at 21:36
  • 1
    I was attempting to use 0.11.x and after pulling all my hair out (including chest hair) I found this answer and wept like a child. You, my friend, have saved me. – Clifton Labrum Feb 28 '14 at 05:25
  • Even 0.9.9.3 is bad? I have these issues just after upgrading an app from Rails 3 to Rails 4 and Ruby from 1.9.3 to 2.2.1. I tried several versions of wkhtmltopdf ranging from 0.9.9 to 0.12.2.1 and generating PDF hangs for every version. I think that this must be related to Ruby version or PDFkit gem itself. – januszm Sep 25 '15 at 11:36
  • @januszm Many people are reporting success with the `wkhtmltopdf-binary` gem. If you need to run the latest version of wkhtmltopdf, try this fork of the gem https://github.com/dmitryrck/wkhtmltopdf_binary_gem – scarver2 Sep 26 '15 at 12:44
  • In my case it fails on images on Rails 4. After I removed all image tags from the html template (converted to pdf) it works (but no images). It worked well on Rails 3.2 so this is clearly related to the Rails upgrade. It's explained here: http://stackoverflow.com/questions/20377401/pdfkit-hangs-when-generating-a-pdf-with-an-image – januszm Sep 26 '15 at 18:54
  • @scarver2 referring to your comment on Sep 26, 2015, does that mean the latest version of wkhtmltopdf no longer has the problem? Would we need to use the fork of the gem you recommended or could we just install the latest version with brew? – Marklar Oct 15 '15 at 05:16
  • 2
    @Marklar The `wkhtmltopdf_binary` gem actually bypasses any Homebrew installation and uses the version found within gem's bin folder. The author, Dmitry Rocha, has been keeping the binaries current. – scarver2 Oct 15 '15 at 16:52
  • @scarver2 thanks for taking the time to answer. Are you sure the binaries are current in the gem even though the gem hasn't been updated in 4 years? – Marklar Oct 15 '15 at 21:18
  • 1
    @Marklar Use this branch https://github.com/dmitryrck/wkhtmltopdf_binary_gem It was updated last month. – scarver2 Oct 16 '15 at 12:45
  • if you are getting `Please switch this formula to SHA256.` when installing the old version try `brew install https://gist.githubusercontent.com/semanticart/389944e2bcdba5424e01/raw/9ed120477b57daf10d7de6d585d49b2017cd6955/wkhtmltopdf.rb`. See: https://gist.github.com/semanticart/389944e2bcdba5424e01 – michael Nov 16 '15 at 19:12
  • Note that this now needs to be `brew cask uninstall wkhtmltopdf` and `brew cask install wkhtmltopdf`. – Serenthia Dec 02 '19 at 16:09
9

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

brew install Caskroom/cask/wkhtmltopdf
Abel
  • 3,989
  • 32
  • 31
2

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.

Community
  • 1
  • 1
user778174
  • 27
  • 6
1

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

Community
  • 1
  • 1
Jacky
  • 8,619
  • 7
  • 36
  • 40
  • Yes, I've implemented `config.threadsafe!` in my development.rb file. I understand there can be a deadlock between wkhtmltopdf and the asset pipeline when using default webrick because it uses only one thread. My app was previously doing nothing before I did this, so this did indeed help get things a step further. However, it's still hanging while waiting for the PDF file (i guess the browser isn't receiving it from the server?) – robmclarty Sep 24 '12 at 13:42
  • It didn't work for me either until I switched from `thin` back to `webrick`. – mikebridge Jan 28 '16 at 18:28
0

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).

kenorb
  • 155,785
  • 88
  • 678
  • 743
Zorak
  • 109
  • 7
0

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

-2

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
kenorb
  • 155,785
  • 88
  • 678
  • 743
Jigar Bhatt
  • 4,217
  • 2
  • 34
  • 42