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:

http://www.fpdf.org/en/download.php

1

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:

require(‘FPDF.php’);

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:
2

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,’ ‘,’http://www.sun.org/’): Using this you can add image with link. In order to put link only you can use this:

$pdf->Link(10, 20, 33,33, 'http://www..org/');

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;
 $rostersCount=count($rosters);
 $rostersPerPage = 14;
 $totalPages = ceil($rostersCount / $rostersPerPage);
 while($pageNumber <= $totalPages){ $pdf->AddPage('L');
     $pdf->SetFont('Arial','B',14);
     $pdf->Cell(40,7,'Start Date: '.$startDate,0,1);
     $pdf->SetFont('Arial','B',14);
     $pdf->Cell(40,7,'Start Time: '.$startTime,0,1);
     $pdf->SetFont('Arial','',12);
     $pdf->SetFont('Arial','B',14);
     $pdf->Cell(40,7,'Instructor: '.$instructorName['ClassInstructor']['name'],0,1);
     $pdf->SetFont('Arial','B',14);
     $pdf->Cell(40,7,'Roster for '.$courseName,0,1);
     $pdf->SetFont('Arial','',12);
     $pdf->Cell(0,7,'',0,1,'R');
     //Table heading
     $pdf->SetFont('Arial','B',13);
     $pdf->SetFillColor(245,245,245);
     $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(40,10,'Signature',1,0,'C',true);
     $pdf->Cell(30,10,'Time out',1,0,'C',true);
     $pdf->Cell(40,10,'Signature',1,0,'C',true);    
     $pdf->Cell(30,10,'Status',1,1,'C',true);
     // Table rows:
     $rowNumber = 1;
     $pdf->SetFont('Arial','',13);
     $pdf->SetFillColor(250,250,250);
     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(50,9,$firstName,1,0,'C',true);
         $pdf->Cell(30,9,$rosters[$count]['Time in'],1,0,'C',true);
         $pdf->Cell(40,9,$rosters[$count]['Signature1'],1,0,'C',true);
         $pdf->Cell(30,9,$rosters[$count]['Time out'],1,0,'C',true);
         $pdf->Cell(40,9,$rosters[$count]['Signature2'],1,0,'C',true);
         $pdf->Cell(30,9,$rosters[$count]['Status'],1,1,'C',true);
         $rowNumber++;
         $count++;
     }
     $pageNumber++;
 }
 $pdf->Output();

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

Leave a Reply

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