Generating PDF files with PHP

PHP does not provide a native library for generating PDF files, instead of that they offer an external PHP extension called PDFlib. You can find the library documentation here. A big drawback of this tool is that the full package is not available as open source, only a Lite version of it that supports only a subset of it’s functionality.

Using PDFlib is pretty straight forward, you can check out a couple of examples here

I’m going to give you a different alternative to work with, it’s called FPDF. This PHP class is a great tool for us as developers, as it allows us to generate PDF files directly from PHP, without the need of using the PDFlib library.
The main advantage of this piece of code is that you can use it without any restrictions.

FPDF is a very good choice if you want to be able to create more complex PDF files, however the performance of the class is not as good as the performance of the library, when you want to create large documents.

Here is the most basic example of how to use FPDF:

<?phprequire('fpdf.php');$pdf = new FPDF();$pdf->AddPage();$pdf->SetFont('Arial','B',16);$pdf->Cell(40,10,'Hello World!');$pdf->Output();

For more examples and documentation go to the project’s page

Facebook Comments

comments

About

I am super cool guy that likes to code anywhere I go, That pic you see of me is me in the Mountain Cerro de Muerto with my laptop, hacking away on the latest HTML5 Project

Tagged with: ,
Posted in post
One comment on “Generating PDF files with PHP
  1. Zeitgeist says:

    I need that for one of my projects.

Leave a Reply