13

Here's an odd little problem that's led me to post my first question on SO. I am using wkhtmltopdf to convert an HTML document to a PDF as part of a Rails app. To do so, I am rendering the Rails web page to a static HTML file in a temp directory, copying a static header, footer and images to the same temp directory, then executing wkhtmltopdf using "system".

This works perfectly in Development and Test environments. In my Staging env, it does not. I suspected permissions at first, but the first couple of parts of that process (creating the HTML static files and copying them to the directory) are working. I can run wkhtmltopdf from the command line in that temp directory and get the expected outcome. Finally, I ran wkhtmltopdf via both "system" and backticks through the Rails console in staging environment, and here's 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) => "" 

Notice that last bit. I'm pointing to local files, but it's looking for them via http. OK, I think, maybe I need to be explicit and feed it the file:// protocol so 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 

Notice that this one fails with a lowercase "l" on Library. What the heck? (And no, it doesn't get any better with the recommendation to ignore the error with that switch.)

Any ideas? Is there a Rails or Ruby setting that would cause system commands to get rewritten? Is there an option I can add to wkhtmltopdf to make sure it loads from local file? I'm quite baffled. Thanks!

lascarides
  • 335
  • 3
  • 9

3 Answers3

5

I have had success when using the absolute file path (notice the extra slash after the 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

This is the same on windows

Unix path

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

Windows path

file:///C:/absolute/path/to/file
Isioma Nnodum
  • 1,318
  • 13
  • 13
1

In last 0.11 whicked-pdf i found one bug
Example C:\Ruby193\lib\ruby\gems\1.9.1\gems\wicked_pdf-0.11.0\lib>wicked_pdf.rb Line 198 I change from:
options[hf][:html][:url] = "file://#{tf.path}" to options[hf][:html][:url] = "file:///#{tf.path}" - (change // to ///)
After change whicked-pdf again worked.

  • In my error I'm seeing four (4) forward slashes, like `file:////tmp/wicked_header_pdf20150807-1155-tqagmw.html`. Is that a problem, do you think? – Joshua Pinter Aug 07 '15 at 17:03
  • I'm gonna guess no because using `curl` with 4 slashes works the same as 3 slashes. In fact, you can use an indefinite amount of slashes and it seems to work just fine. Eg. This works: `curl file:///////////////tmp/wicked_header_pdf20150807-1155-tqagmw.html` – Joshua Pinter Aug 07 '15 at 17:07
0

Take a look at the wicked_pdf gem. You can add a PDF mime type and then whatever page you want pdf'd, just tack on a .pdf to the URL.

I am using this in prod and it works quite well. No need to call wkhtmltopdf directly.

cpuguy83
  • 5,794
  • 4
  • 18
  • 24
  • Thanks, that's a good call. I'd looked at that gem earlier and thought it didn't meet my needs, but I think my use case changed during the course of the project! – lascarides Jan 22 '13 at 00:09
  • 1
    Would still be curious to hear from anyone who knows what that system() oddness was, though. – lascarides Jan 22 '13 at 00:10