5

I'm trying to use PDFKit.

It looks fairly straightforward so I'm trying to follow Ryan's Rails cast.

So I added the gem to my GemFile and updated my application.rb with:

config.middleware.use "PDFKit::Middleware"

After that I installed wkhtmltopdf on linux with:

apt-get install wkhtmltopdf

And I make sure it works:

[nicolas@Minto]%wkhtmltopdf www.google.com gogole.pdf
Loading page (1/2)
Printing pages (2/2)                                               
Done

Everything looks good until I try to add the .pdf on one of my pages...

The website stays in a loading state forever. When I stop the server with a Ctrl - C in the console, I get the following error:

RuntimeError (command failed: "/usr/bin/wkhtmltopdf" "--page-size" "Letter" "--margin-top" "0.75in" "--margin-right" "0.75in" "--margin-bottom" "0.75in" "--margin-left" "0.75in" "--encoding" "UTF-8" "--print-media-type" "--quiet" "-" "-"):
  pdfkit (0.5.2) lib/pdfkit/pdfkit.rb:71:in `to_pdf'
  pdfkit (0.5.2) lib/pdfkit/middleware.rb:21:in `call'

What am I doing wrong?

wythagoras
  • 316
  • 8
  • 16
Nicolas Guillaume
  • 8,160
  • 6
  • 35
  • 44

4 Answers4

0

I also followed Ryan's Rails cast.

PDFkit is dependent on wkhtmltopdf.rb. Any version > 0.9.9 has been causing the PDF creation process to hang just prior to completion, requiring a Ctrl - C. I use OS X 10.6.8, not Linux. However try:

wkhtmltopdf --version

If it is > 0.9.9, then rollback to 0.9.9. Solved my problem. Took me hours to figure out.

Leo Ajc
  • 133
  • 1
  • 3
0

We have a related problem here: PDF Generation hangs using PDFKit and wkhtmotopdf

Quoting the solution from Beerlington:

Try removing any javascript include tags from your HTML and see if that at least lets the PDF render. If that works, then you at least have a starting point.

I tried that and that worked for me.

I think it has to do with assets like javascript and stylesheets with relative path.

Community
  • 1
  • 1
Lucca Mordente
  • 1,131
  • 7
  • 9
0

I recently played with PDFKit and got it to work in my rails application and faced a similar problem.
The three steps you need are :

  1. You will need to put gem "pdfkit" in your GemFile.
  2. Put config.middleware.use PDFKit::Middleware in application.rb file which you already have.
  3. Try putting the following code in pdfkit.rb file under 'config/initializers' folder.

    PDFKit.configure do |config|

    config.wkhtmltopdf = 'C:\software\utilities\wkhtmltopdf\wkhtmltopdf.exe' #Path to your wkhtmltppdf installation directory

    config.root_url = "http://localhost" # Use only if your external hostname is unavailable on the server.

    end

Prashanth
  • 1,388
  • 2
  • 11
  • 26
0

Ok so,

Took a few hours of googling but I finally found the solution in an other stackoverflow question: pdfkit not rendering correctly in rails 3.1

Thank you for helping.

Community
  • 1
  • 1
Nicolas Guillaume
  • 8,160
  • 6
  • 35
  • 44