wkhtmltopdf command fails
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://
?
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.
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
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
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.
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
?
精彩评论