Wednesday, 14 August 2019

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, colors, links and true type fonts. This post shows you how to use the FPDF library to generate PDF files in PHP.

Setup the FPDF library

You can download the FPDF library ZIP file from here. Then, unzip the file in a temporary folder and copy the "font" folder and the PHP file fpdf.php to under your website's root directory. The "font" folder contains some prebuilt font definition files and the file fpdf.php is the FPDF library file.

How to use?

This simple example shows you how to use the FPDF library to create a PDF file embedded with an image and send it to the browser. Under your website's root directory, let's create a PHP script file called create-pdf-files.php. Copy and paste the following codes into the file.


    $imgName = 'mypng.png';
    $myImg = imagecreate(200, 60);
    $bgColor = imagecolorallocate($myImg, 0, 0, 222);
    $textColour = imagecolorallocate($myImg, 255, 255, 0);
    $lineColour = imagecolorallocate($myImg, 128, 255, 0);
    imagestring($myImg, 4, 30, 20, "", $textColour);
    imagesetthickness($myImg, 5);
    imageline($myImg, 30, 40, 165, 40, $lineColour);
    imagepng($myImg, $imgName);

    imagecolordeallocate($myImg, $lineColour);
    imagecolordeallocate($myImg, $textColour);
    imagecolordeallocate($myImg, $bgColor);

    $pdf = new FPDF();
    $pdf->Cell(40,10,'Hello World!');

After including the library file fpdf.php in line 2, we create an image file (line 4-12) by using the PHP's GD and image functions. In line 5, the function imagecreate() is called to create an empty image file with the size of 200 x 60 (width x height). The function imagecolorallocate() is used to create the image's background color (line 6), text color (line 7) and drawing color (line 8). In line 9, the function imagestring() is used to draw a horizontal string in the image with the text color $textColor. Line 10 defines the drawing thickness and line 11 draws a line in the image. The function imagepng() is called to output the image to a PNG file. Codes from line 14 to line 17 are used for deallocating the memory.

Now, we have a PNG image file and can use the FPDF library to create a PDF file. In line 19, the FPDF instance is created and assigned to a variable $pdf. The function AddPage() is used in line 20 to add a new page in the PDF file. Line 21 sets the font and relevant size for printing the text 'Hello World!' in line 22. Line 23 gives a line break and, in line 24, the function Image() is called to insert the already created PNG file $imgName. The PDF file $pdf is sent inline to the browser by using the function Output(). Finally, the image file $imgName is deleted in line 26.

If you open the browser and access the PHP script create-pdf-files.php under your web server, the PDF file looks like this:

The new pdf file