In TCPDF, how to get current page number as integer?
The result of $this->getAliasNumPage() is NOT a number . It's... {:pnp:} You can check this:
$text = "My Footer - Página ".$this->getAliasNumPage().'/'.$this->getAliasNbPages();
$this->Cell(0, 10, $text, 0, false, 'C', 0, '', 0, false, 'T', 'M');
And you will see the number.
But if you perform
echo $text;
you'll see the string doesn't have the number but only {:pnp:}
This came from the fact at this time (so when it executes the Footer function), the PDF is not yet created. So TCPDF can't know the number of pages. So it can't give you this value.
What can you do? in order to get a different footer eg on some pages, you must count the page number. So, declare a global $var at beginning of the Footer() function. Before the Footer() function, set $var to 0 and then, inside Footer() just $var++.
For the last page, just use this:
class mypdf extends tcpdf
{
// Declare for use in all function
protected $last_page = false;
// Will be called at the end
public function Close()
{
$this->last_page = true;
parent::Close();
}
public function Footer()
{
if ($this->last_page)
{
// Here you can display the footer for the last page
}
}
}
Edit: I forgot one option. If you want to know the full number of pages before the display or (eg) if you want to know the size of a PDF bloc to see if it fits a page and so on, you can use the rollbackTransaction(); First, you perform all operations (AddPages, MultiCell, and so on) which will give you the capability of the reading size of the result and all other values. Then you perform a $pdf = $pdf -> rollbackTransaction(); which will "undo" everything and then you run all your functions again, using this time all the value you get from the "first run".
The correct function is PageNo()
http://www.tcpdf.org/doc/code/classTCPDF.html#a9ad828b184f08828f570a7e52316ba79
TCPDF::PageNo()
Returns the current page number.
Returns
int page number
Edit.
OK, now (I think) I understand what you want to do, you add only 1 page and use auto page breaks, you also don't want to automatically number the pages in the footer. In that case you should use getAliasNumPage() and getAliasNbPages(). Define the following variable (edit the text as you will):
$PgNo= "This is the page " . $pdf->getAliasNumPage() . " of " . $pdf->getAliasNbPages();
Put it anywhere in the php document (it is important to put it after defining the fonts) and then just use the variable $PgNo (or however you will call it) wherever you need. You have to define it only once and it will later on get the values automatically depending on which page in the pdf document it is located.
both PageNo()
and getPage()
will return the current page number.
$this->PageNo();
$this->getPage();