Sunday, 2 December 2012

fpdf tutorials with examples


FPDF is free and uses pure PHP code. Full form of FPDF is free pdf. easily available and uses PDFlib to generate pdf files.

If you are using XAMPP with latest version you will easily find it inside xampp/php/pear/ folder. Or you can download : download fpdf

With FPDF you have: 

  • Choice of measure unit, page format and margins
  • Page header and footer management
  • Automatic page break
  • Automatic line break and text justification
  • Image support (JPEG, PNG and GIF)
  • Colors
  • Links
  • TrueType, Type1 and encoding support
  • Page compression
Before starting you can see fpdf function manual here : http://mukundtopiwala.blogspot.in/2012/12/fpdf-manual-and-cheatsheet_2.html


Simple FPDF example

<?php
// depends on where you have placed fpdf.php
// if fpdf.php is outside fpdf directory change it to fpdf.php
require('fpdf/fpdf.php');

$pdf = new FPDF();  // $pdf = new FPDF('P','mm','A4');Default val
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Mukund Topiwala');
$pdf->Output();
// default output set to I means to browser.
?>


Example with footer, header


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

class PDF extends FPDF
{
// Page header
function Header()
{
    // Logo
    $this->Image('logo.jpg',10,10,30);
    // Arial bold 15
    $this->SetFont('Arial','B',15);
    // Title
    $this->Cell(0,12,'Mukund Topiwala',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,'R');
}
}
// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
for($i=1;$i<=5;$i++)
    $pdf->Cell(0,10,'Printing line number '.$i,0,1);
$pdf->Output();
?>
output : 






Example Use of Color, File content.


<?php
require('fpdf/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(248,123,39);
    $this->SetFillColor(26,69,114);
    $this->SetTextColor(255,255,255);
    // 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 0 is set for whole line.
// If you wants column output then set width for it
    $this->MultiCell(0,5,$txt);
}

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

$pdf = new PDF();
$title = 'Mukund Topiwala Document';
$pdf->SetTitle($title);
$pdf->SetAuthor('Jules Verne');
$pdf->PrintChapter(1,'Mukund Part 1','mukund.txt');
$pdf->PrintChapter(2,'Mukund Part 2','mukund.txt');
$pdf->Output();
?>

output :



No comments:

Post a Comment