Example 1 : Line breaks and colors

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

	class PDF extends FPDF
	{
		function Header()
		{
			global $title;

			// Arial bold 15
			$this->SetFont('Arial','B',15);
			// Calculate width of title and position
			$w = $this->GetStringWidth($title)+6;
			$this->SetX((210-$w)/2);
			// Colors of frame, background and text
			$this->SetDrawColor(0,80,180);
			$this->SetFillColor(230,230,0);
			$this->SetTextColor(220,50,50);
			// Thickness of frame (1 mm)
			$this->SetLineWidth(1);
			// Title
			$this->Cell($w,9,$title,1,1,'C',true);
			// Line break
			$this->Ln(10);
		}

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

		function ChapterTitle($num, $label)
		{
			// Arial 12
			$this->SetFont('Arial','',12);
			// Background color
			$this->SetFillColor(200,220,255);
			// Title
			$this->Cell(0,6,"Chapter $num : $label",0,1,'L',true);
			// Line break
			$this->Ln(4);
		}

		function ChapterBody($file)
		{
			// Read text file
			$txt = file_get_contents($file);
			// Times 12
			$this->SetFont('Times','',12);
			// Output justified text
			$this->MultiCell(0,5,$txt);
			// Line break
			$this->Ln();
			// Mention in italics
			$this->SetFont('','I');
			$this->Cell(0,5,'(end of excerpt)');
		}

		function PrintChapter($num, $title, $file)
		{
			$this->AddPage();
			$this->ChapterTitle($num,$title);
			$this->ChapterBody($file);
		}
	}

		$pdf = new PDF();
		$title = '20000 Leagues Under the Seas';
		$pdf->SetTitle($title);
		$pdf->SetAuthor('Jules Verne');
		$pdf->PrintChapter(1,'A RUNAWAY REEF','20k_c1.txt');
		$pdf->PrintChapter(2,'THE PROS AND CONS','20k_c2.txt');
		$pdf->Output();
	?>
See Demo

The example above is very basic. It just prints string. First we have created a class PDF which extends FPDF class. And within this class we have override the methods Header() and Footer(). These function contains the definition of Header and Footer respectively. Create your Header and Footer any way you want by overridding these two methods and whenever a new page is created, before that Footer is called automatically and after that the Header is called.

Next it's time to work with the body. We have divided the body into two parts. First one is Chapter title and second one is Chapter body. These two methods defineds how our Chapter Title and Chapter Body should look like. The method ChapterBody() gets the text from reading a txt file.

The last methods is PrintChapter(). This methods only adds a new page and calls the above two methods - ChapterTitle() and ChapterBody(). So this function actually creates a new page and insert the content within it.

Example 2 : Tables


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

	class PDF extends FPDF
	{
		// Load data
		function LoadData($file)
		{
			// Read file lines
			$lines = file($file);
			$data = array();
			foreach($lines as $line)
				$data[] = explode(';',trim($line));
			return $data;
		}

		// Simple table
		function BasicTable($header, $data)
		{
			// Header
			foreach($header as $col)
				$this->Cell(40,7,$col,1);
			$this->Ln();
			// Data
			foreach($data as $row)
			{
				foreach($row as $col)
					$this->Cell(40,6,$col,1);
				$this->Ln();
			}
		}

		// Better table
		function ImprovedTable($header, $data)
		{
			// Column widths
			$w = array(40, 35, 40, 45);
			// Header
			for($i=0;$i<count($header);$i++)
				$this->Cell($w[$i],7,$header[$i],1,0,'C');
			$this->Ln();
			// Data
			foreach($data as $row)
			{
				$this->Cell($w[0],6,$row[0],'LR');
				$this->Cell($w[1],6,$row[1],'LR');
				$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R');
				$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R');
				$this->Ln();
			}
			// Closing line
			$this->Cell(array_sum($w),0,'','T');
		}

		// Colored table
		function FancyTable($header, $data)
		{
			// Colors, line width and bold font
			$this->SetFillColor(255,0,0);
			$this->SetTextColor(255);
			$this->SetDrawColor(128,0,0);
			$this->SetLineWidth(.3);
			$this->SetFont('','B');
			// Header
			$w = array(40, 35, 40, 45);
			for($i=0;$iCell($w[$i],7,$header[$i],1,0,'C',true);
			$this->Ln();
			// Color and font restoration
			$this->SetFillColor(224,235,255);
			$this->SetTextColor(0);
			$this->SetFont('');
			// Data
			$fill = false;
			foreach($data as $row)
			{
				$this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
				$this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
				$this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
				$this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);
				$this->Ln();
				$fill = !$fill;
			}
			// Closing line
			$this->Cell(array_sum($w),0,'','T');
		}
	}

	$pdf = new PDF();
	// Column headings
	$header = array('Country', 'Capital', 'Area (sq km)', 'Pop. (thousands)');
	// Data loading
	$data = $pdf->LoadData('countries.txt');
	$pdf->SetFont('Arial','',14);
	$pdf->AddPage();
	$pdf->BasicTable($header,$data);
	$pdf->AddPage();
	$pdf->ImprovedTable($header,$data);
	$pdf->AddPage();
	$pdf->FancyTable($header,$data);
	$pdf->Output();
	?>

See Demo

In the above example we have three tables. A table being just a collection of cells, it's natural to build one from them. The first example is achieved in the most basic way possible: simple framed cells, all of the same size and left aligned. The result is rudimentary but very quick to obtain.

The second table brings some improvements: each column has its own width, headings are centered, and numbers right aligned. Moreover, horizontal lines have been removed. This is done by means of the border parameter of the Cell() method, which specifies which sides of the cell must be drawn. Here we want the left (L) and right (R) ones. It remains the problem of the horizontal line to finish the table. There are two possibilities: either check for the last line in the loop, in which case we use LRB for the border parameter; or, as done here, add the line once the loop is over.

The third table is similar to the second one but uses colors. Fill, text and line colors are simply specified. Alternate coloring for rows is obtained by using alternatively transparent and filled cells.