TCPDF在多个页面上分割html表格 [英] TCPDF split html table on multiple pages

查看:1299
本文介绍了TCPDF在多个页面上分割html表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用TCPDF来输出一个html表格,在我的例子中这不是唯一一个我必须修改的表格,所以我需要一些规则来渲染我的页面。
到目前为止,TCPDF对我来说似乎是一个强大的库,所以我希望不要尝试另一个库。



我的问题是我需要将表头添加到每个新表的页面,但我也有其他信息可以写入此pdf,没有表头,所以我不能在tcpdf中使用重写头文件方法。

 < table border =0cellspacing =0cellpadding =0align =centerwidth =100%>< tr>< td>< table border =0cellpacing =0cellpadding =3 leftwidth =100%>< tr>< td class =TableHeaderwidth =5%style =padding:5px; border-top:1px solid#000000; border-left:1px solid#000000; border-bottom:1px solid#E4E9EC; > Nr。< / td>< td class =TableHeaderwidth =40%style =padding:5px; border-top:1px solid#000000; border-bottom:1px solid#E4E9EC; > Denumire< / td>< td class =tableHeaderwidth =20%align =leftstyle =padding:5px; border-top:1px solid#000000; border-bottom:1px solid#E4E9EC ; > Cod< / td>< td class =TableHeaderwidth =10%align =centerstyle =padding:5px; border-top:1px solid#000000; border-bottom:1px solid#E4E9EC ; > Cant。< / td>< td class =TableHeaderLastalign =rightwidth =13%style =padding:5px; border-top:1px solid#000000; border-bottom:1px solid #E4E9EC; > Pret unitar< br> fara TVA(RON)< / td>< td class =TableHeaderLastalign =rightwidth =13%style =padding:5px; border-bottom:1px solid#E4E9EC; border-top:1px solid#000000; border-right:1px solid#000000; < / table>< / td>< / tr>< / table>< table border ="> pret total< / td>< / tr>< 0cellspacing =0cellpadding =0align =centerwidth =100%id =Container>< tr>< td>< table border =0cellspacing =0 cellpadding =3align =leftwidth =100%>< tr>< td class =TableHeaderwidth =5%style =padding:5px; border-left:1px solid #000000; >& nbsp;< / td>< td class =TableHeaderwidth =40%style =padding:5px; >& nbsp;< / td>< td class =TableHeaderwidth =20%style =padding:5px; >& nbsp;< / td>< td class =TableHeaderwidth =10%style =padding:5px; >& nbsp;< / td>< td class =TableHeaderLastalign =rightwidth =13%style =padding:5px; >& nbsp;< / td>< td class =TableHeaderLastalign =rightwidth =13%style =padding:5px; border-right:1px solid#000000; >< / tr>< tr style =border-bottom:1px solid #ccc>< td class =TableRowstyle =padding:5px; border- bottom:1px solid#E4E9EC; border-left:1px solid#000000;> 1< / td>< td class =TableRowstyle =padding:5px; border-bottom:1px solid#E4E9EC;> ;< b> sectiune1< / b>< / td>< td class =TableRowcolspan =5style =padding:5px; border-bottom:1px solid#E4E9EC; border-right:1px solid #000000;>& nbsp;< / td>< / tr>< tr style =border-bottom:1px solid #ccc>< td class =TableRowstyle =padding: 5px; border-left:1px solid#000000; border-bottom:1px solid#E4E9EC;> 1.1< / td>< td class =TableRowstyle =padding:5px; border-bottom:1px solid #E4E9EC;> UNI-Cazan混合物AIREX 25 2S BICOMB MODUL< / td>< td class =TableRowalign =leftstyle =padding:5px; border-bottom:1px solid#E4E9EC; > 94456& nbsp;< / td>< td class =TableRowalign =centerstyle =padding:5px; border-botto m:1px solid#E4E9EC;> 99999< / td>< td class =TableRowalign =rightstyle =padding:5px; border-bottom:1px solid#E4E9EC; > 24.623,00< / td>< td class =TableRowalign =rightstyle =padding:5px; border-bottom:1px solid #cccborder-bottom:1px solid#000000; border-bottom:1px solid#E4E9EC;> 2.462.275.377,00< / td>< / tr>< tr style = >< td class =TableRowstyle =padding:5px; border-left:1px solid#000000; border-bottom:1px solid#E4E9EC; > 1.2< / td>< td class =TableRowstyle =padding:5px; border-bottom:1px solid#E4E9EC; > UNI-Cazan混合物AIREX 25 2S BICOMB MODUL< / td>< td class =TableRowalign =leftstyle =padding:5px; border-bottom:1px solid#E4E9EC; > 94456& nbsp;< / td>< td class =TableRowalign =centerstyle =padding:5px; border-bottom:1px solid#E4E9EC;> 99999< / td><< ; td class =TableRowalign =rightstyle =padding:5px; border-bottom:1px solid#E4E9EC; > 24.623,00< / td>< td class =TableRowalign =rightstyle =padding:5px; border-bottom:1px solid #cccborder-bottom:1px solid#000000; border-bottom:1px solid#E4E9EC;> 2.462.275.377,00< / td>< / tr>< tr style = >< td class =TableRowstyle =padding:5px; border-bottom:1px solid#000; border-left:1px solid#000000; >& nbsp;< / td>< td class =TableRowwidth =40%style =padding:5px; border-bottom:1px solid#000; >< b> TOTAL sectiune1:< / b>< / td>< td class =TableRowstyle =padding:5px; border-bottom:1px solid#000; >& nbsp;< / td>< td class =TableRowstyle =padding:5px; border-bottom:1px solid#000; >& nbsp;< / td>< td class =TableRowLastcolspan =2align =rightstyle =padding:5px; border-bottom:1px solid#000; border-right:1px solid#000000; >< b> 4.924.550.754,00< / b>< / td>< / tr>< tr style =border-bottom:1px solid #ccc>< td class =TableRow style =padding:5px; border-top:1px solid#000; border-bottom:1px solid#000; >& nbsp;< / td>< td class =TableRowstyle =padding:5px; border-top:1px solid#000; border-bottom:1px solid#000;>& nbsp;< / td>< td class =TableRowcolspan =5style =padding:5px; border-top:1px solid#000; border-bottom:1px solid#000; >& nbsp;< / td>< / tr>< tr style =border-bottom:1px solid #ccc>< td class =TableRowstyle =padding:5px; border-top:1px solid#000; border-bottom:1px solid#E4E9EC; border-left:1px solid#000000; > 2< / td>< td class =TableRowstyle =padding:5px; border-bottom:1px solid#E4E9EC; border-top:1px solid#000;>< b> sectiune2< / b>< / td>< td class =TableRowcolspan =5style =padding:5px; border-bottom:1px solid#E4E9EC; border-right:1px solid#000000; border-top:1px solid#000;>& nbsp;< / td>< / tr>< tr style =border-bottom:1px solid #ccc>< td class =TableRow style =padding:5px; border-left:1px solid#000000; border-bottom:1px solid#E4E9EC; > 2.1< / td>< td class =TableRowstyle =padding:5px; border-bottom:1px solid#E4E9EC; > ICI-Cazan缩写MONOLITE 75-JB< / td>< td class =TableRowalign =leftstyle =padding:5px; border-bottom:1px solid#E4E9EC; > 81020076& nbsp;< / td>< td class =TableRowalign =centerstyle =padding:5px; border-bottom:1px solid#E4E9EC;> 99999< / td><< ; td class =TableRowalign =rightstyle =padding:5px; border-bottom:1px solid#E4E9EC; > 34.719,00< / td>< td class =TableRowalign =rightstyle =padding:5px; border-bottom:1px solid #cccborder-bottom:1px solid#000000; border-bottom:1px solid#E4E9EC;> 3.471.865.281,00< / td>< / tr>< tr style = >< td class =TableRowstyle =padding:5px; border-bottom:1px solid#000; border-left:1px solid#000000; >& nbsp;< / td>< td class =TableRowwidth =40%style =padding:5px; border-bottom:1px solid#000; >< b> TOTAL sectiune2:< / b>< / td>< td class =TableRowstyle =padding:5px; border-bottom:1px solid#000; >& nbsp;< / td>< td class =TableRowstyle =padding:5px; border-bottom:1px solid#000; >& nbsp;< / td>< td class =TableRowLastcolspan =2align =rightstyle =padding:5px; border-bottom:1px solid#000; border-right:1px solid#000000; >< b> 3.471.865.281,00< / b>< / td>< / tr>< tr style =border-bottom:1px solid #ccc>< td class =TableRow style =padding:5px; border-top:1px solid#000; border-bottom:1px solid#000; >& nbsp;< / td>< td class =TableRowstyle =padding:5px; border-top:1px solid#000; border-bottom:1px solid#000;>& nbsp;< / td>< td class =TableRowcolspan =5style =padding:5px; border-top:1px solid#000; border-bottom:1px solid#000; >& nbsp;< / td>< / tr>< tr>< td class =TableTotalstyle =padding:5px; border-left:1px solid#000000;>< / td>< td colspan =3class =TableTotalstyle =padding:5px; >< b>< b> Total sectiune1:< / b>< / td>< td align =rightclass =TableTotalstyle =padding:5px; >< b>& nbsp;< / b>< / td>< td class =TableTotalalign =rightstyle =padding:5px; < td class =TableTotalstyle =true padding:5px; border-left:1px solid#000000;>< / td>< td colspan =3class =TableTotalstyle =padding:5px; >< b>< b> Total sectiune2:< / b>< / td>< td align =rightclass =TableTotalstyle =padding:5px; >< b>& nbsp;< / b>< / td>< td class =TableTotalalign =rightstyle =padding:5px; < td class =TableTotalstyle =1 padding:5px; border-left:1px solid#000000; border-top:1px solid#E4E9EC; >< / td>< td colspan =4class =TableTotalstyle =padding:5px; border-top:1px solid#E4E9EC; >< b> Total oferta(RON):< / b>< / td>< td colspan =2align =rightclass =TableTotalstyle =padding:5px; border-right:1px solid#000000; border-top:1px solid#E4E9EC; >< b> 8.396.416.035,00< / b>< / td>< / tr>< tr>< td class =TableTotalstyle =padding:5px; border-left:1px solid#000000; border-bottom:1px solid#000000;>< / td>< td colspan =4class =TableTotalstyle =padding:5px; border-bottom: 1px solid#000000;>< b> Total oferta(RON cu TVA inclusion):< / b>< / td>< td colspan =2align =rightclass =TableTotalstyle =padding:5px; border-bottom:1px solid#000000; border-bottom:1px solid#000000;>< b> 10.411.555.883,40< / b>< / td>< / tr>< / table> < / td>< / tr>< / table> 

