FPDF - Header and Footer


We can make header and footer using Header() and Footer() methods. They already present in FPDF class but have no definition, therefore we have to extend the class and override them.

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

  class PDF extends FPDF
  {
    //Page Header
    function Header(){
      $this->SetFont('Arial','B',15);
      
      $this->Cell(0,15,'Sample Header',0,1,'C');
      
      // Line Break
      $this->Ln();
    }
    
    //Page Header
    function Footer(){
      
      // Position at 15 mm from bottom
      $this->SetY(-15);
      $this->SetFont('Arial','I',13);
      
      // set page number and total number of pages
      $this->Cell(0,15,'Page No : '.$this->PageNo()." / {nb}",0,1,'C');
    }
  }
  
  //Create object of inherited class
  $pdf=new PDF();
  
  //Call this function for special value {nb} (total number of pages) 
  $pdf->AliasNbPages();
  
  $pdf->AddPage();
  
  $pdf->SetFont('Arial','',15);
  
  for($i=0;$i<50;$i++)
  {
    $pdf->Cell(0,15,"Line ".($i+1),0,1);
  }
  
  $pdf->Output();
  
?>
Output