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.
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
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
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 :)
I use this code before closing the foreach tag: $html .= "<pagebreak />";
foreach($matches as $value) {
$html .= "<pagebreak />";
}
I've been successful without using AddPage and instead using css to add the page break, with page-break-after:always
and for the last div, page-break-after:avoid
. Maybe something like this would work:
$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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With