Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tcpdf : prevent page break within a block

Tags:

tcpdf

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?

like image 486
s427 Avatar asked Jan 17 '12 10:01

s427


1 Answers

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

like image 99
michel Avatar answered Sep 18 '22 14:09

michel