How to get a page count in an mPDF document?

醉酒当歌 提交于 2019-12-03 10:54:32

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!

You can use {nbpg}, like

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

The answer can be foound in the original documentation:
http://mpdf1.com/manual/index.php?tid=314
replacement aliases {nb} and {nbpg} for total number
and {PAGENO} for current page number

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:

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!