Does anybody knows how to get the number of generated pages if a PDF document using mPDF library?
-
stackoverflow.com/questions/1098156/… - may helps you– RikeshApr 23, 2012 at 12:40
-
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, like: "page 2 of 5".– Nikita GopkaloApr 23, 2012 at 13:23
6 Answers
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!
-
1Actually accepted answer dint work for me!! And your solution worked out :)– sravisJun 13, 2013 at 9:58
-
i'am creating pdfs using mpdf(inside Yii), pdfs are created from htmls, and i used setfooter to the footer stuff, but its getting displayed in last page only! how to get that in every page? $mPDF1->setFooter('{DATE j-m-Y}|{PAGENO}/{nb}','O|E');– arunMar 7, 2014 at 6:57
You can use {nbpg}, like
<div align="center"><b>{PAGENO} / {nbpg}</b></div>
-
3This is a very useful answer. If you are using
resetpagenum
,{nbpg}
shows the current page number counting from your last reset. If you want to show the total page count, you can use{nb}
. Jun 12, 2014 at 15:02
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();
-
1I've been looking for this for AGES, looking through the docs for hours and I've finally found the answer. Thank you so much! Aug 7, 2016 at 19:54
-
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:
-
This works for me but only shows on the last page, is it possible to show in the footer of every page?– Vince PJul 30, 2013 at 14:48
-
-
- replacement aliases {nb} and {nbpg} for total number
- {PAGENO} for current page number
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();
?>
-
1Please explain how that should be used. How do they return a page count to the application? Nov 7, 2019 at 11:08
-
Even though I can answer your question, the OP's question has nothing to do with returning the number of pages into application. Those are replacement aliases, they are put into your html template and parsed by the mdf class instance. This is an answer for library v4, if I remember correctly, please see the year. The documentation for this library was missing (maybe still is), making it difficult to find the right placeholder names right away.– sr9yarNov 7, 2019 at 13:07
-
1Please include a MWE (Minimum Working Example). You don't have to test it, although it would be nice.... instantiate an object, use the paramter ..etc <?php $pagenumber= '<!--mpdf Page {PAGENO} of {nbpg}mpdf--> $mpdf->WriteHTML($pagenumber); $mpdf->Output();?>– aiaoNov 7, 2019 at 15:01
-
@aiao Actually, feel free to edit my answer. I wrote that answer because I had to spend some time myself finding those placeholders (In fact I didn't know they existed from the start ). IMHO, if the OP got to the point of asking this question, he knows pretty well how to init the library and create a basic pdf from html. The last version I worked with was 6, there's probably newer version out there now. I don't think we should spend so much time improving this answer.– sr9yarNov 7, 2019 at 15:13
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.