Nikita Gopkalo
Nikita Gopkalo

Reputation: 559

How to get a page count in an mPDF document?

Does anybody knows how to get the number of generated pages if a PDF document using mPDF library?

Upvotes: 26

Views: 41093

Answers (6)

sr9yar
sr9yar

Reputation: 5310

  • 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();
?>

Upvotes: 5

John F
John F

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

Goce Dimkovski
Goce Dimkovski

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

Zvonimir Burić
Zvonimir Burić

Reputation: 548

You can use {nbpg}, like

<div align="center"><b>{PAGENO} / {nbpg}</b></div>

Upvotes: 21

Tomi Drpic
Tomi Drpic

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

Nikita Gopkalo
Nikita Gopkalo

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

Related Questions