Web Design / Development
Generating PDFs and PNGs on Ubuntu
05 December 2012
Ever needed to generate nicely formatted PDFs and/or PNGs on-the-fly? Check out wkhtmltopdf for PDF conversion using Webkit printing to PDF. From there you can conver the PDF to a PNG using pdftoppm.
Here's a very condensed run-down of the procedure on Ubuntu;
- install GCC C++ compiler if not already installed (sudo apt-get install build-essential). See http://www.shibuvarkala.com/2009/03/how-to-install-gcc-cc-compiler-in.html
- install XLL libraries (sudo apt-get install libx11-dev, sudo apt-get build-dep qt4-qmake).
- install wkhtmltopdf on server See http://blog.tcs.de/install-wkhtmltopdf-on-ubuntu-server/
- install xvfb; sudo apt-get install xvfb
- You can now generate nice HTML/CSS to PDF conversions using this command: xvfb-run --server-args="-screen 0, 1280x768x24" wkhtmltopdf http://www.your-awesome-site.com some-awesome-file.pdf
- Install Imagemagick or poppler-utils. (apt-get install poppler-utils). This is for converting the PDF to a PNG. Imagemagick seems to run faster.
TCPDF is another widely used PDF conversion utility but I have found its layout and formatting options to be incredibly tedious. It makes a lot more sense to me to just print to PDF and let the Webkit browser do the heavy lifting .. with a helping hand from xvfb. You can invoke the xvfb command in PHP as follows: