PHP FPDF (To generate PDF files)

| By Webner

FPDF (To generate PDF files)
Using PHP we can generate PDF files dynamically. FPDF is a free PHP class that contains a number of PHP functions with the help of which we can create and manipulate PDFs. To get started with FPDF, we just need to download FPDF library from the FPDF website using the following link:


You can download the zip file as shown in the screenshot and can include it in your project by extracting from a zip file (copy the unzipped folder under vedors folder).

You can include the FPDF.php file in your PHP script like this:


After that you just need to create an object, like following:

$pdf = new FPDF();

Now using this object you can use available FPDF functions. Some of them are:

1. $pdf->AddPage(‘P’): Using this you can add a new page in your PDF. You can pass parameters ‘P’ or ‘L’ i.e Portrait or Landscape to specify the page orientation.

2. $pdf->SetFont(‘Arial’ ,‘B’, 20): You can set the font family, style and size using this function. You can see that in screen-short:

3. $pdf->SetTextColor(40,50,90): By using this you can set the font colour for the entire document. Here the colors are represented in RGB values.

4. $pdf->Image(‘sun.png’,10,20,33,0,’ ‘,’’): Using this you can add image with link. In order to put link only you can use this:

$pdf->Link(10, 20, 33,33, '');

5. $pdf->Output(): Finally you can send output using the Output() function.
This is an example of what we have used in one of our projects:

PHP Code:

$count = 0;
 $pageNumber = 1;
 $rostersPerPage = 14;
 $totalPages = ceil($rostersCount / $rostersPerPage);
 while($pageNumber <= $totalPages){ $pdf->AddPage('L');
     $pdf->Cell(40,7,'Start Date: '.$startDate,0,1);
     $pdf->Cell(40,7,'Start Time: '.$startTime,0,1);
     $pdf->Cell(40,7,'Instructor: '.$instructorName['ClassInstructor']['name'],0,1);
     $pdf->Cell(40,7,'Roster for '.$courseName,0,1);
     //Table heading
     $pdf->Cell(50,10,'Last Name',1,0,'C',true);
     $pdf->Cell(50,10,'First Name',1,0,'C',true);
     $pdf->Cell(30,10,'Time in',1,0,'C',true);
     $pdf->Cell(30,10,'Time out',1,0,'C',true);
     // Table rows:
     $rowNumber = 1;
     while($rowNumber <= $rostersPerPage && $count < $rostersCount){ $firstName = substr($rosters[$count]['First Name'],0,$maxTitleLength); $lastName=substr($rosters[$count]['Last Name'],0,$maxTitleLength); $pdf->Cell(50,9,$lastName,1,0,'C',true);
         $pdf->Cell(30,9,$rosters[$count]['Time in'],1,0,'C',true);
         $pdf->Cell(30,9,$rosters[$count]['Time out'],1,0,'C',true);

And following is the screenshot of the output of this pdf file:

Leave a Reply

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