Encounters with domPDF of the 1st kind
While working with one of my clients, there was a requirement to generate PDF dynamically.
Since the project was done in PHP, I had to naturally look for something that worked well with PHP.
A little search and I came up with the following options :
- FPDF (http://www.fpdf.org/)
- ezPDF (http://www.ros.co.nz/pdf/)
- PDFlib (http://www.pdflib.com/products/pdflib-7-family/pdflib/)
- domPDF (http://code.google.com/p/dompdf/)
But some of the hurdles were:
- The PDF generated would be using an external style sheet.
- There would be complex table structures.
- There should be no dependency on external library.
The best option which met all the requirements was domPDF.
So I went ahead and gave domPDF 0.5.1 a shot.
After having spent quite a time to get it working, it still didn’t fail to chew out errors on me
So I decided to give domPDF 0.6.0 Alpha 2 a try.
What I found was that it was a lot better than the stable 0.5.1 and it didn’t give me all those previous headaches. The usage was fairly simple and my PDF was generated on the fly without any hiccups.
Impressed as I was, I sent the sample generated PDF to the client. But to my dismay he said that the PDF was corrupt and Adobe was not able to open the same (I am on Ubuntu FYI). My new-found joy hit a roadblock. Determined not to let this stop me I searched through the Wiki and the Discussion group of dompdf.
In one of the post a user had pointed out a similar corruption issue and a solution was provided.
I used the same and bang-on-target, the generated PDF opened up properly in Adobe Reader.
The code snippet that I used was :
< ?php ob_start(); ?> /* * Your dynamically generated output * having reference to external CSS files * and images */ < ?php $page = ob_get_contents(); ob_end_clean(); $filepdf = "[Name_of_your_dynamically_generated_file].pdf"; require_once("[Path_to_domPDF_folder]/dompdf_config.inc.php"); $dompdf = new DOMPDF(); $dompdf->load_html($page); //This is the part to create a error free PDF $locale_string = setlocale(LC_ALL, 0); setlocale(LC_NUMERIC, array('en_US.UTF8', 'en_US.UTF-8', 'en_US.8859-1', 'en_US', 'American', 'ENG', 'English')); $dompdf->render(); setlocale(LC_ALL, $locale_string); $dompdf->stream($filepdf); ?>
Hope this undocumented code snippet part will help other developers.