PHP allows you to generate PDF files dynamically, which can be useful for a variety of tasks. FPDF is a free PHP class containing a number of functions that let you create and manipulate PDFs.

PDFlib

The PHP API contains a number of functions for handling PDF files designed to be used with the PDFlib. Although extensive, this library is not free for commercial use. A free version called PDFlib Lite is available for personal use, but is limited in functionality. To use the full PDFlib library you must purchase a rather expensive license.

Why FPDF?

An alternative way of generating PDF files with PHP is using FPDF, a free PHP class containing a number of functions for creating and manipulating PDFs. The key word here is free. You are free to download and use this class or customise it to fit your needs. In addition to being free, it's also simpler to use than PDFlib. The PDFlib needs to be installed as an extension in your PHP package, whereas FPDF can just be included in your PHP script and it's ready to use.

Creating PDF files

To get started, you will need to download the FPDF class from the FPDF Web site and include it in your PHP script like this:

require('fpdf.php');

Below is an example of how you can generate a simple PDF using FPDF.

We begin by creating a new FPDF object with:

$pdf= new FPDF();

The FPDF constructor can take the following parameters:

|>String orientation (P or L) -- portrait or landscape
|>String unit (pt,mm,cm and in) -- measure unit
|>Mixed format (A3, A4, A5, Letter and Legal) -- format of pages

Next, we are going to set some document properties:

$pdf->SetAuthor('Lana Kovacevic');
$pdf->SetTitle('FPDF tutorial');

Because we want to use the same font throughout the whole document, we can set it before we create a page.

$pdf->SetFont('Helvetica','B',20);
$pdf->SetTextColor(50,60,100);

The SetFont function takes three parameters; the font family, style and size. We are using Helvetica, Bold and 20 points, which will be applied to the title of our document. You can either use one of the regular font families or set up a different one using the AddFont () function.

With SetTextColor () we are also setting the font colour for the entire document. The colours can be represented as RGB or grey scale. Here we are using RGB values.

Now that that's done, let's set up a page for our PDF document.

$pdf->AddPage('P');
$pdf->SetDisplayMode(real,'default');

You can pass the AddPage () a parameter of "P" or "L" to specify the page orientation. I've used "P" for portrait. The SetDisplayMode function determines how the page will be displayed. You can pass it zoom and layout parameters. Here we're using 100 percent zoom and the viewer's default layout.

Now, that we've set up a page, let's insert an image to make it look nicer and make it a link while we're at it. We'll display the FPDF logo by calling the Image function and passing it the following parameters -- name of the file, the dimensions and the URL.

$pdf->Image('logo.png',10,20,33,0,' ','http://www.fpdf.org/');

You could have also inserted the link with:

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

Now let's make a title for our document with a border around it.

$pdf->SetXY(50,20);
$pdf->SetDrawColor(50,60,100);
$pdf->Cell(100,10,'FPDF Tutorial',1,0,'C',0);

The SetXY function sets the position of x and y coordinates, where we want the title to appear. SetDrawColor will set the colour of the border, using RGB values. After that's done, we call the Cell function to print out a cell rectangle along with the text of our title. We are passing the function the following parameters; width, height, text, border, ln, align and fill. The border is either 0 for no border or 1 for frame. For ln we are using the default value 0, "C" to centre align the text inside it and 0 for fill. Had we used 1 for fill the rectangle would have been coloured in. With a value of 0 we are making it transparent.

Now we want to write the main text to the PDF, that is display a little message.

$pdf->SetXY(10,50);
$pdf->SetFontSize(10);
$pdf->Write(5,'Congratulations! You have generated a PDF. ');

Again we are setting the x and y positions of the text, but this time we are reducing the font size with the SetFontSize function. The write function will print the text to a PDF. The parameter 5 will set the line height. This is only relevant however, if there are multiple lines of text.

Finally we want to send the output to a given destination, using the Output function.

$pdf->Output('example1.pdf','I');

Here we are passing the function the name of the file and the destination, in this case "I". The "I" parameter will send the output to the browser.

Putting it all together:

<?php
require('fpdf.php');

//create a FPDF object
$pdf=new FPDF();

//set document properties
$pdf->SetAuthor('Lana Kovacevic');
$pdf->SetTitle('FPDF tutorial');

//set font for the entire document
$pdf->SetFont('Helvetica','B',20);
$pdf->SetTextColor(50,60,100);

//set up a page
$pdf->AddPage('P');
$pdf->SetDisplayMode(real,'default');

//insert an image and make it a link
$pdf->Image('logo.png',10,20,33,0,' ','http://www.fpdf.org/');

//display the title with a border around it
$pdf->SetXY(50,20);
$pdf->SetDrawColor(50,60,100);
$pdf->Cell(100,10,'FPDF Tutorial',1,0,'C',0);

