Dion
Dion

Reputation: 3335

TCPDF prevent div from breaking into a new page

I've tried pretty much everything I found and could possibly think of to prevent a div (with some text and a table inside) to not break into the next page, but haven't found anything working.

The basic structure of the div looks like this:

<div nobr="true" style="page-break-inside:avoid;">
   <b>...</b><br /><a>...</a>...
   <table nobr="true" style="page-break-inside:avoid;">
        <tr>...</tr>
        ...
   </table>
</div>

The div still breaks anywhere (e.g. inside the table, or after the first child). Is there any workaround to fix this? (I've also tried to write every div seperately and check if a new page was created and rollback in case)

Upvotes: 1

Views: 1463

Answers (1)

Dion
Dion

Reputation: 3335

Finally managed to fix this! In case someone has the same issue, here's my solution:

  • Create a copy of the pdf class
  • Check which page you're at before writing the HTML code
  • Write HTML code
  • Check page you are after writing the HTML code, if it is not the same as before you have to create a new page (in the original pdf class) before actually writing

    $_pdf = clone $pdf;
    
    $startpage = $_pdf->PageNo();
    
    $_pdf->writeHTMLCell( 0, 0, '', '', $html, 0, 1, false, true, 'C'  );
    
    $endpage = $_pdf->PageNo();
    
    if($startpage != $endpage) {
        $pdf->addPage();
    }
    
    $pdf->writeHTML($css . $htmlP, true, false, true, false, '');
    

Upvotes: 2

Related Questions