7
votes

I'm using TCPDF to print some tables of data: one big table (although usually not longer than a page) followed by a second, smaller one.
In some cases, the two tables together are longer than one page, so TCPDF inserts a page break in the middle of the second table. My clients want to avoid that behavior: they would rather have the second table completely on a new page, ie insert the page break before the table, if both table cannot fit on a single page.

Of course if both tables fit on one page, no page break should be used.

So does anybody know if there is a way to instruct TCPDF not to insert a page break within a given table?

3

3 Answers

17
votes

Start a transaction, insert the table, check if you are in a new page, if yes, roll back and add a page before insering your table.

VERY IMPORTANT: don't forget the TRUE calling rollback:

$this->startTransaction(); 
$start_page = $this->getPage();                       
$this->writeHTMLCell( 0, 0, '', '', $html, 0, 1, false, true, 'C'  );
$end_page = $this->getPage();
if  ($end_page != $start_page) {
    $this->rollbackTransaction(true); // don't forget the true
    $this->AddPage();
    $this->writeHTMLCell( 0, 0, '', '', $html, 0, 1, false, true, 'C'  );
}else{
    $this->commitTransaction();     
} 

Hope it helps michel

8
votes

according to docs, there is a option to add nobr="true" to table tag as attribute.

 $tbl = <<<EOD
<table border="1" cellpadding="2" cellspacing="2" nobr="true">
 <tr>
  <th colspan="3" align="center">NON-BREAKING TABLE</th>
 </tr>
 <tr>
  <td>1-1</td>
  <td>1-2</td>
  <td>1-3</td>
 </tr>
</table>
EOD;

$pdf->writeHTML($tbl, true, false, false, false, '');

http://www.tcpdf.org/examples/example_048.phps

-1
votes

Calculate a height of the second table in advance. Use a checkPageBreak method to add a page break if needed.