title: TCPDF split html table on multiple pages
body: <p>I use TCPDF for outputting an html table, this one in my example is not the only one that i would have to modify so i need some rules for my pages to be rendered.
  So far TCPDF seemed to me a powerfull library so i would appreciate not to try another one. </p>
  
  <p>My issue is that i need to add the table header to every new page the table will appear to, but i have also other info to write to this pdf, that must not have the table header so i cannot use the rewrite of header method in tcpdf.</p>
  
  <pre><code>&lt;table border="0" cellspacing="0" cellpadding="0" align="center" width="100%" &gt;&lt;tr&gt;&lt;td&gt;&lt;table border="0" cellspacing="0" cellpadding="3" align="left" width="100%" &gt;&lt;tr&gt;&lt;td class="TableHeader"  width="5%" style="padding: 5px; border-top: 1px solid #000000; border-left: 1px solid #000000; border-bottom: 1px solid #E4E9EC;" &gt;Nr.&lt;/td&gt;&lt;td class="TableHeader" width="40%" style=" padding: 5px; border-top: 1px solid #000000; border-bottom: 1px solid #E4E9EC;" &gt;Denumire&lt;/td&gt;&lt;td class="TableHeader" width="20%" align="left" style=" padding: 5px; border-top: 1px solid #000000; border-bottom: 1px solid #E4E9EC;" &gt;Cod&lt;/td&gt;&lt;td class="TableHeader" width="10%" align="center" style=" padding: 5px; border-top: 1px solid #000000; border-bottom: 1px solid #E4E9EC;" &gt;Cant.&lt;/td&gt;&lt;td class="TableHeaderLast" align="right" width="13%" style=" padding: 5px; border-top: 1px solid #000000; border-bottom: 1px solid #E4E9EC;" &gt;Pret unitar&lt;br /&gt;fara TVA (RON)&lt;/td&gt;&lt;td class="TableHeaderLast" align="right" width="13%" style=" padding: 5px; border-bottom: 1px solid #E4E9EC; border-top: 1px solid #000000; border-right: 1px solid #000000;" &gt;Pret total&lt;br /&gt;fara TVA (RON)&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;table border="0" cellspacing="0" cellpadding="0" align="center" width="100%" id="Container"&gt;&lt;tr&gt;&lt;td&gt;&lt;table border="0" cellspacing="0" cellpadding="3" align="left" width="100%" &gt;&lt;tr&gt;&lt;td class="TableHeader"  width="5%" style="padding: 5px; border-left: 1px solid #000000;" &gt;&amp;nbsp;&lt;/td&gt;&lt;td class="TableHeader" width="40%" style=" padding: 5px;" &gt;&amp;nbsp;&lt;/td&gt;&lt;td class="TableHeader" width="20%" style=" padding: 5px;" &gt;&amp;nbsp;&lt;/td&gt;&lt;td class="TableHeader" width="10%" style=" padding: 5px;" &gt;&amp;nbsp;&lt;/td&gt;&lt;td class="TableHeaderLast" align="right" width="13%" style=" padding: 5px;" &gt;&amp;nbsp;&lt;/td&gt;&lt;td class="TableHeaderLast" align="right" width="13%" style=" padding: 5px; border-right: 1px solid #000000;" &gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr style="border-bottom: 1px solid #ccc"&gt;&lt;td class="TableRow" style="padding: 5px; border-bottom: 1px solid #E4E9EC; border-left: 1px solid #000000; "&gt;1&lt;/td&gt;&lt;td class="TableRow" style="padding: 5px; border-bottom: 1px solid #E4E9EC;"&gt;&lt;b&gt;sectiune1&lt;/b&gt;&lt;/td&gt;&lt;td class="TableRow" colspan="5" style="padding: 5px; border-bottom: 1px solid #E4E9EC; border-right: 1px solid #000000;"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr style="border-bottom: 1px solid #ccc"&gt;&lt;td class="TableRow" style="padding: 5px; border-left: 1px solid #000000;border-bottom: 1px solid #E4E9EC; "&gt;1.1&lt;/td&gt;&lt;td class="TableRow" style="padding: 5px;border-bottom: 1px solid #E4E9EC; "&gt;UNI-Cazan mixt AIREX 25 2S BICOMB MODUL  &lt;/td&gt;&lt;td class="TableRow" align="left" style="padding: 5px;border-bottom: 1px solid #E4E9EC; "&gt;94456&amp;nbsp;&lt;/td&gt;&lt;td class="TableRow"  align="center" style="padding: 5px;border-bottom: 1px solid #E4E9EC;"&gt;99999&lt;/td&gt;&lt;td class="TableRow" align="right" style="padding: 5px;border-bottom: 1px solid #E4E9EC; "&gt;24.623,00&lt;/td&gt;&lt;td class="TableRow" align="right"  style="padding: 5px;  border-right: 1px solid #000000;border-bottom: 1px solid #E4E9EC;"&gt;2.462.275.377,00&lt;/td&gt;&lt;/tr&gt;&lt;tr style="border-bottom: 1px solid #ccc"&gt;&lt;td class="TableRow" style="padding: 5px; border-left: 1px solid #000000;border-bottom: 1px solid #E4E9EC; "&gt;1.2&lt;/td&gt;&lt;td class="TableRow" style="padding: 5px;border-bottom: 1px solid #E4E9EC; "&gt;UNI-Cazan mixt AIREX 25 2S BICOMB MODUL  &lt;/td&gt;&lt;td class="TableRow" align="left" style="padding: 5px;border-bottom: 1px solid #E4E9EC; "&gt;94456&amp;nbsp;&lt;/td&gt;&lt;td class="TableRow"  align="center" style="padding: 5px;border-bottom: 1px solid #E4E9EC;"&gt;99999&lt;/td&gt;&lt;td class="TableRow" align="right" style="padding: 5px;border-bottom: 1px solid #E4E9EC; "&gt;24.623,00&lt;/td&gt;&lt;td class="TableRow" align="right"  style="padding: 5px;  border-right: 1px solid #000000;border-bottom: 1px solid #E4E9EC;"&gt;2.462.275.377,00&lt;/td&gt;&lt;/tr&gt;&lt;tr style="border-bottom: 1px solid #ccc"&gt;&lt;td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; border-left: 1px solid #000000; "&gt;&amp;nbsp;&lt;/td&gt;&lt;td class="TableRow" width="40%" style="padding: 5px; border-bottom: 1px solid #000; "&gt;&lt;b&gt; TOTAL sectiune1:&lt;/b&gt;&lt;/td&gt;&lt;td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; "&gt; &amp;nbsp;&lt;/td&gt;&lt;td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; "&gt; &amp;nbsp;&lt;/td&gt;&lt;td class="TableRowLast" colspan="2" align="right" style="padding: 5px; border-bottom: 1px solid #000; border-right: 1px solid #000000; "&gt;&lt;b&gt;4.924.550.754,00&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr style="border-bottom: 1px solid #ccc"&gt;&lt;td class="TableRow" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000; "&gt;&amp;nbsp;&lt;/td&gt;&lt;td class="TableRow" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td class="TableRow" colspan="5" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000; "&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr style="border-bottom: 1px solid #ccc"&gt;&lt;td class="TableRow" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #E4E9EC; border-left: 1px solid #000000; "&gt;2&lt;/td&gt;&lt;td class="TableRow" style="padding: 5px; border-bottom: 1px solid #E4E9EC; border-top: 1px solid #000;"&gt;&lt;b&gt;sectiune2&lt;/b&gt;&lt;/td&gt;&lt;td class="TableRow" colspan="5" style="padding: 5px; border-bottom: 1px solid #E4E9EC; border-right: 1px solid #000000; border-top: 1px solid #000;"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr style="border-bottom: 1px solid #ccc"&gt;&lt;td class="TableRow" style="padding: 5px; border-left: 1px solid #000000;border-bottom: 1px solid #E4E9EC; "&gt;2.1&lt;/td&gt;&lt;td class="TableRow" style="padding: 5px;border-bottom: 1px solid #E4E9EC; "&gt;ICI-Cazan condensatie MONOLITE 75-JB&lt;/td&gt;&lt;td class="TableRow" align="left" style="padding: 5px;border-bottom: 1px solid #E4E9EC; "&gt;81020076&amp;nbsp;&lt;/td&gt;&lt;td class="TableRow"  align="center" style="padding: 5px;border-bottom: 1px solid #E4E9EC;"&gt;99999&lt;/td&gt;&lt;td class="TableRow" align="right" style="padding: 5px;border-bottom: 1px solid #E4E9EC; "&gt;34.719,00&lt;/td&gt;&lt;td class="TableRow" align="right"  style="padding: 5px;  border-right: 1px solid #000000;border-bottom: 1px solid #E4E9EC;"&gt;3.471.865.281,00&lt;/td&gt;&lt;/tr&gt;&lt;tr style="border-bottom: 1px solid #ccc"&gt;&lt;td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; border-left: 1px solid #000000; "&gt;&amp;nbsp;&lt;/td&gt;&lt;td class="TableRow" width="40%" style="padding: 5px; border-bottom: 1px solid #000; "&gt;&lt;b&gt; TOTAL sectiune2:&lt;/b&gt;&lt;/td&gt;&lt;td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; "&gt; &amp;nbsp;&lt;/td&gt;&lt;td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; "&gt; &amp;nbsp;&lt;/td&gt;&lt;td class="TableRowLast" colspan="2" align="right" style="padding: 5px; border-bottom: 1px solid #000; border-right: 1px solid #000000; "&gt;&lt;b&gt;3.471.865.281,00&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr style="border-bottom: 1px solid #ccc"&gt;&lt;td class="TableRow" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000; "&gt;&amp;nbsp;&lt;/td&gt;&lt;td class="TableRow" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000;"&gt;&amp;nbsp;&lt;/td&gt;&lt;td class="TableRow" colspan="5" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000; "&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="TableTotal"  style=" padding: 5px; border-left: 1px solid #000000;"&gt;&lt;/td&gt;&lt;td colspan="3" class="TableTotal"  style=" padding: 5px; " &gt;&lt;b&gt;Total sectiune1:&lt;/b&gt;&lt;/td&gt;&lt;td align="right" class="TableTotal" style=" padding: 5px; " &gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/td&gt;&lt;td class="TableTotal" align="right" style=" padding: 5px; border-right: 1px solid #000000;" &gt;&lt;b&gt;4.924.550.754,00 &lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="TableTotal"  style=" padding: 5px; border-left: 1px solid #000000;"&gt;&lt;/td&gt;&lt;td colspan="3" class="TableTotal"  style=" padding: 5px; " &gt;&lt;b&gt;Total sectiune2:&lt;/b&gt;&lt;/td&gt;&lt;td align="right" class="TableTotal" style=" padding: 5px; " &gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;/td&gt;&lt;td class="TableTotal" align="right" style=" padding: 5px; border-right: 1px solid #000000;" &gt;&lt;b&gt;3.471.865.281,00 &lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="TableTotal"  style=" padding: 5px; border-left: 1px solid #000000;border-top: 1px solid #E4E9EC; "&gt;&lt;/td&gt;&lt;td colspan="4" class="TableTotal"  style=" padding: 5px;border-top: 1px solid #E4E9EC; " &gt;&lt;b&gt;Total oferta (RON):&lt;/b&gt;&lt;/td&gt;&lt;td colspan="2" align="right" class="TableTotal" style=" padding: 5px; border-right: 1px solid #000000;border-top: 1px solid #E4E9EC; " &gt;&lt;b&gt;8.396.416.035,00 &lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="TableTotal"  style=" padding: 5px; border-left: 1px solid #000000;border-bottom: 1px solid #000000;"&gt;&lt;/td&gt;&lt;td colspan="4" class="TableTotal"  style=" padding: 5px;border-bottom: 1px solid #000000;" &gt;&lt;b&gt;Total oferta (RON cu TVA inclus):&lt;/b&gt;&lt;/td&gt;&lt;td colspan="2" align="right" class="TableTotal" style=" padding: 5px; border-right: 1px solid #000000;border-bottom: 1px solid #000000;" &gt;&lt;b&gt;10.411.555.883,40 &lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
  </code></pre>
  
  <p>I thought of the following solution: if somehow i could split this html to rows and write row by row to pdf so that i can rollback when i write over the footer and then i add a new page, but there is a major problem, i get all sorts of warnings because the html table tags are not closed.
  Here is my code:</p>
  
  <pre><code>$pdf-&gt;setAutoPageBreak(false);
  $pdf-&gt;startTransaction();
  $html = new simple_html_dom();
  $html-&gt;load($data);
  $single = $html-&gt;find('#Container', 0);
  if($single){
  
   $rows = $single-&gt;getElementsByTagName('tr');
   $rows = $rows[0]-&gt;getElementsByTagName('tr');
  
   if($rows)
     for($i=1;$i&lt;(count($rows)-1);$i++){
       $pdf-&gt;writeHTMLCell($w=0, $h=0, $x='', $y='', '&lt;table&gt;', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
       $pdf-&gt;writeHTMLCell($w=0, $h=0, $x='', $y='', $rows[$i]-&gt;outertext, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
       $pdf-&gt;writeHTMLCell($w=0, $h=0, $x='', $y='', '&lt;/table&gt;', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
  
     if ($pdf-&gt;getY() &gt; $pdf-&gt;getPageHeight() - 30) {
         $pdf-&gt;rollbackTransaction(true);
         $pdf-&gt;AddPage();
         $pdf-&gt;writeHTMLCell($w=0, $h=0, $x='', $y='', '&lt;table&gt;', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
         $pdf-&gt;writeHTMLCell($w=0, $h=0, $x='', $y='', $rows[$i]-&gt;outertext, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
         $pdf-&gt;writeHTMLCell($w=0, $h=0, $x='', $y='', '&lt;/table&gt;', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
          }
      }
     }
  
  
      $pdf-&gt;commitTransaction();
      $pdf-&gt;setAutoPageBreak(true, 30);
  </code></pre>
  
  <p>I took a quick look at this 2 articles, maybe someone could find this handy:
  <a href="http://www.onemoretake.com/2009/03/27/revisited-tcpdf-variable-height-table-rows-with-multicell/" rel="nofollow">Some paging with multicell</a>
  and <a href="http://www.jonathanlbrown.com/2011/07/17/tcpdf-paging-those-multicells/" rel="nofollow">Another paging with multicell</a> . I found this 2 worthy of an example as a solution logic, but the methods used with tcpdf seemed to me a little weird.</p>