Generate PDF with PHP | PHP2PDF Tutorial

generate pdf from php php2pdf

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 fpdf.org 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.

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

Result

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.
Share

Waqas Yousaf is Freelance Web Developer, Blogger, start-up manager and Co-Founder of wishdd.com.

Leave a Reply

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

1 × three =