Search code examples
mpdf

mPDF creates an extra blank page when the content reaches a certain width. How can I get rid of the extra blank page?


I am have this problem with mpdf where if some text hits the very last line of the page, it will create an extra blank page.

I've tried a million things, but can't seem to figure this out. I want a sure fire way to make sure there are no blank pages in my pdfs.

So, I was wondering if there was a {PAGEEND} tag or some tag I put in my html to let mPDF that this is the end of the page and don't make a page after this page. I did not see any thing in the docs for this.

Here's what i have in my mpdf configuration:

$mpdf = new \Mpdf\Mpdf([
    'format' => 'A4',
    'margin_left' => 0,
    'margin_right' => 0,
    'margin_top' => 6,
    'margin_bottom' => 20,
    'margin_header' => 5,
    'margin_footer' => 5
]);

if ( isset( $_REQUEST['css_files'] ) ) {
    $css_files = $_REQUEST['css_files'];
    if ( is_array($css_files) ) {
        foreach ( $_REQUEST['css_files'] as $cssurl ) {
            $stylesheet = file_get_contents($cssurl);
            $mpdf->WriteHTML($stylesheet,1);
        }
    }
}

$mpdf->useSubstitutions = false; // optional - just as an example
$mpdf->simpleTables = false;
$mpdf->SetAuthor("My Website");
$mpdf->SetDisplayMode('fullpage');
$mpdf->setAutoBottomMargin = 'stretch';
$mpdf->SetHTMLFooter('<div width="90%" align="right">{PAGENO}</div>');
$mpdf->setBasePath($url);
$mpdf->WriteHTML( $html );
$mpdf->Output($title . ".pdf", 'D'); exit;

I have the margin-bottom set to 20mm so that the content doesn't conflict with my footer.

The css_files is an array received from my template. The css files are the website styles and a print stylesheet. I'm loading both of them and applying them to the page.

I've tried sprinkling page-break-after: avoid; all over my print.css, but i still get that pesky extra blank page.

I have also gone through and cleared out any extra bottom margins, but that did not seem to help.


Solution

  • After several hours of troubleshooting this turned out to be something completely stupid.

    What was happening is that my "print to pdf" button was being printed on page 4. I couldn't see it because it has white text on a white background in the pdf.

    It wasn't until I shared the generated pdf into Teams that we saw the text. We havea conditional that we setup to remove html from the page that we don't need in the pdf.

    Keep in mind, the form with the print to pdf button is wrapped in a div with hidden-print class on it and the button itself also has the hidden-print class on it. No idea why it was not picking up that style on this one element in the page. So removing it from the code was best option.