문제

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

도움이 되었습니까?

해결책 4

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:

다른 팁

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>

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

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top