13

I am creating PDF reports using FPDF. Now how do I generate page numbers on each page of a report at the bottom of the page. Below is the sample code for generating a 2 page PDF.

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

        $pdf = new FPDF();
        $pdf->AliasNbPages();
        $pdf->AddPage();
        $pdf->SetFont('Arial','',16);

        $start_x=$pdf->GetX(); 
        $current_y = $pdf->GetY();
        $current_x = $pdf->GetX();

        $cell_width = 25; $cell_height=14; 
        $j = 20;  // This value will be coming from Database so we dont know how many pages the report is going to be 
        for ($i = 0; $i<$j ; $i++){
            $pdf->MultiCell($cell_width,$cell_height,'Hello1',1);
            $current_x+=$cell_width;
            $pdf->Ln();
        }

        $pdf->Output();


        ?>

Note : The $j value will be coming from the database so we don't know how many pages is the report going to be.

Jay
  • 477
  • 3
  • 7
  • 20
  • May a lil late but with this code you will produce only 1 page. To add more pages you have to add more `$pdf->AddPage()` may into your `for`-loop. – Dwza Oct 06 '14 at 13:07

2 Answers2

17

To add an A4 page, with portrait orientation, do:

$pdf->AddPage("P","A4");

Create a new class which extends the FPDF class, and override the pre-defined Footer method.

Example:

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');
    }
}
Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
Vipin Kumar Soni
  • 826
  • 10
  • 18
  • How to call this Footer method from my code ? Can you please tell me. – Jay May 20 '14 at 09:26
  • For the above code I have added $pdf = new PDF(); and it worked for me. thanks.!! – Jay May 20 '14 at 10:25
  • 1
    Header and Footer functions don't need to be called separately. If you extend FPDF and then instantiate, those are called automatically. – Nasir Ahmed May 30 '16 at 02:47
17

According to my comment you can place

$pdf->PageNo();

on your page where ever you like. Also you can add a placeholder to this

$pdf->AliasNbPages(); 

What would look like

$pdf->AliasNbPages('{totalPages}');

By default it's {nb}. It's not necessary to add a placeholder

Than you could add the pagesum like

$pdf->Cell(0, 5, "Page " . $pdf->PageNo() . "/{totalPages}", 0, 1);

or without your own placeholder

$pdf->Cell(0, 5, "Page " . $pdf->PageNo() . "/{nb}", 0, 1);

this would produce e.g.

Page 1/10

in case there were 10 pages :)

But beware

Using the placeholder will mess up the width of the cell. So if you have e.g. 180 page-width than 90 isn't the mid anymore (In the line where you use the placeholder). You will see if you try :)

Dwza
  • 6,494
  • 6
  • 41
  • 73
  • 1
    To not destroy the layout too much I used "#" instead of "{totalPages}" as placeholder. Works great! – Bernhard May 07 '18 at 09:05