Reputation: 873
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
Upvotes: 0
Views: 15985
Reputation: 69
Your Main class:
$o_pdf->AddPage(); //need to add first and set isFinished true
$o_pdf->isFinished = true;
And your FPDF extended class:
if($this->isFinished) {
//your footer on last page
}
Hope this will be useful for you!!!
Upvotes: 0
Reputation: 58
I'm late to the party too - LOL Paul is on the right track. I amended his solution to work with a while loop.
$pdf = new PDF();
$pdf->isFinished = false;
$pdf->AddPage();
// Add your page contents here
while($row1 = $rs1->fetch_assoc()){
// output...
$pdf->isFinished = false;
}
$pdf->isFinished = true;
Now In the Footer...
function Footer()
{
$this->SetY(-15);
if($this->isFinished){
$this->Cell(195,10, "Signature:_______________________________", 0, 0,'R');
}
}
I hope this helps someone.
Upvotes: 2
Reputation: 19
For people stumbling upon this question many years later: Danny's answer above can also be used to skip the footer (or header) on any arbitrary page. You just need to make sure that you set your skip-Boolean to true after adding the page, then add the content, and set it back to false in case you want a footer on the next pages.
$pdf = new PDF();
$pdf->isFinished = false;
// [...]
$pdf->AddPage();
$pdf->isFinished = true;
// Add your page contents here
$pdf->isFinished = false;
I posted a longer version here.
I would have liked to add this as a comment to Danny's answer. However, being new and low on reputation, I had to post my own. :/
Upvotes: 0
Reputation: 676
If you use MultiCell() and AutoPageBreak, the content you're writing may create an automatic page break.
In this case, setting a boolean flag before a MultiCell() call can create the last footer expected on the last two pages instead of the very last one only.
At the end of your code, when you don't have anything else to write in your document, do the following :
$this->InFooter = true;
$this->SetY(-15);
$this->Write(5, "My end footer text");
$this->InFooter = false;
InFooter boolean tells to FPDF that we are writing content in the Footer and FPDF will not automatically create a page break.
SetY() is needed otherwise your content will be written outside the document.
Upvotes: 1
Reputation: 2579
I know i'm a little late to the party here but when I ran into this same problem it was because I was not calling the AliasNbPages()
function. The example they give doesn't seem to include this in their code !
I just call it right after I make a new pdf:
$pdf = new PDF();
$pdf->AliasNbPages();
Now my {nb} is replaced with the total number of pages !
Upvotes: 0
Reputation: 729
{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
Upvotes: 3