Friday, October 5, 2012

HTML to PDF

Many projects require generating custom reports or information by converting the content from one format to other. The conversions may be to image, csv or pdf formats. The formats are converted for the ease of user. Certain file formats are best suited for different user specific requirements. For example, text/html is best format for emails, xml/json for web services, pdf or doc for saving the details for reference and taking printed hard copy. HTML and text documents are a non-issue for developers developing websites.
There are useful open source tools and libraries for generating images of various formats, csv(comma separated values), doc/docx and xls. Today, we are going to evaluate the options for PDF generation. Let’s have a look at some of the options available for generating PDF.
  1. FPDF: It is a simple class for generating PDF from pure PHP without using any extension like PDFlib library. And “F” in FPDF stands for “Free”. FPDF requires no special PHP extensions. Though, Zlib and GD extensions are to be enabled to support compression and GIF support respectively. It works with PHP 4 (at least 4.3.10) and PHP 5. The tutorials, download and demos are available at the project site http://www.fpdf.org/
  2. eZPDF: If you can’t control the PHP extensions, ezPDF is the class for you. It is another open source PDF generation class which generates PDF on the fly without requiring any special extensions at all. The performance is quite good but the features are slightly limited for complex outputs. Though, it is an excellent tool for generating simple PDF files. For details and documentation, you may refer the project site http://www.ros.co.nz/pdf/
  3. DOMPDF: It is a PHP5 (PHP 5.3 recommended) based PDF generation library and renders PDF primarily with PDFlib or with bundled and enhanced version of CezPDF class of R&OS. It is a feature rich library and supports almost all the HTML elements and styling attributes. Using with PDFlib, you will be required to download and enable PECL extension of PDFlib. The Mbstring is also required for the library to work. For further reading, refer to DOMPDF project home; the code is hosted at Google Code.
  4. FPDI: It is an extension built on FPDF. It is essentially a collection of PHP classes for reading existing PDF files and generates new PDF files from the existing files. The existing PDF files are used as templates to generate new files. It doesn’t require any additional library or PHP extension. The details are available at the FPDI Project Home.
  5. TCPDF: It is another extension built on FPDF. It extends the features provided by FPDF to provide even more options for generation of complex PDF structures. Other than almost complete HTML support, it also supports Javascript, digital signatures, barcodes and much more. For further reading, download, examples and documentation, please refer to the Project Homepage.
  6. PDFlib: For those who are keen on buying expensive licenses, PDFlib is the PDF generation library. Working on Open source technologies, I don’t prefer paid options. The details can be read at the project home.
  7. HTML2PDF: HTML2FPDF is a PHP Class library that uses the FPDF class library to convert HTML files to PDF files. HTML2PDF library is a collection of three classes namely PDF, HTML2FPDF and FPDF. The FPDF class of HTML2PDF is basically a modified version of original FPDF class. These classes are inherited classes with PDF class extending HTML2FPDF class, which in turn extends the FPDF class.