Search code examples
phptcpdf

tcpdf if cell bigger than free space jump to new page


Hi i need a way to make content create in new page if it bigger than page free area enter image description here


Solution

  • The CSS property page-break-inside: avoid; on an element will start it on the next page if it would otherwise be split by a page break. I have confirmed this property works with TCPDF.

    Some example code:

    <?php
    
    require_once('./tcpdf-master/tcpdf.php');
    
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $pdf->AddPage();
    
    $html = <<<EOD
    <p style="page-break-inside: avoid;">This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. </p>
    <p style="page-break-inside: avoid;">This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. </p>
    <p style="page-break-inside: avoid;">This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. </p>
    <p style="page-break-inside: avoid;">This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. </p>
    <p style="page-break-inside: avoid;">This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. This is a long paragraph to test out the "page-break-inside: avoid" property. </p>
    EOD;
    
    $pdf->writeHTMLCell( 0, 0, '', '', $html, 0, 1, false, true, 'C' );
    $pdf->Output('page-break.pdf', 'I');
    

    This will output a PDF like this:

    PDF with page-break-inside: avoid on the paragraphs

    Whereas without the style="page-break-inside: avoid;" parts, it will output this:

    PDF without page-break-inside: avoid