Creating PDF’s on the fly using PHP & FPDF

Generating PDF’s is an easy method to make a very nice printable and/or savable version of an article. This could be helpful in a WordPress blog or any articles website. This method utilizes the popular FPDF class.

First of all, I suggest you get the latest FPDF version from here. I suggest creating a directory such as “pdf” on your web hosting space. You will need to place the fpdf.php into that directory, you will only need it.

Let’s make our first “Hello World” example. Place the following in a file called test.php:

<?php
require('fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>

Now let’s see that code line by line…

require('fpdf.php');
This line includes our FPDF class that we need to create the PDF file.

$pdf=new FPDF();
This line creates a new instance of the FPDF class which will be binded to $pdf

$pdf->AddPage();
This line tells FPDF to add a new page to the PDF file; obviously we need one page so we will call this function once.

$pdf->SetFont('Arial','B',16);
This line tells the FPDF class to change the font to Arial, bold, 16 pt.

$pdf->Cell(40,10,'Hello World!');
This line is just like the “echo” of PHP, the text fields in PDF files are just sort of rectangles with text in them, so we want the width of 40 pt. and a height of 10 pt., the third value is the text to be written in that rectangular box.

$pdf->Output();
Our final line, which pretty much means: “we’re done, show us our PDF!”

That’s a very simple FPDF usage, however, here is a bit of more advanced version of FPDF usage:

<?php
require('fpdf.php');
$pdf=new FPDF("L", "mm", "A4");
$pdf->AddPage();
$pdf->SetFont('Arial','BIU',30);
$pdf->SetTextColor(0,0,255);
$pdf->Cell(60,20,'PDF+PHP Test',1,1,C,0);
$pdf->Output();
?>

Now let’s do a quick review of that more advanced code:

$pdf=new FPDF("L", "mm", "A4");
This will create a new instance but instead will default to creating landscape pages because of the first L, P can be used instead to default to portrait pages. The second value is the default measurement unit, a choice of point (pt), millimeter (mm), centimeter (cm) and inch (in) is given. The last value is the size of the page, the choice of A3, A4, A5, Letter & Legal is given.

$pdf->AddPage();
Simple function, just add the page, you can tell the function to create either a portrait (P) or landscape (L) by giving it as a first value (ex: $pdf->AddPage("L"), $pdf->AddPage("P")).

$pdf->SetFont('Arial','BIU',38);
This required function again says that we want an Arial, 30 mm in size (because of the default size unit), the 'BIU' simply tells that we want it to be Bold, Italic & Underlined.

$pdf->SetTextColor(0,0,255);
This sets the default text color for the text we will be writing, I’ve chosen blue here, the first value is red (r), the second is green (g) & blue (b).

$pdf->Cell(60,20,'PDF+PHP Test',0,1,C,0);
This makes the so famous rectangle with 60 mm of width & 20 mm of height, we wrote ‘PDF+PHP Test’ and the first 0 means we do not want a border. The 1 next to it means that once it’s done the cell, it will go to the beginning of the next line, if 0 is provided, then it will be to the right of it, if 2 is provided then it will go below. The C is just the alignment which is center of the text inside the box, possible values are left (L), center (C), right (R).

$pdf->Output();
Output our brand new colorful PDF file!

Thanks for reading!

  • http://www.douglaskarr.com Doug Karr

    Nice. Curious if you know of a means with FPDF that will automatically generate the appropriate number of pages? Or… even better, has anyone written a function to simply pass a URL and auto generate the PDF?

    Thanks!

  • http://filosofie.be onno

    I would like to see some comparing between fpdf, reportlab, pdfLib and other libs where you can make PDFs

  • Tarek Jubaer

    Is it possible to write the whole HTML file to a PDF file??????????

  • http://www.unwaba.net Mathieu C.

    We use HTMLDOC (http://www.htmldoc.org/) to convert our HTML outputs to PDFs. A little passthru and we’re set :) I’ve used FPDF in the past, great class, but why reinvent anything ? I mean, you can reproduce the same behavior with a simple command line tool…

    See.

  • http://www.unwaba.net Mathieu C.

    Last comment contained a code snippet which obviously got lost (I should have known better). Please discard my previous comment.

    Thanks.

    Matt

  • http://www.volny.cz/pizdetzhostu/val/ Adele

    Thanks man, i agree

  • Pingback: php web hosting

  • Adeq

    Kewl :)

  • http://www.altcom.co.uk Harv

    There is a problem with this on IE. If the page is served as https and the server is sending Pragma: no-cache, then the file will not work.
    These to lines need to be added to the output function were the other header() functions are:
    header(’Cache-Control: maxage=3600′);
    header(’Pragma: public’);

    Happy days!

  • http://www.leafwriter.com M.I.Z. Khalid

    At last, I found something that works, and works without version errors!!!

    Keep up your good work folks.
    Khalid – Sri Lanka

  • http://www.acomputerportal.com/players_and_viewers.html#PDF_Portable_Document_Files PDF Links

    Thank You :) This is Great for simple stuff. How about more complex issues, MS word Doc to PDF for example.

  • hana

    the alignment only has right,left anf center..how about justify..can’t fpdf creates justify alignment?

  • http://www.yalamber.com yalamber

    thank you very much for this tutorial. was a great help

  • Pingback: 2bouncy4u.com » Blog Archive » Need real options on VPS hosting?

  • Chris

    I’m having some problems with the FPDF class. I’m hoping you can help me. I’m using FPDF to write some text on a PDF. I’m displaying 8 different cells of text. The Cells are being displayed, but after two cells, it puts the next cell onto a new page as If I had asked for a new PDF page to be made, but I did not. I’m guessing it is somehow pushing them down and pushing it onto the next page. How can I get rid of these annoying spaces and make it all fit on only the one PDF page?

    I posted my complete issue with the code here: http://stackoverflow.com/questions/1496888/why-are-there-unexpected-spaces-on-my-pdf-using-fpdf-with-php

    You can view the outputted pdf here: http://idea-palette.com/pdf/

  • imigreat

    Great job man, you are a soul saver and the boss. You made my day! Thanx