Php – FPDF Footer on last page using If statement and {nb}

fpdfif statementpdf-generationPHP

I have seen quite a few questions on this but not definitive answer. Im using FPDF to dynamically create PDF with values form a DB, I only want a footer on the last page, seeing as its values are based on DB values the PDF could be 2,3,4 pages long. So i started messing around with the footter of FPDF like so

function Footer()
{
    $this->SetY(-15);
    $this->SetFont('Arial','I',8);
    //Page number
    $pagenumber = '{nb}';
    if($this->PageNo() == 2){
        $this->Cell(173,10, ' FOOTER TEST  -  '.$pagenumber, 0, 0);
    }
}

This retruns the value "FOOTER TEST – 2" on the second page and if i set the if to "<=2" will put the same on page one and two, GREAT! it recognises $pagenumber as 2 or if i had 3 pages as 3, so it can see that i have however many pages, however, when i change the code to:

function Footer()
{
    $this->SetY(-15);
    $this->SetFont('Arial','I',8);
    //Page number
    $pagenumber = '{nb}';
    if($this->PageNo() == $pagenumber){
        $this->Cell(173,10, ' FOOTER TEST  -  '.$pagenumber, 0, 0);
    }
}

Thinking that it output the last page once, adding that to the if statement will make it work, it doesn't display anything, like the if statement cant recognize the value for $pagenumber, is this because it is a string or int trying to compare to the opposite or something else?

Any help greatly appropriated.

Ian

Best Answer

{nb} is done as a late replacement after you have finished writing (as it doesn't know how many pages there are before that) whereas the footer is output whenever a page is complete, your best bet is to probably set a flag on the class to true when you have finished outputting everything and then check the flag in the footer

function Footer()
{
    $this->SetY(-15);
    $this->SetFont('Arial','I',8);
    //Page number
    if($this->isFinished){
        $this->Cell(173,10, ' FOOTER TEST  -  {nb}', 0, 0);
    }
}

and then in your PDF writing code

...
$pdf->isFinished = true;

Hope this helps

Related Topic