PhantomJS for HTML to PDF Rendering
Published 2 years ago
Several years ago, I was assigned the task of generating large PDFs from HTML using dompdf. The rules were simple, write the HTML and corresponding CSS, pass this off to dompdf and it spits out the content in PDF format, or so I thought. But, the results were far from fantastic. CSS was barely supported, element position was mostly ignored, images never seemed to display correctly, and rendering time was abysmal. We ended up writing two completely separate HTML documents, one for display in the browser and one for conversion to dompdf.
One down-side is that PhantomJS is a stand-alone application. It must be compiled and installed on the system you want to use it on. Fortunately, there are some very kind volunteers, who take it upon themselves to compile binaries for some of the most common operating systems. In my case, I found a compatible linux binary, copied it to the server, and it was ready to go.
Rendering is one of many features of PhantomJS, but this one alone makes it stand above it's predecessors. Next time you need to render your web content, take a look at PhantomJS. You will not be dissapointed!