PHP pdf generation

There are many options to use when generating a pdf, some are quicker to implement than others. There are a few popular libraries that can be used to layout a pdf:

  • fpdf
  • tcpdf
  • R&OS pdf

I have used fpdf in the past to generate pdf documents and it works quite well. One thing that can be annoying with pdf generation is having to lay out each components X and Y location. If you want to avoid this you can use an html to pdf tool. I have used dompdf for this purpose. You can use general html layout to setup your document then run it through dompdf and you will get a pdf that closely resembles the html layout you created. One word of wisdom here, it is better to use tables for layout in html when you are going to convert to pdf. Many of the html to pdf converters do not play well with advanced css positioning; even with basic html layout you may have to adjust a few items to get it to look right as a pdf.

Dompdf can utilize many different pdf libraries when converting to pdf, so if you have a preference you can setup dompdf to use it. Dompdf comes out of the box with adapater classes that can utilize: pdflib, tcpdf and R&OS pdf. By default dompdf will use the bundled version of R&OS pdf class.

One major advantage of using one of the aforementioned libraries, is that you do not need to install any php extensions to use them. The libraries are created in php and therefore can be dropped in on most hosting environments and utilized easily as an include.

A short example on using dompdf:

require_once('dompdf/dompdf_config.inc.php');

$dompdf = new DOMPDF();
$dompdf-> set_paper('letter','portrait');

$dompdf->load_html( file_get_contents('your_html_layout.html') );

$dompdf->render();
$dompdf->stream('generated_pdf.pdf');

Comments

Be the first to leave a comment on this post.

Leave a comment

To leave a comment, please log in / sign up