How to get a page count in an mPDF document?

蓝咒 提交于 2019-11-27 17:34:43

问题


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


回答1:


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!




回答2:


You can use {nbpg}, like

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



回答3:


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:




回答4:


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



回答5:


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



回答6:


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.



来源:https://stackoverflow.com/questions/10280595/how-to-get-a-page-count-in-an-mpdf-document

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