Wkhtmltopdf trying to download from http, not file

There was a small issue here that made me post my first SO question. I use wkhtmltopdf to convert an HTML document to PDF as part of a Rails application. To do this, I transfer the Rails webpage to a static HTML file in the temp directory, copying the static header, footer and images to the same temp directory and then executing wkhtmltopdf using "system".

This works great in development and testing environments. In my Staging env this is not the case. At first I suspected permissions, but the first two parts of this process work (creating static HTML files and copying them to a directory). I can run wkhtmltopdf from the command line in this temporary directory and get the expected result. Finally, I ran wkhtmltopdf through the "system" and backticks through the Rails console in the middleware, and here is what I get as output:

> `wkhtmltopdf --footer-html tmp/invoices/footer.html --header-html tmp/invoices/header.html -s Letter -L 0in -R 0in -T 0.5in -B 1in tmp/invoices/test.html tmp/invoices/this.pdf` Loading pages (1/6) QPainter::begin(): Returned false ] 10% Error: Unable to write to destination Error: Failed loading page http://tmp/invoices/test.html (sometimes it will work just to ignore this error with --load-error-handling ignore) => "" 

Note the last bit. I point to local files, but they look for them through http. Well, I think, maybe I need to be explicit and pass the file: // protocol so that it doesn't look for http. So I try this:

 > system("wkhtmltopdf --footer-html file://Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/footer.html --header-html file://Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/header.html -s Letter -L 0in -R 0in -T 0.5in -B 1in file://Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/test.html file://Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/this.pdf") Loading pages (1/6) Error: Failed loading page file://library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/test.html (sometimes it will work just to ignore this error with --load-error-handling ignore) => false 

Note that this failure occurs with the lowercase "l" in the Library. What the heck? (And no, it's best not to ignore the error with this switch.)

Any ideas? Is there a Rails or Ruby parameter that will cause system commands to be overwritten? Is there an option that I can add to wkhtmltopdf to make sure it is loaded from a local file? I'm pretty puzzled. Thanks!

+6
source share
3 answers

Take a look at the pearl wicked_pdf. You can add a PDF type of PDF file, and then any page that you want pdf'd on, just link the .pdf file to the URL.

I use this in prod and it works quite well. No need to access wkhtmltopdf directly.

0
source

I had success using the absolute path to the file (note the extra slash after file:// )

 wkhtmltopdf --footer-html file:///Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/footer.html --header-html file:///Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/header.html -s Letter -L 0in -R 0in -T 0.5in -B 1in file:///Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/test.html file:///Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/this.pdf 

It is the same in windows

Unix Path

 file:///absolute/path/to/file 

Windows path

 file:///C:/absolute/path/to/file 
+1
source

In the last 0.11 whicked-pdf, I found one error
example C: \ Ruby193 \ Lib \ ruby ​​\ gems \ 1.9.1 \ gems \ wicked_pdf-0.11.0 \ Lib> wicked_pdf.rb Line 198 I am changing from:
options [hf] [: html] [: url] = "file: // # {tf.path}" in the option [hf] [: html] [: url] = "file: /// # {tf.path }" - (change to///)
After the whicked-pdf file has changed again,

+1
source

All Articles