problem implementing wicked pdf on heroku
Asked Answered
T

4

17

I am using this guide for integrating wicked_pdf on heroku. But somehow it doesn't seem to work. I got the logs from heroku and its says this:

Processing PdfController#get_pdf to pdf (for 115.248.175.50 at 2011-02-15 23:54:44) [GET]
  Parameters: {"format"=>"pdf", "action"=>"get_pdf", "id"=>"1", "controller"=>"pdf"}
***************WICKED***************
Rendering pdf/get_pdf

RuntimeError (PDF could not be generated!
/usr/ruby1.8.7/lib/ruby/1.8/open3.rb:73:in `exec': No such file or directory - /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/bin/wkhtmltopdf-amd64       - -  (Errno::ENOENT)
    from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:73:in `popen3'
    from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:59:in `fork'
    from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:59:in `popen3'
    from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:57:in `fork'
    from /usr/ruby1.8.7/lib/ruby/1.8/open3.rb:57:in `popen3'
    from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/wicked_pdf.rb:22:in `pdf_from_string'
    from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/pdf_helper.rb:28:in `make_pdf'
    from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/pdf_helper.rb:39:in `make_and_send_pdf'
    from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/vendor/plugins/wicked_pdf/lib/pdf_helper.rb:13:in `render'
    from /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/app/controllers/schedule_controller.rb:33:in `get_pdf'

Please Help. Thanks in Advance.

EDIT:

I made some changes in my code and got through this error but still stuck here

NoMethodError (undefined method empty? for #<Pathname:0x2b7112392480>)

Tappet answered 16/2, 2011 at 8:7 Comment(0)
M
6

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.

Maggard answered 24/2, 2011 at 9:7 Comment(2)
you can refer ruby-doc.org/core/classes/String.html and ruby-doc.org/stdlib/libdoc/pathname/rdoc/index.htmlMaggard
Thanks, actually in the wicked_pdf config file I was using Rails.root.join('bin', 'wkhtmltopdf-amd64') and this was giving me the PATH object added to_s to it, and it workedTappet
S
42

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://mcmap.net/q/688238/-problem-implementing-wicked-pdf-on-heroku
  4. How to install wkhtmltopdf using Homebrew `https://mcmap.net/q/414543/-why-does-pdfkit-wkhtmltopdf-hang-but-renders-pdf-as-expected-when-rails-app-is-killed

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.

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.

Sedgewick answered 16/5, 2013 at 1:36 Comment(9)
Clear, concise answer. Worked first time for me. Many thanks.Dactylo
As an update, using the following worked first time on Heroku: Rails 4.1.6, wicked_pdf 0.11.0 and wkhtmltopdf-binary 0.9.9.3Humo
How did you get the AMD64 binary? All I see are Linux packages. I extracted the .deb archive and it looks like there are linked dependencies instead of an all inclusive standalone binary.Wanids
@Wanids Thank you for bringing this to my attention. The statics were moved to this location: wkhtmltopdf.org/old-downloads.html I updated the citation to the new url 2/11/2015.Sedgewick
Thanks for that. I just tried using this build pack and it seems to work: github.com/dscout/wkhtmltopdf-buildpackWanids
Thanks @Dex. I added that info to my answer.Sedgewick
a gem packed with a executable seems to work, too: github.com/rposborne/wkhtmltopdf-herokuJanitajanith
Thanks @juanitofatas. I'm glad to see rposborne has updated the wkhtmltopdf-heroku gem to a recent version of the wkhtmltopdf library. I've added the reference to my answer.Sedgewick
@Sedgewick typo in wicked_pdf.rb? "i368" instead of "i386"Ogg
M
6

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.

Maggard answered 24/2, 2011 at 9:7 Comment(2)
you can refer ruby-doc.org/core/classes/String.html and ruby-doc.org/stdlib/libdoc/pathname/rdoc/index.htmlMaggard
Thanks, actually in the wicked_pdf config file I was using Rails.root.join('bin', 'wkhtmltopdf-amd64') and this was giving me the PATH object added to_s to it, and it workedTappet
S
3

If you add a local copy of wkhtmltopdf to your repo you can point to it like this in an initializer.

PDFKit.configure do |config|
config.wkhtmltopdf = "#{RAILS_ROOT}/lib/wkhtmltopdf"
end

remember to chmod +x your wkhtmltopdf copy in your repo before you - git add it

Hope this helps.

Scalawag answered 24/2, 2011 at 0:18 Comment(3)
Sorry, so used to using PDFkit I see wkhtmltopdf and I straight away think PDFkit,Scalawag
Maybe PDFkit is the Answer your actually looking for?Scalawag
No, I need to use wicked_pdf :)Tappet
S
0

Is looking for the wicked_pdf binary in : /app/fa369291-829b-4b61-9efe-b2f0d0a0a42c/home/bin/wkhtmltopdf-amd64

And is not there, put the binary in a folder within your application and then change the file config/initializers/wicked_pdf.rb to include:

:exe_path => "#{Rails.root}/path/to/wkhtmltopdf-amd64"

Another problem you might come to if wicked_pdf create files, is the fact that Heroku filesystem is only read, so unless you find a way to tell wicked_pdf to create the pdfs in the tmp directory you might not be able to use it.

Skinflint answered 16/2, 2011 at 9:10 Comment(2)
Thanks. But I have already done that the binary exists in my RAILS_ROOT/bin/ and in the config/initializers/wicked_pdf.rb I have specified :exe_path => Rails.root.join('bin', 'wkhtmltopdf-amd64').to_s but it still doesn't workTappet
Your answer seems legit. Could it be something with the platform type of the binary? I ran RUBY_PLATFORM from a Heroku console and got => "x86_64-linux" as the response.Sedgewick

© 2022 - 2024 — McMap. All rights reserved.