wkhtmltopdf command fails
Asked Answered
G

4

14

I am trying to convert a HTML file to PDF using wkhtmltopdf.

For that purpose I have installed wkhtmltopdf on a Debian box and am trying to run this command:

/usr/bin/xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf /path/convert.1303714349.4961.html.tmp /path/convert.1303714349.4961.pdf.tmp

I get the following error:

Loading page (1/2)
Error: Failed loading page file:///path/convert.1303714349.4961.html.tmp (sometimes it will work just to ignore this error with --ignore-load-errors)

The file does exist in /path/convert.1303714349.4961.html.tmp, and the permissions are set correctly. Can it have anything to do with the path being prepended with file://?

Gingili answered 25/4, 2011 at 7:11 Comment(1)
I had this problem because of a javascript code which reloaded my page while it was converted to pdf. I dunno, maybe this will help somebody :-?Blimp
M
40

I believe wkhtmtopdf does not handle local files with non-typical extensions. Try renaming the input file to *.html.

See issue and comments at https://web.archive.org/web/20131124022426/http://code.google.com/p/wkhtmltopdf/issues/detail?id=486.

Monroemonroy answered 2/7, 2011 at 16:15 Comment(0)
H
3

Another useful tip is to replace the spaces in the filename by some char like "_", because of when the filename has spaces the process fails loading page

Hookah answered 19/2, 2014 at 14:44 Comment(0)
L
1

Your trouble is caused by the negotiation of user permission between the filesystem, xvfb-run wkhtmltopdf and headless X11 server.

The User runs this command:

cd /home/user;
touch somehtml.html;
/usr/bin/xvfb-run --server-args="-screen 0, 1024x768x24" 
     wkhtmltopdf somehtml.html preview.pdf;

wkhtmltopdf produces a Warning:

Warning: Failed loading page file:///home/user/somehtml.html (ignored)

The target PDF file doesn't exist, has missing content from somefile.html or is blank.

How to reproduce the problem, an sscce:

Conditions:

64 bit Fedora Linux 17, PC Desktop
Install via a 'sudo yum install wkhtmltopdf'
wkhtmltopdf has version 0.10.0
From a terminal, using xvfb-run

The offending HTML in somehtml.html:

<html>
<head>head tag</head>
  <body>
    <h1>this should be H1<h1>
    Words words words in a paragraph.
  </body>
</html>

Be in a user's directory and run

cd /home/user;
/usr/bin/xvfb-run --server-args="-screen 0, 1024x768x24" wkhtmltopdf /home/user/somehtml.html /home/user/preview.pdf

wkhtmltopdf produces a warning

Warning: Failed loading page file:///home/user/somehtml.html (ignored)

/home/user/preview.pdf is missing or blank.

What is causing the problem:

Your xvfb-run jails wkhtmlpdf to the user and doesn't have permission to read and or write to /home/user/somehtml.pdf or /home/user/preview.pdf

To see if this is your problem, ask xvfb-run what kind of permissions we have?

/usr/bin/xvfb-run --server-args="-screen 0, 1024x768x24" whoami > /tmp/secret.txt; cat /tmp/secret.txt

Workaround:

/tmp typically has looser permissions than /home/user/, so do all your work inside /tmp

cd /tmp;
touch /tmp/somehtml.html;
#put above html in somehtml.html
cat /tmp/somehtml.html
    <html>
    <head>head tag</head>
      <body>
        <h1>this should be H1<h1>
        Words words words in a paragraph.
      </body>
    </html>
/usr/bin/xvfb-run --server-args="-screen 0, 1024x768x24" wkhtmltopdf /tmp/somehtml.html /tmp/preview.pdf

And you should get this stdout:

loaded the Generic plugin 
Loading page (1/2)
Printing pages (2/2)
Done

And preview.pdf

chrome_wkhtmltopdf_and_xvfb-run

Why is not wkhtmltopdf smarter and handling this problem for me?

This is harder than it looks, because wkhtmltopdf has to contend with hostile 3rd party proprietary PDF rendering software (made by adobe?) and they work in concert with browser developers to obfuscate and make access to their engines hard because many large corporations by them money for their top secret source code. wkhtmltopdf is pricking into that world so that open source people can have it without paying suits in tall office buildings cash money. That's why adobe pay money to developers of browsers to make this hard for us. We are diluting their profit mechanism, so they retaliate by throwing stop sticks at us where they can. This is that. The more we spreads this round the world to novices, the harder the 3rd party throws stop sticks, since you are preventing profit in others.

Limestone answered 21/12, 2018 at 15:48 Comment(0)
P
0

I haven't had any problem with "file://" paths on my Windows 7 box.

Did you try the advice in the error message, --igonore-load-errors?

Peddler answered 25/4, 2011 at 17:32 Comment(1)
Where do I set --load-error-handling ignore? In the Rails config initializer?Ora

© 2022 - 2024 — McMap. All rights reserved.