Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add page break after match of certain class on div with mpdf

Tags:

php

mpdf

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 :)

like image 846
user2031297 Avatar asked Jan 21 '14 00:01

user2031297


2 Answers

I use this code before closing the foreach tag: $html .= "&lt;pagebreak /&gt;";

foreach($matches as $value) {
   $html .= "<pagebreak />";
 }
like image 200
jpx3m Avatar answered Sep 28 '22 04:09

jpx3m


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);
like image 45
mozgras Avatar answered Sep 28 '22 06:09

mozgras