Does anybody knows how to get the number of generated pages if a PDF document using mPDF library?
-
http://stackoverflow.com/questions/1098156/number-of-pages-in-a-pdf-file - may helps you – Rikesh Apr 23 '12 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 Gopkalo Apr 23 '12 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!

- 321
- 3
- 3
-
1Actually accepted answer dint work for me!! And your solution worked out :) – sravis Jun 13 '13 at 09: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'); – arun Mar 07 '14 at 06:57
You can use {nbpg}, like
<div align="center"><b>{PAGENO} / {nbpg}</b></div>

- 528
- 3
- 11
-
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}`. – Mandy Schoep Jun 12 '14 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! – Aleksandar Bencun Aug 07 '16 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:

- 559
- 1
- 6
- 13
-
This works for me but only shows on the last page, is it possible to show in the footer of every page? – Vince P Jul 30 '13 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();
?>

- 4,850
- 5
- 53
- 59
-
1Please explain how that should be used. How do they return a page count to the application? – Nico Haase Nov 07 '19 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. – sr9yar Nov 07 '19 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 $mpdf->WriteHTML($pagenumber); $mpdf->Output();?> – aiao Nov 07 '19 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. – sr9yar Nov 07 '19 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.

- 65
- 1
- 7