php 如何获取 mPDF 文档中的页数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10280595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a page count in an mPDF document?
提问by Nikita Gopkalo
Does anybody knows how to get the number of generated pages if a PDF document using mPDF library?
如果使用 mPDF 库的 PDF 文档,有人知道如何获取生成的页数吗?
采纳答案by Nikita Gopkalo
add this to a main mPDF class:
将此添加到主 mPDF 类:
function getPageCount() {
return count($this->pages);
}
then add a html-parser such string:
然后添加一个 html-parser 这样的字符串:
$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:
在这些操作之后,您可以直接在解析的 HTML 中插入 {PAGECNT} 以获得结果。如果您需要指示页面,这很有用:
回答by Tomi Drpic
I was looking for the same functionallity while using EYiiPdf (a wrapper for mPDF on Yii) and the following worked like a charm:
在使用 EYiiPdf(Yii 上 mPDF 的包装器)时,我一直在寻找相同的功能,以下内容就像一个魅力:
$mPDF->setFooter('{PAGENO} / {nb}');
I checked mPDF's source and found this at mpdf.php:1656 (version 5.4):
我检查了 mPDF 的源代码,并在 mpdf.php:1656(5.4 版)中找到了这个:
function AliasNbPages($alias='{nb}') {
//Define an alias for total number of pages
$this->aliasNbPg=$alias;
}
Hope it helps!
希望能帮助到你!
回答by Zvonimir Buri?
You can use {nbpg}, like
您可以使用{nbpg},例如
<div align="center"><b>{PAGENO} / {nbpg}</b></div>
回答by John F
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.
如果您试图返回页数,以便将其保存到数据库或 mpdf 之外的其他一些操作中,则很容易以这种方式提取它。
After you write your content:
写完内容后:
$mpdf->WriteHTML($html);
$page_count = $mpdf -> page;
$mpdf->Output();
回答by sr9yar
- replacement aliases {nb}and {nbpg}for total number
- {PAGENO}for current page number
- 总数的替换别名{nb}和{nbpg}
- {PAGENO}当前页码
UPDATE
更新
Please note, this answer refers to mdf library v4, which was a current version at the time of writing.
请注意,此答案指的是 mdf 库 v4,这是撰写本文时的当前版本。
Minimum Working Example by @aiao
@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();
?>
回答by Goce Dimkovski
Watch for the line:
注意线路:
preg_replace('/\{DATE\s+(.*?)\}/e',"date('\\1')",$hd);
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.php 函数 Footer() 可能会导致你的“{PAGENO} / {nb}”不显示。只需将其注释掉或用于strpos('{DATE' > -1)检查它是否可用。您还可能需要添加:
$mpdf->ignore_invalid_utf8 = true;
$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.
在这些更改之后,分页终于对我有用了。

