3

I have a problem. How to add a new page from HTML to PDF?

Immediately I will say that I do not know why, but solutions such as:

<pagebreak />

or

h1 {page-break-before: always}

not working.

My PHP Code:

$pdf = new Pdf([
    'mode' => Pdf::MODE_UTF8,
    'format' => Pdf::FORMAT_A4,
    'orientation' => Pdf::ORIENT_PORTRAIT,
    'destination' => Pdf::DEST_FILE,
    'filename' => $path,
    'content' => $content,
    'cssInline' => '.font_next{font-family:DoodlePen}table{border-collapse:collapse;width:100%}td{border:1px solid #000}',
]);
return $pdf->render();

Does anyone have this experience and can help you?

2 Answers2

8

From the docs, looks like you can either use HTML like:

<pagebreak />
or
<tocpagebreak />

Or PHP:

$mpdf->AddPage();
$mpdf->TOCpagebreak();

But you already said you tried the html and din't work. Maybe you have a parent element with float?

Community
  • 1
  • 1
Clyff
  • 4,046
  • 2
  • 17
  • 32
3

OK, it works.

I use this method: https://davidwalsh.name/css-page-breaks

In HTML:

<div className="page-break"></div>

In PHP:

$pdf = new Pdf([
    'mode' => Pdf::MODE_UTF8,
    'format' => Pdf::FORMAT_A4,
    'orientation' => Pdf::ORIENT_PORTRAIT,
    'destination' => Pdf::DEST_FILE,
    'filename' => $path,
    'content' => $content,
    'cssInline' => '
        @media all{
            .font_next{font-family:DoodlePen}table{border-collapse:collapse;width:100%}td{border:1px solid #000}.page-break {display: none;}
        }
        @media print{
            .page-break{display: block;page-break-before: always;}
        }
    ',
]);
return $pdf->render();