9

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

user2031297
  • 161
  • 1
  • 2
  • 14

3 Answers3

28

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

foreach($matches as $value) {
   $html .= "<pagebreak />";
 }
GAMITG
  • 3,810
  • 7
  • 32
  • 51
jpx3m
  • 281
  • 3
  • 4
9

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);
GAMITG
  • 3,810
  • 7
  • 32
  • 51
mozgras
  • 3,215
  • 3
  • 21
  • 17
0
        $mpdf = new \Mpdf\Mpdf();
        $mpdf->use_kwt = true;
        $html = $this->load->view('client_tid_pdf', $data,true);
        $mpdf->autoPageBreak = true;
        $mpdf->setAutoTopMargin='stretch';
        $mpdf->setAutoBottomMargin = 'stretch';
        $mpdf->WriteHTML($html);
        $mpdf->Output(); // opens in browser
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 19 '22 at 07:43