php 在 div 上的某些类与 mpdf 匹配后添加分页符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21246879/
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
Add page break after match of certain class on div with mpdf
提问by user2031297
I am converting a report (in html)to pdf using mPDF library, but is putting all pages into one. I want to add a page break after a certain but I haven't been successful.
我正在使用 mPDF 库将报告(以 html 格式)转换为 pdf,但将所有页面合二为一。我想在某个时间后添加一个分页符,但我没有成功。
html
html
<div class='text my_form' style='margin:0px 0 0 0; padding:0px 0 0 0; border-style:dotted; border-color:white;'>
I'm passing this to the php as $html
我将它作为 $html 传递给 php
PHP
PHP
require_once ("./libraries/MPDF57/mpdf.php");
$mpdf=new mPDF("", "Letter", "", "", 10, 10);
I want to add a page break after it finds the div shown above
我想在找到上面显示的 div 后添加一个分页符
preg_match_all("/((\<div\s)(class='text\smy_form'[\s\w\s]*border-color:white)(.+?)(\>)/", $html, $matches, PREG_SET_ORDER);
foreach($matches as $value) {
$html= $mpdf->AddPage();
}
$mpdf->WriteHTML($html);
$filename = "documentx.pdf";
$mpdf->Output($fileName, 'F');
However, is not working, please help me go through the right track :)
但是,不起作用,请帮助我走上正轨:)
回答by jpx3m
I use this code before closing the foreach tag: $html .= "<pagebreak />";
我在关闭 foreach 标记之前使用此代码: $html .= "<pagebreak />";
foreach($matches as $value) {
$html .= "<pagebreak />";
}
回答by mozgras
I've been successful without using AddPage and instead using css to add the page break, with page-break-after:alwaysand for the last div, page-break-after:avoid. Maybe something like this would work:
我没有使用 AddPage 而是使用 css 添加分页符,page-break-after:always以及最后一个 div,page-break-after:avoid. 也许这样的事情会起作用:
$len = count($matches);
$i = 1;
foreach($matches as $value) {
if ($i < $len) {
$html .= "<div style='page-break-after:always'>" . $html . "</div>";
} else {
$html .= "<div style='page-break-after:avoid'>" . $html . "</div>";
}
$i++;
}
$mpdf->WriteHTML($html);

