Creating PDF Using PHP

What is it About?

In this article you will learn how to create PDF documents using PHP on the fly.

Creating PDF Documents using PHP

Installing FPDF

There are many PHP libraries available for creating PDF files but I am gonna use FPDF in this article. Click here to download the FPDF library.
After downloading, extract all files and then copy fpdf.php file and font folder in your php project as shown below:

Installing FPDF library

Creating your first PDF

Now create/open index.php in your favourite editor and include fpdf.php file in index.php:

//<a href="http://www.php.net/require_once">require_once</a> is used so that FPDF class is

//included only once

//and if path is incorrent, then it will stop execution

//and display error



require_once( 'fpdf.php' );

In index.php, write following code and execute it in a browser.

&lt;?php

1)	$fpdf = new FPDF();

2)	$fpdf-&gt;AddPage();

3)	$fpdf-&gt;SetFont( 'Arial', 'B', 16 );

4)	$fpdf-&gt;Cell( 0, 9, 'Page Heading', 1, 1, 'C' );

5)	$fpdf-&gt;Output();

?&gt;

Now I shall explain each and every line of code.

  1. Create new object of class FPDF
  2. Add a page in pdf document. If you execute this line of code twice, then 2 pages will be added in a single pdf.
  3. Set font to Arial, Bold, and 16 Size
  4. Create heading with following parameters

    0 – 100% width

    9 – height

    “Page Heading” – With this text

    1 – Border around it

    1 – Move pionter to new line after writing this heading

    ‘C’ – Center aligned

  5. Finally generate PDF file and output it to the browser so that user can save it.

    Warning: Never echo anything in the script file which generate pdf!

Writing large text

To write a paragraph of text in PDF, add following line of code before $fpdf->Output();

$fpdf-&gt;write( "Here goes the paragraph of text.

If I hit enter/return, it will also move the pionter to new line. nSimilarly if I use n, it will again move the pionter to new line" );

The above line of code prints the provided string as a flowing text. Flowing text means that if string is not small enough to fit with in a single line, it will automatically move the pionter to a new line. If this method found n within a string, it will again automatically move the pionter to a new line.

Multiline heading

Note that Cell method can’t print multiline text. If you want multiline headings, I would prefer to use MultiCell method of class FPDF. Try these lines of code:

$fpdf-&gt;SetFont( 'Arial', 'B', 15 ); //set font to Arial, Bold, and 15 Size

$fpdf-&gt;MultiCell( 0, 6, 'This is multiline sub heading. This heading is created using MultiCell method.', 0, 1 );

Inserting an image

Now let’s insert an image in the document:

$fpdf-&gt;Image('myImage.jpg', 10, 65 );

The first parameter specifies the image to be inserted in the document. You can also specify absolute path like this http://www.xyz.com/image.jpg

Second and third paramters are x and y values. Note that top-left corner of the document is 0, 0.

After inserting an image, if you write something on the page using Write method, it will not appear after the image.
This happens because the Image method doesn’t insert image in the normal document flow.
It places the image according to the specified x, y coordinates.
To move the pionter down, use ln method

$fpdf-&gt;ln( 50 );

$fpdf-&gt;Write( 5, 'This is some text' );

Header and Footer

To generate header and footer, you need to extend FPDF class. Then you need to implement Header and Footer methods.
Now we shall move the lines that generate header in the Header method. Then we shall write some new lines in Footer to generate footer.

class PDF extends FPDF {

    function Header() {

	$this-&gt;SetFont( 'Arial', 'B', 18 );

	$this-&gt;Cell( 0, 9, 'Page Heading', 1, 1, 'C' );

	$this-&gt;ln( 5 );

    }



    function Footer() {

	//move pionter at the bottom of the page

	$this-&gt;SetY( -15 );



	//set font to Arial, Bold, size 10

	$this-&gt;SetFont( 'Arial', 'B', 10 );



	//set font color to blue

	$this-&gt;SetTextColor( 52, 98, 185 );



	$this-&gt;Cell( 0, 10, 'www.iFadey.com', 0, 0, 'L' );



	//set font color to gray

	$this-&gt;SetTextColor( 150, 150, 150 );



	//write Page No

	$this-&gt;Cell( 0, 10, 'Page No: ' . $this-&gt;PageNo(), 0, 0, 'R' );

    }

}

Once you created this extended class, now don’t forget to create new instance of PDF class instead of FPDF in the beginning of your script.

$fpdf = new PDF();

You maybe thinking why we created a new extended class for creating header and footer? Actually Header and Footer methods are automatically called whenever a new page is created in the pdf document.
So it automatically inserts header and footer on each page. You don’t need to replicate the header/footer code. Also you even don’t have to call them each time a new page is created.

Let’s take a look at the code in Header method. Actually this is exactly the same code written before to create the header. I move those three lines of code in this method and changed the $fpdf to $this.
If you are getting confused here, then better download the final script from the bottom of this article.

In Footer method:

  1. SetY( -15 ) – Move pionter at the bottom of the page
  2. SetFont( ‘Arial’, ‘B’, 10 ) – Set font to Arial, Bold, and size 10
  3. SetTextColor( 52, 98, 185 ) – Set font color to blue. These three parameters are RGB values. You can get RGB values from any web design tool like Fireworks, Photoshop or check this website.
  4. Cell( 0, 10, ‘www.iFadey.com’, 0, 0, ‘L’ ) – Write text “www.iFadey.com” with left alignment and height of 10 and width of 100%
  5. $this->SetTextColor( 150, 150, 150 ) – Change font color to gray
  6. Cell( 0, 10, ‘Page No: ‘ . $this->PageNo(), 0, 0, ‘R’ ) – Write Page No with right alignment, 100% width, 10 height. PageNo() method returns the current Page No.

Download final script file

Click here to download the final script file that I created for this tutorial.

Want to learn more…

I have covered the basics of FPDF in this article. Following are some resources from where you can learn more about using FPDF.

  1. FPDF API Reference
  2. FPDF Tutorials on fpdf.org
  3. Generating Colorful Graph Using FPDF

comments powered by Disqus