//Set x and y position for the main text, reduce font size and write content
$pdf->SetXY (10,50);
$pdf->SetFontSize(10);
$pdf->Write(5,'Congratulations! You have generated a PDF.');

//Output the document
$pdf->Output('example1.pdf','I');
?>

Now that you've learnt how to generate a simple PDF, let's see what else we can do with FPDF. The example code below demonstrates how to make a header and a footer for your document.


<?php
require('fpdf.php');

class PDF extends FPDF
{
  function Header()
    {
      $this->Image('logo.png',10,8,33);
      $this->SetFont('Helvetica','B',15);
      $this->SetXY(50, 10);
      $this->Cell(0,10,'This is a header',1,0,'C');
     }

  function Footer()
    {
      $this->SetXY(100,-15);
      $this->SetFont('Helvetica','I',10);
      $this->Write (5, 'This is a footer');
    }
}

$pdf=new PDF();
$pdf->AddPage();
$pdf->Output('example2.pdf','D');
?>


As you can see we are creating a child class of FPDF using inheritance and setting up the behaviour for both the Header and Footer functions. We then create a new object of this PDF class and add a page to our document. The AddPage () will automatically call the Header and Footer. Finally, we output it to a file called example2.pdf, this time using the "D" option for the sake of the example. This will send the file to the browser and open a dialog box, prompting the user to save the file.

Now that you have an understanding of how FPDF works, go ahead and play with some of its functions and have fun building your own PDFs. For the full list of functions, refer to the FPDF Web site.

Wide World of Web This was published in Wide World of Web, check every Wednesday for more stories

Related links

Comments

1

Struchkov Vladimir - 22/02/08

This article is nice. But many programmers can't use this library. This class doesn't suppurt for example cyrilic language. Look at this page http://acko.net/node/56. This is extension forf fpdf

» Report offensive content

2

Sean Falzon - 21/03/08

Personally a better option for generating PDF's is FOP

because FOP can output other formats not just PDF's you can cater for a broad range of requirements using the same templates.

also FPDF has not been updated in 4 years so that generally makes me wonder what happened to development.

» Report offensive content

3

me_great - 25/03/08

I want to convert a whole webpage into pdf using php.
Can you please help.

» Report offensive content

4

George - 03/05/08

HTML 2 FPDF > PDF would be nice. Usually one would like to think at a higher level, that is, tables (for example) with information in them and not cells. That way one could focus on the information to be presented instead of writing a custom rendering engine each time.

I have probably shot myself in the leg, I will go an read the manual now :)

» Report offensive content

5

mukti - 03/06/08

Good, but i have a question, how make a pdf file with a password?

» Report offensive content

6

Tim - 09/07/08

Any idea how much processing power it would take to generate a 300dpi .pdf file the size of an 8.5x11 piece of paper?

» Report offensive content

7

Ketan Patel - 17/07/08

Hi,

I use fpdf to generate pdf. it is working fine. but when i use session variables with it start problem .. IE need to refresh or retry to generate pdf...

» Report offensive content

8

ming_andrada - 30/07/08

hi, this one is really nice, i just got question here? about the logo.png, guys, where can i save it? and how can i call that image?

pls do give me ur answers...

thanks a lot...

» Report offensive content

9

Greegus Virere - 02/08/08

Ketan Patel > Mess with that one too... if you use output after calling session_start() page (in my case in opera) just stuck while loading. i "solved" the problem by putting script into new file without sessioning.

» Report offensive content

10

http://www.b2c-shopping.co.uk - 09/08/08

I am learn build pdf file these days, I want to read a excel file and export it to pdf, can PHP do this? thanks.

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

10

http://www.b2c-shopping.co.uk - 08/09/08

I am learn build pdf file these days, I want to read a excel file and export it to pdf, can ... more

9

Greegus Virere - 08/02/08

Ketan Patel > Mess with that one too... if you use output after calling session_start() page (in my case in opera) ... more

8

ming_andrada - 30/07/08

hi, this one is really nice, i just got question here? about the logo.png, guys, where can i save it? and ... more

Log in


Sign up | Forgot your password?

  • Chris Duckett Safari gets Gears

    Since its release in May last year, Gears has supported only Internet Explorer and Firefox browsers. With the addition of Safari into the Gears fold, it closes the loop of major browsers to support Gears Read more »

    -- posted by Chris Duckett

  • Renai LeMay MyPerfect.com.au has potential

    Victorian Web start-up My Perfect has a strong story and rationale for why it will succeed. But it has to overcome some challenges and design flaws first. Read more »

    -- posted by Renai LeMay

  • Brendon Chase Blog against poverty

    Worldwide Blog Action Day is 15 October, in 2008 the goal is to raise awareness and conversation around the worldwide topic of poverty and in the process raise money for the cause. Who's in? Read more »

    -- posted by Brendon Chase

What's on?