Creating dynamic PDFs using PHP and FPDF

The first thing you will want to do is download the FPDF class files from http://www.fpdf.org/ – this site is also an excellent resource for the FPDF class, if you get stuck then a look through the manual or frequently asked questions will help out no end.

In order to use the class we will include it in the main PHP processing file, using (suprise) the PHP “include” function. So at the very top of our document we have:

include ("FPDF/fpdf.php");

This is assuming that you have placed your FPDF files in a sub directory called “FPDF”.

To start with – let create a new PDF and add some text to our PDF to get things up and running and make sure everything is working OK.

Firstly we call our class constructor with any variables we require:

$PDF = new FPDF('P','pt','A4');

So, in this line we have created $PDF as a new FPDF object and set the following defaults, ‘P’ means our document is Portrait, ‘pt’ is the measurement unit we are using – in this case points and ‘A4’ sets our document to A4 size.

Next we will add some text, but first we need to create our first page:

$PDF->AddPage();

Here we are calling the AddPage method of our object. With FPDF you must call AddPage initially, but by default if your content then overruns subsequent pages will be added for you. Now we can add our text:

$PDF->SetFont('Arial','B',16);
$PDF->Cell(40,10,'Hello World!');
$PDF->Output();

The first line is fairly obvious – we are setting our font family, making the text bold and setting the size to 16. The second line
creates a new cell 40 pts wide and 10 points high, the third line finalizes and outputs our PDF – a quick note on this – you must not have any other output from the page (so no echo’s or print_r’s or anything) when using this method.

So if we now run that code we now get the following in a PDF:

wp1

The most important thing to understand is how the PDF generation works in terms or element positioning. You can specify X and Y positions absolutely and also specify the position of the next element when creatign the current element. For example – if we change the code to be:

include ("FPDF/fpdf.php");
$PDF = new FPDF('P','pt','A4');
$PDF->AddPage();
$PDF->SetFont('Arial','B',16);
$PDF->Cell(100,10,'Hello World!',0,0);
$PDF->Cell(100,10,'Element next to last element',0,1);
$PDF->Ln('10');
$PDF->Cell(100,10,'Element below other elements',0,0);
PDF->Output();

The two new parameters at the end of our calls to the “Cell” mthod specify a border of 0 and then either 0 for positioning an element to the left or 1 to position the element below. We ha\ve also added the Ln(’10’) to create a 10 point line break. The output then becomes this:

wp2

So now I will show a more complete (and hopefully) useful example for creating a letter dynamically as a PDF (as one of the advantages of using PDFs in this way is their extremly useful for atatching to emails / printing when standard HTML just does not cut it).

Using the following code:

>include ("FPDF/fpdf.php");
$PDF = new FPDF('P','pt','A4');
$PDF->SetMargins('30','30');
$PDF->SetAutoPageBreak(true,'30');
$PDF->AddPage();
$PDF->SetFont('Arial','',10);
$PDF->Cell(0,10,'11 Test Street',0,1,'R');
$PDF->Cell(0,10,'Madeup Road,',0,1,'R');
$PDF->Cell(0,10,'Somwhereville,',0,1,'R');
$PDF->Cell(0,10,'ABX 567,',0,1,'R');
$PDF->Ln('30');
$PDF->SetFont('Arial','',11);
$PDF->Cell(100,10,'Dear Mr Man,',0,1);
$PDF->Ln('30');
$PDF->MultiCell(0,11,'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec consectetur, lectus sit amet fermentum egestas, nulla velit hendrerit libero, non iaculis nulla erat quis ligula. Etiam vel sem at est hendrerit ornare quis nec sem. Phasellus ultrices aliquam elit, et rhoncus purus imperdiet sit amet. Morbi sed odio nisi, ac volutpat dolor.',0,'L');
$PDF->Ln('30');
$PDF->Cell(0,10,'Yours Sincerly,',0,1,'R');
$PDF->Ln('10');
$PDF->Image('sig.jpg','450',null,120);
$PDF->Ln('10');
$PDF->SetFont('Arial','B',11);
$PDF->Cell(0,10,'Robert Basden',0,1,'R');
$PDF->Output();

Gives the following output:

wp3

Note the use of an image for the signature – we could have used the AddFont method – but that requires you to also prepare your font for addition using other tools – however this would be more flexible in the long run.

I would suggest having a look through the FPDF user manual and oher examples that have been provided on the site. All in all FPDF is an extremely useful and powerful addition to a web developers tool kit.

Rob Basden

Rob founded a web design company company which was acquired by Linear Blue in 2009 in order to bring web development to bring professional web development services in-house. After graduating from Warwick University with a bachelors degree in Computer Systems Engineering, Rob entered the world of web development. He began by supporting a development team then moved on to running his own team as development director. His skills include extensive knowledge of PHP / HTML / Javascript / CSS / Ajax methodologies and Flash / Actionscript.

More Posts

6 thoughts on “Creating dynamic PDFs using PHP and FPDF”

    1. Hi there Raahsalah,

      The main cell method does not deal with line breaks, your first port of call should be to look at MultiCell method from the manual at: http://www.fpdf.org/.

      Without digging through the code I believe this sets up multiple cells with a line break denoting a new cell. If your text file does not have line breaks then the text will wrap automatically when it reaches the right hand border otherwise each new line character (\n) will force a line break.

      I hope that helps! If not please feel free to message again.

      Kind regards.

      Rob

  1. Using a ‘for’ loop I wish to generate several pdf files for different strings stored in an array.

    But FPDF or $pdf is producing only one pdf file i.e. working only once. I get an information that FPDF will work only once. How to use it again and again within the same for loop and get more than one pdf file?
    e.g. liver.pdf, heart.pdf, kidneys.pdf etc. I have given outputfile name as ‘result’.’$h’.’.pdf’ for download. result0.pdf I am getting. result1.pdf, result2.pdf, result3.pdf etc. I am unable to get.

    I find that FPDF is more convenient than dompdf, tcpdf, ezpdf etc. etc. except the above hitch of not being able to repeat the output using single php file and single php program. I could produce dozens of text and html files using one php script without problems. For pdf, the problem seems to be a check of something already being sent to server. But this objection, CezPDF or dompdf is not raising.

Leave a Reply