problem implementing wicked pdf on heroku

How to get WickedPdf gem to work on Heroku

(see citation below for urls)

  1. Create a folder named bin in the root of your Rails app.
  2. Download and extract version 0.9.9 of the wkhtmltopdf binaries to the bin folder. You will need the version for your development system and the AMD64 version for Heroku. Do not add to your git repo yet.
  3. Set the execute permissions for each binary in the bin folder e.g. chmod +x bin/wkhtmltopdf-amd64
  4. Now git add bin
  5. Add wicked_pdf to your Gemfile and bundle install. No other wkhtmltopdf gems are needed.
  6. Create a file in the config/initializers folder called wicked_pdf.rb with the source code below.
  7. git commit -am 'added wkhtmltopdf binaries and wicked_pdf gem'
  8. git push heroku

Your Rails project is now configured for Heroku. From here, add your program logic for PDF files.

** config/initializers/wicked_pdf.rb **

if Rails.env.production?
  wkhtmltopdf_path = "#{Rails.root}/bin/wkhtmltopdf-amd64"
else
  # Linux (check your processor for Intel x86 or AMD x64)
  # wkhtmltopdf_path = "#{Rails.root}/bin/wkhtmltopdf-amd64"
  # wkhtmltopdf_path = "#{Rails.root}/bin/wkhtmltopdf-i386"
  # OS X
  wkhtmltopdf_path = "#{Rails.root}/bin/wkhtmltopdf-0.9.9-OS-X.i386"            
  # Windows
  # wkhtmltopdf_path = 'C:\Program Files/wkhtmltopdf/wkhtmltopdf.exe'
end

WickedPdf.config = { exe_path: wkhtmltopdf_path, wkhtmltopdf: wkhtmltopdf_path }

How I came to this conclusion:

For starters, there are SEVERAL misleading articles on the Web regarding wkhtmltopdf, the wicked_pdf gem, and Heroku. There are also issues with recent versions of wkhtmltopdf (10.0+) that cause execution to hang.

Lastly, at some point there must have been a change in the config variable names for WickedPdf because several articles reference :exe_path while other articles reference :wkhtmltopdf. Even WickedPdf's GitHub documentation alternates between each variable.

My solution you do not need to compile wkhtmltopdf from source. You do not need to install wkhtmltopdf from Homebrew. I provided the solution to this if you really want to brew install wkhtmltopdf. You also do not need to add any of the 'wkhtmltopdf' helper gems to your project. I tried wkhtmltopdf, wkhtmltopdf-heroku, wkhtmltopdf-engineyard, wkhtmltopdf-binary gems and GitHub repos. Worked great locally. No luck at Heroku.

From a Rails console on Heroku: I investigated what CPU was running Heroku's servers by running RUBY_PLATFORM which yielded "x86_64-linux". I also peeked inside the bin folder on Heroku by issuing the Dir.entries('bin') to see what files actually exist there. I noticed some of the wkhtmltopdf gems I was testing placed their executables in this folder. A clue! I ran WickedPdf.new.pdf_from_string('Hello') to see the output errors and test various WickedPdf configurations.

Then, in @barlow's answer to configuring PDFKit, there is a subnote that gave the ultimate key. You must give Unix execute permissions to wkhtmltopdf the binary before committing to Git. Bingo!

Cite:

  1. Official wkhtmltopdf legacy static binaries http://wkhtmltopdf.org/old-downloads.html
  2. wicked_pdf https://github.com/mileszs/wicked_pdf
  3. Barlow's PDFKit.config https://stackoverflow.com/a/5098984/307308
  4. How to install wkhtmltopdf using Homebrew `https://stackoverflow.com/a/14043085/307308

At the time of this writing:

  • rails (3.2.13)
  • wicked_pdf (0.9.6)
  • wkhtmltopdf (0.9.9)

Amended 2/12/2015

dscout has developed a gem that encompasses the concepts of my answer. I recommend using the gem if your Heroku instance supports buildpacks.

  • https://github.com/dscout/wkhtmltopdf-buildpack
  • https://devcenter.heroku.com/articles/buildpacks

Amended 3/27/2015

Another gem that works with Heroku (and Linux AMD64 OSes) wkhtmltopdf-heroku It auto-detects if pdfkit, wicked_pdf and wisepdf gems are installed.


After looking at your Edit part which says "undefined method empty? for #<Pathname:0x2b...>

This means you are somewhere using empty? function on a path Object whereas empty? is a function of a string and not path.

try to find out where you have used a path Object and use to_s on that object.

Try it out.