Creating PDF Files in PHP by Using the FPDF Library

Creating PDF Files in PHP by Using the FPDF Library

FPDF is a free library in PHP for generating PDF files. It has high level functions and support various features such as margins, header, footer, page break, images, colours, links and true type fonts. This post shows you how to use the FPDF library to generate a PDF file in PHP.

Setup the FPDF library

You can download the latest FPDF library ZIP file from here. Then, unzip the files in a temporary folder and copy this folder to under your web project's root directory.

How to use?

First, the FPDF library file needs to be included.

require('.\fpdf183\fpdf.php');

For example, fpdf183 is the folder where the FPDF library files are put.

To create the PDF file object, we can do like this:

$pdf = new FPDF();

Before adding content to the PDF file, we need to add a new page to the document.

$pdf->AddPage();

The AddPage() function can also be used to set the page's orientation, size and rotation.

AddPage([string orientation [, mixed size [, int rotation]]])

If you want to set the page's properties with landscape, A4 size and clockwise rotation 90 degree, you can do like this:

$pdf->AddPage('L', 'A4', 90);

The FPDF library provides Text() function to print a string in the document precisely.

 $pdf->Text(40, 40, 'Hello World!');

The first and the second parameters define the abscissa (x) and ordinate (y) of the string's origin respectively. The origin is on the baseline and on the left of the first character. The above code snippet prints out string Hello World! starting from the point (40mm, 40mm) on the document.

Before printing strings, you can use SetFont() function to choose the font you want.

$pdf->SetFont('Times', 'B', 16);

Here, Times font family is used, and the font size is set as 16 with bold style.

The FPDF library also provides two basic drawing functions Line() and Rect() to draw lines and rectangles. To draw a line between two points, for example (40, 50) and (70, 80), we can call the Line() function like this:

$pdf->Line(40, 50, 70, 80);

Function Rect() can be used to print a rectangle by defining the upper-left corner point, width and height.

$pdf->Rect(40, 50, 30, 30);

The above code prints a square in the upper-left corner (40, 50) with side length of 30.

Before drawing, we can set the line width and the colour. Function SetLineWidth() defines the line width in mm (millimetre).

//sets the line width of 0.5mm
$pdf->SetLineWidth(0.5);

Function SetDrawColor() defines the colour for drawing with RGB components.

//set the drawing colour as yellow
$pdf->SetDrawColor(255, 255, 0);

After editing the PDF file content, don't forget to call the function Output() to send the document to the destination you want. Here, you can choose four options:

  • I: send the file inline to the browser
  • D: send to the browser and force a download
  • F: save to a local file
  • S: return the document as a string By default, option I is used:
    //by default, send the file inline to the browser
    $pdf->Output();
    

The following codes print out the string Hello World! and a yellow square with blue diagonals in a PDF file.

require('.\fpdf183\fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();

$pdf->SetFont('Times','B', 16);
$pdf->Text(40,40,'Hello World!');

$pdf->SetLineWidth(0.5);
//set the drawing colour as yellow
$pdf->SetDrawColor(255, 255, 0);
$pdf->Rect(40, 50, 30, 30);

//set the drawing colour as blue
$pdf->SetDrawColor(0, 0, 255);
$pdf->Line(40, 50, 70, 80);
$pdf->Line(70, 50, 40, 80);

//send the file inline to the browser
$pdf->Output();

Open your browser and run the PHP file that contains the above codes, it will create the PDF file as below. fpdf.JPG

Thanks for reading! To find more programming tutorials, please visit: CodeBilby.com

Did you find this article valuable?

Support Yan's Blog by becoming a sponsor. Any amount is appreciated!