fpdf add page number

Here an easy way to add page number at the end of a fpdf document

In your fpdf class, before calling addpage(), call the AliasNbPages() fonction

$this->AliasNbPages();

then create a footer() fonction to override the default empty footer

function Footer()
{
        $this->SetY(-10);
        $this->SetFont('Arial', '', 8);
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}', 0, 0, 'C');
        $this->SetY(-10);
        $this->Cell(0,10,"date d'impression: ".date('d/m/Y'), 0, 0, 'R');
}

The complete class : file pdfListeDemandes.class.php

<?php

class pdfListeDemandes extends FPDF
{
   function init()
   {
                $this->AliasNbPages();
                ...
   }
   function writeListe()
   {
         $this->init();
         ...
   }

   function Footer()
   {
        $this->SetY(-10);
        $this->SetFont('Arial', '', 8);
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}', 0, 0, 'C');
        $this->SetY(-10);
        $this->Cell(0,10,"date d'impression: ".date('d/m/Y'), 0, 0, 'R');
   }

}

And the code to call your class

$pdf = new pdfListeDemandes();
$pdf->writeListe();
$pdf->output('Liste_demande.pdf', 'D');
exit;

Leave a Reply