Tcpdf Split Html Table On Multiple Pages
Solution 1:
I think your solution is a good one. Only other method I can think of is to use fixed width multicells which can be quite a pain but gives you static column widths.
I noticed a few problems however:
- Your transaction wraps the entire document, a rollback should reset the document to nothing.
- Your warnings are likely caused by the fact that you are passing partial html code which is not render-able to writeHTMLCell. (You pass just <table> or </table> which when alone, cannot be rendered)
- Your columns wouldn't line up if you wrapped each row in table tags. The best bet is to wrap the rows on that page in a table block.
Try these changes and let me know how it goes
$pdf->setAutoPageBreak(false);
//$pdf->startTransaction(); // Moved$html = new simple_html_dom();
$html->load($data);
$single = $html->find('#Container', 0);
if($single){
$rows = $single->getElementsByTagName('tr');
$rows = $rows[0]->getElementsByTagName('tr');
if($rows) {
$pdf->startTransaction(); // Start transaction only because we may need it// Header for html, this starts the html and can optionally insert the header row as the first row on every new page.$html_header = '<tr><td>Name</td><td>Age</td></tr>';
$html_buffer = '<table>'.$html_header;
for($i=1;$i<(count($rows)-1);$i++){
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html_buffer.$rows[$i]->outertext.'</table>', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
if ($pdf->getY() < ($pdf->getPageHeight() - 30)) { // Note the less-than operator// We might be able to add some more text, so undo that$pdf->rollbackTransaction(true);
// And store the html$html_buffer .= $rows[$i]->outertext;
}else{
// We exceeded our limit$pdf->rollbackTransaction(true);
// Write last known good table$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html_buffer.'</table>', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
// Add a new page$pdf->AddPage();
// End this transaction$pdf->commitTransaction();
// Start a new transaction$pdf->startTransaction();
// Reset html buffer$html_buffer = '<table>'.$html_header;
// Add line we couldn't fit on last page to html buffer$html_buffer .= $rows[$i]->outertext;
}
}
// There is still information in our buffer and it fits on a single page$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html_buffer.'</table>', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
// Final commit$pdf->commitTransaction();
}
}
$pdf->setAutoPageBreak(true, 30);
This wraps everything on the page in a table block so the columns will line up on that page. (They may still be different from page to page however) Also, I gave you the ability to add your own header to each new page like you wanted. Just change $html_header to your own header row code.
You are right when you said TCPDF can be confusing but it is also extremely powerful and once you get the hang of it you can make some very nice documents.
Solution 2:
Go to TCPDF and choose Example 48, you will see an example with <thead>
element.
Post a Comment for "Tcpdf Split Html Table On Multiple Pages"