Rails 3 - wkhtmltopdf command failed when using PDFKit to create PDF documents
Asked Answered
S

6

5

I have setup PDFKit in my Rails 3 application, using RVM (had to manually copy the wkhtmltopdf binary). When I try to render the PDF version of a page, I get this error:

RuntimeError in AgenciesController#show

command failed: ["lib/wkhtmltopdf/wkhtmltopdf", "--disable-smart-shrinking", "--page-size", "Letter", "--margin-top", "0.75in", "--margin-right", "0.75in", "--margin-bottom", "0.75in", "--margin-left", "0.75in", "--encoding", "UTF-8", "--quiet", "\n.......\n", "-"]

The following is in my applicaition.rb:

    config.middleware.use "PDFKit::Middleware"
    PDFKit.configure do |config|
    config.wkhtmltopdf = 'lib/wkhtmltopdf/wkhtmltopdf' 

    end

An ideas why this is happening? how can I fix it?

In the console, I noticed this message:

 (sometimes it will work just to ignore this error with --ignore-load-errors)

Where do I invoke that switch? wkhtmltopdf seems to be working fine on the command line, I can do something like "./wkhtmltopdf http://www.google.com google.pdf" and generate a PDF.

Thanks for your help,

Peter

Speleology answered 12/8, 2010 at 13:38 Comment(3)
I tried config.wkhtmltopdf with absolute path, restarted the server, but got the same error.Speleology
Did you manage to get the fix for it ? I have stored executables on my_app_path/lib/wkhtmltopdf Like you said it works from terminal. But doesn't work when I run Webrick server on production mode. Any help would be appreciated. ThanksValise
No, I gave up on this approach as it wasn't working for what I was trying to do, and went to Prawn instead (github.com/prawnpdf/prawn).Speleology
W
5

Judging from the source code, you can set options on pdfkit. I think the following will work:

PDFKit.configure do |config|
  config.default_options[:ignore_load_errors] = true
end

(I didn't test it though)

Wilcox answered 12/8, 2010 at 13:59 Comment(2)
That worked so that the app doesn't crash when I load a page with .pdf. The PDF page, however, is getting rendered blank. What could be causing that?Speleology
I'm guessing that wkhtmltopdf fails. Not sure what to do about that though.Wilcox
L
4

I searched it on google, and found the answer on a blog.

Solution is here:

Installing dependencies

$sudo aptitude install openssl build-essential xorg libssl-dev

For 64bits OS Run one by one following commands:

$ sudo wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
$ sudo tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
$ sudo mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf 
$ sudo chmod +x /usr/local/bin/wkhtmltopdf

Finally, go to your rails app/config/initializer folder and create new file pdfkit.rb and paste the following code into it:

PDFKit.configure do |config|
   config.wkhtmltopdf = '/usr/local/bin/wkhtmltopdf' if Rails.env.production?
end

That's it. Now your pdf file will be downloaded. Also visit for further information: http://www.stormconsultancy.co.uk/blog/development/generating-pdfs-in-rails-with-pdfkit-and-deploying-to-a-server/

Thanks.

Lathing answered 11/3, 2013 at 19:49 Comment(0)
C
2

you may want to check out this plugin wicked pdf

Cycloparaffin answered 11/10, 2011 at 17:16 Comment(0)
S
1

I used this hack.

config.wkhtmltopdf = `which wkhtmltopdf`.gsub(/\n/, '')

the which command returns a new line at the end.

Synchronic answered 21/9, 2010 at 6:5 Comment(0)
R
1

I was facing the similar issue on my ubuntu os. But then re-installed wkhtmltopdf using instruction in https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF.

Downloaded binary file from http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltopdf-0.9.9-static-i386.tar.bz2&can=2&q= and put it in the /usr/local/bin/ directory which solved an issue for me.

Reformed answered 2/1, 2013 at 12:12 Comment(0)
A
0

Since you already have wkhtmltopdf installed and it appears to be working, maybe give wicked_pdf a shot. It is working great for me in my Ruby 1.9 Rails 3 app. It is just as easy, but gives you a little more control over what can get rendered as a pdf by having you explicitly use render :pdf => 'my_template' when you want a pdf. Of course, you can place that inside a responds_to block if you wish.

ps Incase I wasn't clear, wicked_pdf also uses wkhtmltopdf.

Adventist answered 22/4, 2011 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.