Table of Contents
Generate PDF from HTML page with CSS support
For the generation of the PDF from any HTML page the Qt's webkit can be used. The project home:
http://code.google.com/p/wkhtmltopdf/
It works like a browser, which send the page on virtual pdf printer. The result is very close to the source page.
Requirements
- QT 4.4 or above (Runtime dep)
- CMake 2.6 or above (Compiletime dep)
- Subvertion (For SVN builds)
Installation
svn checkout http://wkhtmltopdf.googlecode.com/svn/trunk/ wkhtmltopdf cd wkhtmltopdf cmake -D CMAKE_INSTALL_PREFIX=/usr . make make install
Usage
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
Usage as server-side console application in the scripts
- Firstly you should install the “Xvfb” server. It's required because the wkhtmltopdf application will require an X11 server. Please note that you have to add this service to the auto-loading, but for the tests you can run:
/usr/bin/Xvfb :0 -screen 0 1024x768x24 -ac
- Run the following command to test the PDF generation:
DISPLAY=":0.0" wkhtmltopdf http://www.arscommunity.com community.pdf
- Use exec in your php script:
<?php function func_generate_pdf($in_file, $out_file) { $exec = sprintf('DISPLAY="%s" %s %s %s', ':0.0', 'wkhtmltopdf', $in_file, $out_file); exec($exec, $result); } ?>