The ramblings of a Web Developer
In: Development|PHP
17 Aug 2010Recently, I had a spec to produce PDF downloads/exports of a reporting suite. On the face of it, this doesn’t sound like a particularly complicated task, but after some research it turned out to be much more complicated than it needed to be.
The programming language was PHP, with some parts being converted to C++ using Facebook’s Hip-Hop. Ill therefore be talking about PHP in this post.
Googling ways of producing PDFs from HTML, using PHP brings up a number of solutions:
Most of these have some basic requirements, and some common limitations:
To this end, i carried on looking – I know I needed a better solution than any of these offered. Eventually I stumbled upon WKHTMLTOPDF. This is basically just a server side install of WebKit, with a wrapper around to focus and extend some of its functionality. It’s key function – being able to save rendered web pages (including JavaScript execution), and save them as a PDF.
This actually makes generating PDFs absolutely easy. All you have to do, is send the HTML, CSS & JS that would have gone to the browser, to a file. Then pass this file into WKHTMLTOPDF, and serve the generated PDF to the browser!
Here’s a fe lines of PHP to show just how easy is to do!
And there you have it! Top quality PDFs, with minimal code!
Blog of Jon Reed. I am Senior Software Engineer, at AOL UK. I believe in working had & playing hard. I love gadgets and technology.