Generate PDF with PHP | PHP2PDF Tutorial


Why Generate PDF Files With PHP?

No site is completed with out generating PDF file reports today. There are plenty of open source free libraries to achieve this task but my personal favorite is FPDF class. I find it suitable for the task because of its simplicity, relatively smaller size and ease of use.

Just head to the to download a free copy of the library. Unzip it in your web directory and include “fpdf.php” and that’s about it.

Source Code

Below, You can find a pretty basic no bells and whistles code to make use of fpdf library and generate pdf file with php.

[php] <? require(‘fdpf/fpdf.php’); $ObjPdf = new FPDF(); $ObjPdf->AddPage();
$ObjPdf->SetFont(‘Arial’,’ B’,16);
$ObjPdf->Cell(30,20,’Please generate the pdf for me..’);


Lets say you have saved the code in your local webserver’s root directory with file name “pdf.php” and you have library in a folder named “fpdf”, then you’ll open pdf.php in your browser, it will generate a pdf file.

Happy experimenting.

You can reach Waqas Yousaf through twitter @wiqi.

Leave a Reply

Your email address will not be published. Required fields are marked *