9

I am converting an html file to PDF by using mPdf, but the returned PDF file includes blank pages. I want to delete those blank pages.

A.L
  • 10,259
  • 10
  • 67
  • 98

3 Answers3

8

Had the same problem and realized that my CSS was forcing these page breaks. Make sure you don't have this in your CSS:

page-break-after: always;
biko
  • 510
  • 6
  • 15
0

It may be for many reasons:

1) Make sure the elements doesn't have unnecessary margins nor Paddings

2) Configure correctly the page properties (specially margins) :

$page_orientation = 0;
$page_size = 'Letter';
$page_margins = array('LEFT(int)','RIGHT(int)','UP(int)','BOTTOM(int)');
$pdf_output = 'I';
$css_files = array(
    'path/file.css',
    'path/file_2.css',
);

$orientationPage = array('','-L');

/* ===== [ MPDF ] =====*/
$mpdf=new mPDF('utf-8', $page_size.$orientationPage[$page_orientation],'','',$page_margins[0],$page_margins[1],$page_margins[2],$page_margins[3]);

// Loading CSS

    for($i = 0; $i < count($css_files); $i++){
        $stylesheet = file_get_contents('../../'.$css_files[$i]);
        $mpdf->WriteHTML($stylesheet,1); // 1 para indicar que es CSS

    }

// Set header & Footer (This are variables with html code)
    $mpdf->SetHTMLHeader($header);
    $mpdf->SetHTMLFooter($footer);
    $mpdf->SetDisplayMode('fullpage');
    $mpdf->SetTitle($title);
    $mpdf->WriteHTML($html); // <-- insert HTML

// Create PDF
    $mpdf->Output($titulo.'.pdf',$pdf_output);

3) Make sure you haven't unnecessary "Page Breaks" on the HTML

<pagebreak type="NEXT-ODD" resetpagenum="1" pagenumstyle="i" suppress="off" />

I hope it can help you!

0

I had the same problem and i fixed it by removing the AddPage property from my code

I changed the following code

    // Code with bug
    $mpdf = new mPDF('utf-8', array(380,500));
    $mpdf->WriteHTML($htmlContent);
    $mpdf->AddPage('P'); // It will add extra page - I that i removed this line
    $mpdf->Output();

Into this

    // code after resolving the bug
    $mpdf = new mPDF('utf-8', array(380,500));
    $mpdf->WriteHTML($htmlContent);
    $mpdf->Output();
sijo vijayan
  • 1,590
  • 1
  • 21
  • 34
  • Not sure why this is downvoted. It's easy to leave an extra AddPage dangling on the end of a loop. This was my problem as well. If you're using CSS formatting for page breaks, addPage may be redundant. – 111 May 25 '20 at 06:08