|
Usage
The following lists some usages for wkhtmltopdf, please post more examples in the comments To convert a remote HTML file to PDF: wkhtmltopdf http://www.google.com google.pdf To convert a local HTML file to PDF: wkhtmltopdf my.html my.pdf You can also convert to PS files if you like: wkhtmltopdf my.html my.ps The eler2.pdf sample file wkhtmltopdf http://geekz.co.uk/lovesraymond/archive/eler-highlights-2008 eler2.pdf -H --outline |
► Sign in to add a comment
Note that on Windows, you can't use absolute path names with drives for HTML files at the moment:
will fail. You need to rather use file:/// URLs:
I got this to work under Windows perfectly, however, I'm unsure of what to do in OS X -- I downloaded the file and tried running it in a terminal window (cd to directory, then ./wkhtmltopdf-0.8.3.1.i386) and got a permission denied error; then, when running with sudo, got an error saying 'command not found'. Is the i386 file an archive? Or is it simply an executable binary file? And if so, shouldn't I just be able to run it explicitly from the terminal?
I use "wkhtmltopdf-0.8.3.1-OS-X.i386" with MacOS X. After I downloaded, I changed the permissions with chmod +x
any sample coding how to I run this in PHP exec() in Linux enviroment?
@kentch84, look at the code for this Drupal module:
http://drupal.org/project/print
Did not know this but if reading a local file, it's important, IMPORTANT that it have the .html extension.
This will NOT work
This will
Hi there,
What if the images need to be taken from a given directory on the filesystem instead of a website? Like, in the html file there's a <img src="/images/someimage.jpeg">, and the "images" directory is somewhere on the filesystem
Rosko Relative images work fine, I have produced a pdf with:
<img src="images/PHONE.JPG"
You need to run wkhtmltopdf in the directory above images.
How do I use php ?
Centos Apache 2.1 Php 5.2
is there any opportunity to generate PDF files from some directory?
@Rosko <img src="file:///var/path/to/file/image.png"> worked for us
@cihanozturk53
For PHP (Apache MAC OS X) we've used this for generating a pdf from a script we normally display:
ob_start();
passthru('/path/wkhtmltopdf http://example.com/pdf/get_html_for_pdf.php' -');
file_put_contents('/path/to/file.pdf', ob_get_clean());
I'm attempting to run wkhtmltopdf-0.9.3-OS-X.i368 from the command line in OSX 10.6.2. I keep getting "command not found." I've ran chmod +x to change the permissions. Has anyone ran into this before?
Hi, everyone.
I'm successfully generating PDF's here, but somehow I can't use copy and paste on the texts in it. Is there any parameter or configuration that I can setup to change that?
nsbingham, I downloaded wkhtmltopdf-0.9.3-OS-X.i368, copied it to /usr/local/bin, and then ran chmod 777 on it (yes, overkill I know), and it then worked from the command line. I have OSX 1.6.3
Did you copy it to the /usr/local/bin folder?
more doc here: http://madalgo.au.dk/~jakobt/wkhtmltopdf-0.9.0-doc.html
Is there a parameter to produce postscript instead of PDF, writing this to stdout?
If you are having problems with Chinese/Japanese characters not displaying in documents generated using wkhtmltopdf, make sure you have proper fonts installed in your system! Otherwise, the required fonts will not be embedded to the PDFs.
On Debian Lenny the following aptitude line does the magic:
$ sudo aptitude install ttf-arphic-bkai00mp ttf-arphic-bsmi00lp ttf-arphic-gbsn00lp ttf-arphic-gbsn00lp
This seems to work for at least Chinese and Japanese documents.
OSX. Permission change not enough. Following sirmarkv's note, I did both (a) did chmod +x (b) moved to /usr/var/bin/
It then ran OK from the command line. Hopefully the manual (at http://madalgo.au.dk/~jakobt/wkhtmltopdf-0.9.9-doc.html) could mention this at some point when being updated (or give a more correct OS X installation instruction).
I had the same issue on OSX as well. Be nice if it was part of MacPorts? so you could just do sudo port install wkhtmltopdf!
I have convert html to pdf in windows 7 system But unable to see final file. I type following line in commandline. wkhtmltopdf file:///f:/download/14264-h/14264-h.htm xxy.pdf But I was not found xxy.pdf anywhere. Please help...
Hi ....I am trying to run wkhtmltopdf from my java program.Am currently using wkhtmltox-0.10.0_beta4-installer.exe for Windows.I use the following command to execute the tool:
This works fine and my pdfs are placed in the target path but the images are missing . Images are provided as a relative link SRC="/ebir/images/dnb_print.gif" . The pdf shows empty white boxes in place of the image
This issue occurs only when i call from java program.When i run from cmd prompt the pdf is dispalyed with images. Please let me know how to run from java .
does anyone have an example usage for passing page? to the supplied header.html? i can't make it work as noted
Where are the docs? http://madalgo.au.dk/~jakobt/wkhtmltopdf-0.9.9-doc.html says: Not Found
The requested URL /~jakobt/wkhtmltopdf-0.9.9-doc.html was not found on this server.
When running wkhtmltopdf in PHP using exec() on a Mac OSX environment the call silently fails, as I'm getting a shell return code of 5 and an empty output.
Right after installing it worked perfectly for a few minutes, but then this error started. I've upped PHP's memory_limit up to 1024 MB and checked user/file permissions on both the wkhtmltopdf program, the HTML source file, and the destination folder, and everything is in order.
If the exact command that tried to be run in PHP is run directly from the command line, it works just fine.
Any one has any idea what this code mean?
hello, i have a question. I need convert html file to pdf with "Table of Contents". What structure should have my file(how should i show that "current title" should be in table of contents )? Does this lib can do this?
http://www.oschina.net/p/wkhtmltopdf
is there an option for STDOUT?? Thanks
On my mac I followed the directions to (a) chmod +x (b)copy it to /usr/local/bin but still had trouble.
I figured it out and feel stupid, but I'm posting it to save someone as clueless as I.
I was trying to invoke it using "wkhtmltoimage" but it needed the full download file name of "wkhtmltoimage-0.10.0_beta4-OS-X.i386".
To get the styling right, we found we needed to remove the BASE tag from our HTML. Then we just copied our styles directory into the directory with wkhtmltopdf, and then the regular style sheet links in the html were picked up automatically and correctly. Our HTML has:
@ablopez824: Find a solution? I'm seeing the exact same results after taking the same steps. Executing the same command via bash works fine. Currently production is Windows (which I haven't tried from an exec call yet), but it'd be nice to have it working locally in OSX to flesh out the rest of the feature.
On Mac OS-X 10.6.5 with Drupal 7rc4. I have downloaded wkhtmltoimage-0.10.0_beta4-OS-X.i386, placed it in modules/print/lib, chmod to 755. Drupal does not recognize it as a PHP tool. What is next?
I've found that if you substitute "-" for one of the arguments, it uses stdin/stdout for that argument. So, if you call "wkhtmltopdf - -" it will read in the HTML from stdin and write the pdf to stdout.
Hi all!
I have a quiet large document to be converted with references between the HTML files, e. g.:
...
<a name="_glg17.16"></a>(17.16)
...
<a href="#_glg17.16">Equation 17.16</a>
...
After converting this into PDF the link in the document is an absolute poniter to the original html-file like this: file:///C:/some_directory/source_file.html#_glg17.16
Is there any possibility to tell wkhtmltopdf that this link points to somewhere in the same PDF document? The same is for links to other HTML-file which are part of the same PDF document.
I used Version 0.9.9
Thanks in advance.
Quick tip from experience: Was working with wkhtmltopdf just fine locally and on a remote staging, but when I set up a third staging, this time on a fictitious domain with only /etc/hosts definition, my generated PDF was unformatted. Turns out it could not find my attached css or other assets. Adding its false domain to /etc/hosts on that staging server (the one that was running the site and wkhtmltopdf) solved it.
Hi, I need a little help.. I want to convert a simple html file to pdf.
html: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <title>content</title> <style type="text/css" media="screen"> #footer{ margin-top: 68px; margin-left: 60px; font-family:"Helvetica",Times,serif; font-weight: bolder; font-size: 40px; height: 60px; } #infooter{ margin-left: 60px; color: black; margin:0pt auto; } </style> </head> <body>
<div id="footer"> <div id="infooter">Some Text</div> </div> </body> </html>but i get only a file with 0KB! i can't open it. wkhtmltopdf give me this output (from cmd): c:\html2pdf>wkhtmltopdf.exe content.html content.pdf Loading pages (1/6) Counting pages (2/6) Resolving Links (4/6) Loading headers and footers (5/6) Printing pages (6/6) [===============================================================] Page 1 of 1 c:\html2pdf>
There is no "done" and i dont know why.
Thank you for helping me...
M.M.
I know this is ugly. No really.
My website had all absolute links for CSS and images e.g. <img src="/societycard/images/thing.png" />
I mucked about with process working directories for ages and then gave up and did something naughty:
(pwd:/) ln -s /var/www/xsearch/societycard
And hey presto everything worked. Would welcome another solution!
BTW: I am using XSL/XML systems and all includes are absolute to ensure that the transform works on server and client. That is why image links are as well, to follow suit.
Is -toc option removed from 0.10.0 version? It is in the extended help but not in the simple help and of course the program does not recognize it if passed as an argument.
Hi, I try get dimensions of body in html file, this file is composed by div as a header and a table maked whith divs y css (display:table, display:table-row), etc. Well, if I launch a script on document.load event for get height and width dimension (offsetHeight/offsetWidth ) in Firefox
Opera Chromiumbut in wkhtml (0.9.6) I get
I don't understand which mean this numbers ?? The PDF cocument have a only one sheet, Letter in landscape mode. Anybody tell me which mean this numbers ? Thanks.Just wondering, is there a way to have access to the variables like 'title' in the cover page html?
We are doing some dynamic generation of the pdf's and I would like to be able to add a title to the cover page dynamically.
Thanks,
Casey
Feature Request: Page Width & Page Height tweaks
It'd be nice to note in the -H usage that --page-width and --page-height:
And if you wanted to go Really Crazy you could add "point" based variants on --page-width and --page-height since that's how PDF pages are defined. Us PDF-native-speakers would appreciate it. The rest of us silly backwards folks from the states might appreciate being able to specify dimensions in inches while you're at it.
How about accepting some subset of html's unit definition postfix string thingamajiggers. "pt", "mm", "in". Leave "mm" the default for backward compatibility. You /could/ through in some of the others while you're at it, but that's really just gold plating. Points and Inches would actually get used.
And if you want to go gonzo-nutso, you could come up with some way to define individual page sizes rather than a fixed size across all pages. That is of admittedly limited utility, but I should would appreciate it.
Feature Request: custom user agent
Lots of pages out there check for "Safari" or "Chrome" for their webkit-specific layout tweaks, but have never heard of wkhtmltopdf. It'd be nice to trick those pages into using their webkit layout for wkhtmltopdf
In other words: My buttons done shrunk!
None the less: GREAT tool. Love it.
The usage for wkhtmltoimage (sorry I can't edit the wiki)
Name:
Synopsis:
Description:General Options:
Specifying A Proxy:
Contact:
Hi! I've got problems on windows with asp.net:
p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.FileName = Server.MapPath("~/bin/wkhtmltopdf.exe"); p.StartInfo.Arguments = "file:///d:/tmp/test.html file:///d:/tmp/out.pdf"; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.Start(); p.WaitForExit();I've set full access do d:\tmp for Process start user, but I'm still getting
Please help me.
Didn't want this to get lost in the bug's section because I need this resolved ASAP for an automated system I'm working on.
I can't get the images in this HTML report to work: http://beforethecode.net/report.html
It uses a <BASE HREF> tag to locate the images/stylesheets URL (stylesheets are loading fine) but no images show up. Even after removing the <BASE HREF> and hard coding a local path to the IMG tag's they still don't work within this file... help!
Try it yourself: wkhtmltopdf.exe --disable-smart-shrinking --page-size Letter http://beforethecode.net/report.html report.pdf
How do I turn off the bookmarks bar?
I have a question, how to generate a pdf via a string (html markup) ?
@izayn i am getting the same error and found the problem, please change your path "d:\tmp\test.html d:\tmp\out.pdf"
and try to comment the 'p.WaitForExit?()
Hope it helps
Khushal
@gitaw: you need to use $pdf->set_html instead $pdf->set_url. It then creates a temporary hmtl file by using the string input you provided in the local file system, then reads it to create the PDF output and deletes the temporary file.
@xgho: it works perfect for me.
Some more wkhtmltopdf command line examples:
For converting stories or documents to be sent to handheld devices: wkhtmltopdf -O Landscape -B 0 -L 0 -T 0 -R 0 mystory.htm mypdf.pdf Next step would be to convert it with Imagemagick to jpg files and download to any handheld device that can display jpegs (especially if it doesn't have an ebook reader). More details here: http://www.distasis.com/recipes/portp.htm
To convert documents to e-books: wkhtmltopdf -g --disable-smart-shrinking --no-background --header-center title? --header-font-size 24 --footer-center page? mydoc.htm mypdf.pdf The added flags cause the output to be printed in greyscale, disable shrinking of the output, turn off printing of the background (mine's usually set to grey instead of white be default) and add a centered title in 24 point font size to the header and a footer with the current page number centered.
Looks like the brackets aren't showing on my previous comment for converting documents to e-books when specifying title and page information. If you need exact syntax for the options that isn't mangled by the wiki, you can find it here: http://www.distasis.com/cpp/books.htm#run
Is there a way to set user password for the generated pdf i.e it must prompt for password whenever user tries to open the document similar to set_option :user_password in htmldoc
how to convert not oly one page? if i need all pages on site?
Hi, I have an html file. The conversion works fine if the images and CSS are on the local file system. But If I delete CSS and images from the local file system and try to run it it doesn't download the images and CSS from the hrefs all by itself.
Help much appreciated.
Thanks
Hi , how can include a footer and/or header with the php class? Regards
I used wkhtmltopdf.exe installer on Windows and I am generating PDF files using JAVA Process Object:
Process process = Runtime.getRuntime().exec("C:\\Program Files\\wkhtmltopdf\\wkhtmltopdf --margin-left 0mm --margin-right 0mm --margin-top 0mm --margin-bottom 0mm --page-size Letter --page-width 216mm --page-height 279mm D:\\env\\tmp\\a.html D:\\env\\tmp\\a.pdf"); InputStream is = process.getInputStream(); byte[] byteArray = IOUtils.toByteArray(is); System.out.println("WKExporter BYTE ARRAY " + byteArray.length); //=> 0As you can see I get byteArray.length = 0.
Is it possible to redirect the output to the standard output stream of the process rather than writing it on the disk?
Thanks a lot
hi, i am using wkhtmltopdf 0.9.9 . Fonts appear to be bolder in the pdf when i am using transparent png images. Is there a fix for this?
What does error codes 6 and 11 mean?
Is there any way to increase quality of PDF? I lose coloring and shadows became nasty.
For those who are invoking wkhtmltopdf from another program (java Process object, C#, perl etc):
For some odd reason, the output from wkhtmltopdf goes to stderr and NOT stdout.
So, for example in java, you would have to do:
//ProcessBuilder is the recommended way of creating processes since Java 1.5 //Runtime.getRuntime.exec() is deprecated. Do not use. ProcessBuilder pb = new ProcessBuilder("wkhtmltopdf.exe", htmlFilePath, pdfFilePath); Process process = pb.start(); BufferedReader errStreamReader = new BufferedReader(new InputStreamReader(process.getErrorStream()); //not "process.getInputStream()" line = reader.readLine(); while(line != null) { System.out.println(line); line = reader.readLine(); }Also, you MUST read from the stream (even if you do nothing with it) because otherwise the stream buffer will fill and the process will hang and never return.
To futureproof your code, just in case the devs of wkhtmltopdf decide to write to stdout, you can redirect stderr of the child process to stdout and read only one stream like this:
ProcessBuilder pb = new ProcessBuilder("wkhtmltopdf.exe", htmlFilePath, pdfFilePath); pb.redirectErrorStream(true); Process process = pb.start(); BufferedReader inStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream());Hope this helps
Update on comment above:
I raised this in the issues section and was promptly replied that this was by design:
Issue 825 : wkhtmltopdf is writing to STDERR instead of STDOUT
Comment 2 by project member antial...@gmail.com, Today (10 hours ago) This is by design, since we support outputting the actual pdf to stdout. E.g. wkhtmltopdf google.com - > abe.pdf
To render PDFs containing chinese characters on a CentOS 5.x server, you additionally need to install chinese fonts.
yum install fonts-chinese.noarch
Also the HTML code should contain the content charset definition in the heads meta-tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
Hi, thanks for this awesome application. This is usage for Autoplay Media Studio based LUA 5.1:
Regards, KintaRo?
How does smart width in wkhtmltoimage work? it seems like it always defaults to 1024, and will not honor the body width of my page, which can be much slimmer.
The packaging for Mac OS X is entirely undocumented and as supplied it doesn't work at all. It's wrapped up as an app but not usable as one: double clicking it does nothing except occasionally doing the interminable bounce of death. The critical thing you actually need is lurking inside the bundle in /path/to/wkhtmltopdf.app/Contents/MacOS/wkhtmltopdf. I got it to work by simply using that binary instead of anything to do with the app. I symlinked it in case there was something in the bundle that it needed:
And now it works.
This took me an entire day to find out: it should be the first thing in the docs, since nothing will work without this information. As others have mentioned, it would be much better if this was made available through MacPorts?/Homebrew/Fink, or built using a normal configure/make process.
I decided to have a go at building it myself (so I could have a 64-bit 10.7 build), which worked, and I documented how I did it on my blog.
QPixmap: Cannot create a QPixmap when no GUI is being used Segmentation fault (core dumped)
Got the above when running in command line mode (the site I was pointing at has SVG-rendered charts) - Ubuntu 12.04 desktop
command: wkhtmltopdf http://www.highcharts.com highcharts.pdf
Any ideas?