Web Design / Development

Generating PDFs and PNGs on Ubuntu

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;

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:

<?php
$url = 'http://www.your-awesome-site.com/somepage.html;
$path = '/home/youraccount/somedir'; //path to where you want to write the file. Make sure the web user (www-data) owns this folder and has write permissions.
$pdfcommand = 'xvfb-run --server-args="-screen 0, 960x768x24" wkhtmltopdf '.escapeshellarg($url).' '.escapeshellarg($path).'some-awesome-file.pdf';
exec($pdfcommand);
//Then to convert the file to PNG:
$pngcommand = 'convert -density 300 '.escapeshellarg($path).'reminder.pdf '.escapeshellarg($path).'some-awesome-file.png';//imagemagick seems faster. 'convert' is the Imagemagick command. 
exec($pngcommand);
?>

Add comment


Security code
Refresh

request-a-quote

© 2003-2012 Plethora Design, LLC · Contact Us · Make a Payment · Search

877-51_Skype5-6682 / 703-291-8022. Northern Virginia web design company convenient to Washington, D.C. and Maryland.