:如果以某种方式我可以拆分这个HTML到行,并逐行写入pdf,这样我可以回滚时,我写在页脚,然后我添加一个新的页面,但有一个主要问题,我得到各种警告,因为
这是我的代码:

  $ pdf-> setAutoPageBreak(false) ; 
$ pdf-> startTransaction();
$ 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)
for($ i = 1; $ i<(count($ rows )-1); $ i ++){
$ pdf-> writeHTMLCell($ w = 0,$ h = 0,$ x =' ,$ Y =,‘<表>’,$边界= 0,$ LN = 1,$填= 0,$ reseth =真,$ ALIGN =,$ autopadding = FALSE);
$ pdf-> writeHTMLCell($ w = 0,$ h = 0,$ x ='',$ y ='',$ rows [$ i] - > outertext,$ border = 0,$ ln = 1,$ fill = 0,$ reseth = true,$ align ='',$ autopadding = false);
$ pdf-> writeHTMLCell($ w = 0,$ h = 0,$ x ='',$ y ='','< / table>',$ border = 0,$ ln = 1 ,$ fill = 0,$ reseth = true,$ align ='',$ autopadding = false);

if($ pdf-> getY()> $ pdf-> getPageHeight() - 30){
$ pdf-> rollbackTransaction(true);
$ pdf-> AddPage();
$ pdf-> writeHTMLCell($ w = 0,$ h = 0,$ x ='',$ y ='','< table>,$ border = 0,$ ln = 1, $ fill = 0,$ reseth = true,$ align ='',$ autopadding = false);
$ pdf-> writeHTMLCell($ w = 0,$ h = 0,$ x ='',$ y ='',$ rows [$ i] - > outertext,$ border = 0,$ ln = 1,$ fill = 0,$ reseth = true,$ align ='',$ autopadding = false);
$ pdf-> writeHTMLCell($ w = 0,$ h = 0,$ x ='',$ y ='','< / table>',$ border = 0,$ ln = 1 ,$ fill = 0,$ reseth = true,$ align ='',$ autopadding = false);
}
}
}


$ pdf-> commitTransaction();
$ pdf-> setAutoPageBreak(true,30);

我快速浏览了这两篇文章,也许有人可以找到它:
一些分页与多单元
另一个多小区分页。我发现这2个值得作为一个解决方案逻辑的例子,但与tcpdf一起使用的方法在我看来有点奇怪。

解决方案

我认为你的解决方案很好。我能想到的其他方法是使用固定宽度的多单元,这可能会非常痛苦,但会给你静态列宽。



我注意到一些问题: p>


  1. 您的交易包装整个文档,回滚应该将文档重置为空。 您的警告是可能是由于您传递了部分HTML代码而无法写入HTMLCell。 (只传递< table>或< / table>当单独使用时,无法呈现)

  2. 如果您将每行包装在表格标签中,那么您的列不会排队。最好的办法是将该页面上的行包装在一个表格块中。

尝试这些更改并让我知道它是如何发生的

  $ pdf-> setAutoPageBreak(false); 
// $ pdf-> startTransaction(); //移动
$ html = new simple_html_dom();
$ html->载入($ data);
$ single = $ html-> find('#Container',0);
if($ single){

$ rows = $ single-> getElementsByTagName('tr');
$ rows = $ rows [0] - > getElementsByTagName('tr');

if($ rows){
$ pdf-> startTransaction(); //开始事务只是因为我们可能需要它
//标题为html,这会启动html,并可以选择性地将标题行插入每个新页面的第一行。
$ html_header ='< tr>< td>名称< / td>< td>年龄< / 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)){//注意小于运算符
//我们可能能够添加更多的文本,所以撤消
$ pdf-> rollbackTransaction(true);
//并存储html
$ html_buffer。= $ rows [$ i] - > outertext;
} else {
//我们超出了我们的限制
$ pdf-> rollbackTransaction(true);
//写上一个已知的好表
$ pdf-> writeHTMLCell($ w = 0,$ h = 0,$ x ='',$ y ='',$ html_buffer。'< / table>',$ border = 0,$ ln = 1,$ fill = 0,$ reseth = true,$ align ='',$ autopadding = false);
//添加新页面
$ pdf-> AddPage();
//结束此事务
$ pdf-> commitTransaction();
//开始新的交易
$ pdf-> startTransaction();
//重置html缓冲区
$ html_buffer ='< table>'。$ html_header;
//我们无法将最后一页的行添加到html缓冲区
$ html_buffer。= $ rows [$ i] - > outertext;


//我们的缓冲区中仍然有信息,并且它适合于单个页面
$ 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);

这将页面上的所有内容封装在一个表格块中,以便列中的列将排列在该页面上。 (然而,它们可能仍然不同于页面)另外,我还给了你自己的标题添加到每个新页面的能力,就像你想要的一样。只需将$ html_header更改为您自己的标题行代码即可。



当您说TCPDF可能会令人困惑时,您是对的,但它也非常强大,一旦你掌握了它你可以制作一些非常好的文档。


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.

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.

<table border="0" cellspacing="0" cellpadding="0" align="center" width="100%" ><tr><td><table border="0" cellspacing="0" cellpadding="3" align="left" width="100%" ><tr><td class="TableHeader"  width="5%" style="padding: 5px; border-top: 1px solid #000000; border-left: 1px solid #000000; border-bottom: 1px solid #E4E9EC;" >Nr.</td><td class="TableHeader" width="40%" style=" padding: 5px; border-top: 1px solid #000000; border-bottom: 1px solid #E4E9EC;" >Denumire</td><td class="TableHeader" width="20%" align="left" style=" padding: 5px; border-top: 1px solid #000000; border-bottom: 1px solid #E4E9EC;" >Cod</td><td class="TableHeader" width="10%" align="center" style=" padding: 5px; border-top: 1px solid #000000; border-bottom: 1px solid #E4E9EC;" >Cant.</td><td class="TableHeaderLast" align="right" width="13%" style=" padding: 5px; border-top: 1px solid #000000; border-bottom: 1px solid #E4E9EC;" >Pret unitar<br />fara TVA (RON)</td><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;" >Pret total<br />fara TVA (RON)</td></tr></table></td></tr></table><table border="0" cellspacing="0" cellpadding="0" align="center" width="100%" id="Container"><tr><td><table border="0" cellspacing="0" cellpadding="3" align="left" width="100%" ><tr><td class="TableHeader"  width="5%" style="padding: 5px; border-left: 1px solid #000000;" >&nbsp;</td><td class="TableHeader" width="40%" style=" padding: 5px;" >&nbsp;</td><td class="TableHeader" width="20%" style=" padding: 5px;" >&nbsp;</td><td class="TableHeader" width="10%" style=" padding: 5px;" >&nbsp;</td><td class="TableHeaderLast" align="right" width="13%" style=" padding: 5px;" >&nbsp;</td><td class="TableHeaderLast" align="right" width="13%" style=" padding: 5px; border-right: 1px solid #000000;" >&nbsp;</td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #E4E9EC; border-left: 1px solid #000000; ">1</td><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #E4E9EC;"><b>sectiune1</b></td><td class="TableRow" colspan="5" style="padding: 5px; border-bottom: 1px solid #E4E9EC; border-right: 1px solid #000000;">&nbsp;</td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-left: 1px solid #000000;border-bottom: 1px solid #E4E9EC; ">1.1</td><td class="TableRow" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">UNI-Cazan mixt AIREX 25 2S BICOMB MODUL  </td><td class="TableRow" align="left" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">94456&nbsp;</td><td class="TableRow"  align="center" style="padding: 5px;border-bottom: 1px solid #E4E9EC;">99999</td><td class="TableRow" align="right" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">24.623,00</td><td class="TableRow" align="right"  style="padding: 5px;  border-right: 1px solid #000000;border-bottom: 1px solid #E4E9EC;">2.462.275.377,00</td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-left: 1px solid #000000;border-bottom: 1px solid #E4E9EC; ">1.2</td><td class="TableRow" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">UNI-Cazan mixt AIREX 25 2S BICOMB MODUL  </td><td class="TableRow" align="left" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">94456&nbsp;</td><td class="TableRow"  align="center" style="padding: 5px;border-bottom: 1px solid #E4E9EC;">99999</td><td class="TableRow" align="right" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">24.623,00</td><td class="TableRow" align="right"  style="padding: 5px;  border-right: 1px solid #000000;border-bottom: 1px solid #E4E9EC;">2.462.275.377,00</td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; border-left: 1px solid #000000; ">&nbsp;</td><td class="TableRow" width="40%" style="padding: 5px; border-bottom: 1px solid #000; "><b> TOTAL sectiune1:</b></td><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; "> &nbsp;</td><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; "> &nbsp;</td><td class="TableRowLast" colspan="2" align="right" style="padding: 5px; border-bottom: 1px solid #000; border-right: 1px solid #000000; "><b>4.924.550.754,00</b></td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000; ">&nbsp;</td><td class="TableRow" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000;">&nbsp;</td><td class="TableRow" colspan="5" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000; ">&nbsp;</td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #E4E9EC; border-left: 1px solid #000000; ">2</td><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #E4E9EC; border-top: 1px solid #000;"><b>sectiune2</b></td><td class="TableRow" colspan="5" style="padding: 5px; border-bottom: 1px solid #E4E9EC; border-right: 1px solid #000000; border-top: 1px solid #000;">&nbsp;</td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-left: 1px solid #000000;border-bottom: 1px solid #E4E9EC; ">2.1</td><td class="TableRow" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">ICI-Cazan condensatie MONOLITE 75-JB</td><td class="TableRow" align="left" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">81020076&nbsp;</td><td class="TableRow"  align="center" style="padding: 5px;border-bottom: 1px solid #E4E9EC;">99999</td><td class="TableRow" align="right" style="padding: 5px;border-bottom: 1px solid #E4E9EC; ">34.719,00</td><td class="TableRow" align="right"  style="padding: 5px;  border-right: 1px solid #000000;border-bottom: 1px solid #E4E9EC;">3.471.865.281,00</td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; border-left: 1px solid #000000; ">&nbsp;</td><td class="TableRow" width="40%" style="padding: 5px; border-bottom: 1px solid #000; "><b> TOTAL sectiune2:</b></td><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; "> &nbsp;</td><td class="TableRow" style="padding: 5px; border-bottom: 1px solid #000; "> &nbsp;</td><td class="TableRowLast" colspan="2" align="right" style="padding: 5px; border-bottom: 1px solid #000; border-right: 1px solid #000000; "><b>3.471.865.281,00</b></td></tr><tr style="border-bottom: 1px solid #ccc"><td class="TableRow" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000; ">&nbsp;</td><td class="TableRow" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000;">&nbsp;</td><td class="TableRow" colspan="5" style="padding: 5px; border-top: 1px solid #000; border-bottom: 1px solid #000; ">&nbsp;</td></tr><tr><td class="TableTotal"  style=" padding: 5px; border-left: 1px solid #000000;"></td><td colspan="3" class="TableTotal"  style=" padding: 5px; " ><b>Total sectiune1:</b></td><td align="right" class="TableTotal" style=" padding: 5px; " ><b>&nbsp;</b></td><td class="TableTotal" align="right" style=" padding: 5px; border-right: 1px solid #000000;" ><b>4.924.550.754,00 </b></td></tr><tr><td class="TableTotal"  style=" padding: 5px; border-left: 1px solid #000000;"></td><td colspan="3" class="TableTotal"  style=" padding: 5px; " ><b>Total sectiune2:</b></td><td align="right" class="TableTotal" style=" padding: 5px; " ><b>&nbsp;</b></td><td class="TableTotal" align="right" style=" padding: 5px; border-right: 1px solid #000000;" ><b>3.471.865.281,00 </b></td></tr><tr><td class="TableTotal"  style=" padding: 5px; border-left: 1px solid #000000;border-top: 1px solid #E4E9EC; "></td><td colspan="4" class="TableTotal"  style=" padding: 5px;border-top: 1px solid #E4E9EC; " ><b>Total oferta (RON):</b></td><td colspan="2" align="right" class="TableTotal" style=" padding: 5px; border-right: 1px solid #000000;border-top: 1px solid #E4E9EC; " ><b>8.396.416.035,00 </b></td></tr><tr><td class="TableTotal"  style=" padding: 5px; border-left: 1px solid #000000;border-bottom: 1px solid #000000;"></td><td colspan="4" class="TableTotal"  style=" padding: 5px;border-bottom: 1px solid #000000;" ><b>Total oferta (RON cu TVA inclus):</b></td><td colspan="2" align="right" class="TableTotal" style=" padding: 5px; border-right: 1px solid #000000;border-bottom: 1px solid #000000;" ><b>10.411.555.883,40 </b></td></tr></table></td></tr></table>

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:

$pdf->setAutoPageBreak(false);
$pdf->startTransaction();
$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)
   for($i=1;$i<(count($rows)-1);$i++){
     $pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', '<table>', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
     $pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $rows[$i]->outertext, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
     $pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', '</table>', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);

   if ($pdf->getY() > $pdf->getPageHeight() - 30) {
       $pdf->rollbackTransaction(true);
       $pdf->AddPage();
       $pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', '<table>', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
       $pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $rows[$i]->outertext, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
       $pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', '</table>', $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=false);
        }
    }
   }


    $pdf->commitTransaction();
    $pdf->setAutoPageBreak(true, 30);

I took a quick look at this 2 articles, maybe someone could find this handy: Some paging with multicell and Another paging with multicell . I found this 2 worthy of an example as a solution logic, but the methods used with tcpdf seemed to me a little weird.

解决方案

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:

  1. Your transaction wraps the entire document, a rollback should reset the document to nothing.
  2. 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)
  3. 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.

这篇关于TCPDF在多个页面上分割html表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