How to delete blank page in mPdf

13k views Asked by At

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.

3

There are 3 answers

1
sijo vijayan On

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();
0
biko On

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;
0
Hugo Acosta On

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!