Examples

Go through the following two example, and then we will get into the bottom of this -

Example 1 : Hello World

Consider the following example -

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

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

Before you go any further you need to import the FPDF library on the top of any code.

After that you need to create a FPDF object through which we will create our PDF.

	$pdf = new FPDF();

There's no page at the moment, so we have to add one with AddPage(). This creates only one page.

Before we can print text, it's mandatory to select a font with SetFont(). We choose Arial bold 16:

	$pdf->SetFont('Arial','B',16);

We can now print a cell with Cell(). A cell is a rectangular area, possibly framed, which contains a line of text. It is output at the current position. After the Cell is added, the current position is moved after the Cell.

	$pdf->Cell(40,10,'Hello World !',1);

Finally we are sending output -

	$pdf->Output('name.pdf', 'D')

Example 2 : Header and Footer

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

	class PDF extends FPDF
	{
		// Page header
		function Header()
		{
			// Logo
			$this->Image('logo.png',10,6,30);
			// Arial bold 15
			$this->SetFont('Arial','B',15);
			// Move to the right
			$this->Cell(80);
			// Title
			$this->Cell(30,10,'Title',1,0,'C');
			// Line break
			$this->Ln(20);
		}

		// Page footer
		function Footer()
		{
			// Position at 1.5 cm from bottom
			$this->SetY(-15);
			// Arial italic 8
			$this->SetFont('Arial','I',8);
			// Page number
			$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
		}
	}

	// Instanciation of inherited class
	$pdf = new PDF();
	$pdf->AliasNbPages();
	$pdf->AddPage();
	$pdf->SetFont('Times','',12);
	for($i=1;$i<=40;$i++)
		$pdf->Cell(0,10,'Printing line number '.$i,0,1);
	$pdf->Output();
	?>

In the above example we have created a Class which extends FPDF class.

This example makes use of the Header() and Footer() methods to process page headers and footers. They are called automatically. They already exist in the FPDF class but do nothing, therefore we have to extend the class and override them.

Within the header function we have inserted a logo using Image(), we have set the header font using SetFont(), we have created a Cell to insert a title and we gave some line break using Ln()

In the footer method, we have positioned the footer from the bottom, we have set the font, and created a Cell to insert the page Number.

__construct()

	
	__construct([string orientation [, string unit [, mixed size]]])

This is the class constructor. It allows to set up the page size, the orientation and the unit of measure used in all methods (except for font sizes).

orientation

Default page orientation. Possible values are (case insensitive):

Default value is P

unit

User unit. Possible values are:

A point equals 1/72 of inch, that is to say about 0.35 mm (an inch being 2.54 cm). This is a very common unit in typography; font sizes are expressed in that unit. Default value is mm.

size

The size used for pages. It can be either one of the following values (case insensitive):

or an array containing the width and the height (expressed in the unit given by unit).

Default value is A4.

	
	$pdf = new FPDF('P','mm',array(100,150));

Output()

	
	string Output([string dest [, string name]])

Send the document to a given destination: browser, file or string. In the case of a browser, the PDF viewer may be used or a download may be forced. The method first calls Close() if necessary to terminate the document.

dest

Destination where to send the document. It can be one of the following:

name

The name of the file. It is ignored in case of destination S. The default value is doc.pdf

In case when the PDF is sent to the browser, nothing else must be output by the script, neither before nor after (no HTML, not even a space or a carriage return). If you send something before, you will get the error message: "Some data has already been output, can't send PDF file". If you send something after, the document might not display.

Close()

	
	Close()

Terminates the PDF document. It is not necessary to call this method explicitly because Output() does it automatically.

Header()

This method is used to render the page header. It is automatically called by AddPage() and should not be called directly by the application. The implementation in FPDF is empty, so you have to subclass it and override the method if you want a specific processing.


	class PDF extends FPDF
	{
		function Header()
		{
			// Select Arial bold 15
			$this->SetFont('Arial','B',15);
			// Move to the right
			$this->Cell(80);
			// Framed title
			$this->Cell(30,10,'Title',1,0,'C');
			// Line break
			$this->Ln(20);
		}
	}

Footer()

This method is used to render the page footer. It is automatically called by AddPage() and Close() and should not be called directly by the application. The implementation in FPDF is empty, so you have to subclass it and override the method if you want a specific processing.


	class PDF extends FPDF
	{
		function Footer()
		{
			// Go to 1.5 cm from bottom
			$this->SetY(-15);
			// Select Arial italic 8
			$this->SetFont('Arial','I',8);
			// Print centered page number
			$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
		}
	}