Reputation: 559
Does anybody knows how to get the number of generated pages if a PDF document using mPDF library?
Upvotes: 26
Views: 41093
Reputation: 5310
UPDATE
Please note, this answer refers to mdf library v4, which was a current version at the time of writing.
Minimum Working Example by @aiao
<?php
$pagenumber= '<!--mpdf Page {PAGENO} of {nbpg}mpdf--> $mpdf->WriteHTML($pagenumber);
$mpdf->Output();
?>
<?php
$pagenumber= '<!--mpdf Page {PAGENO} of {nbpg}mpdf--> $mpdf->WriteHTML($pagenumber);
$mpdf->Output();
?>
Upvotes: 5
Reputation: 429
If you are trying to return the number of pages so you can save this to a database or some other operation outside of mpdf it's easy to pull it this way.
After you write your content:
$mpdf->WriteHTML($html);
$page_count = $mpdf -> page;
$mpdf->Output();
Upvotes: 10
Reputation: 75
Watch for the line:
preg_replace('/\{DATE\s+(.*?)\}/e',"date('\\1')",$hd);
in mpdf.php function Footer()
It may cause your "{PAGENO} / {nb}" to not be displayed.
Just comment it out or use strpos('{DATE' > -1)
to check if it is available.
Also you may need to add:
$mpdf->ignore_invalid_utf8 = true;
and also if you don't want footer line:
$mpdf->defaultfooterline = false;
After these changes the pagination worked for me at last.
Upvotes: 0
Reputation: 548
You can use {nbpg}, like
<div align="center"><b>{PAGENO} / {nbpg}</b></div>
Upvotes: 21
Reputation: 351
I was looking for the same functionallity while using EYiiPdf (a wrapper for mPDF on Yii) and the following worked like a charm:
$mPDF->setFooter('{PAGENO} / {nb}');
I checked mPDF's source and found this at mpdf.php:1656 (version 5.4):
function AliasNbPages($alias='{nb}') {
//Define an alias for total number of pages
$this->aliasNbPg=$alias;
}
Hope it helps!
Upvotes: 35
Reputation: 559
add this to a main mPDF class:
function getPageCount() {
return count($this->pages);
}
then add a html-parser such string:
$html = str_replace('{PAGECNT}', $this->getPageCount(), $html);
after these actions you can insert {PAGECNT} directly in your parsed HTML to get the result. This is useful is you need to indicate a page:
Upvotes: 8