php – TCPDF在多个页面上拆分html表

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – TCPDF在多个页面上拆分html表脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用TCPDF输出一个html表,在我的例子中这个不是我必须修改的唯一一个,所以我需要一些规则来呈现我的页面.
到目前为止,TCPDF在我看来是一个强大的库,所以我希望不要尝试另一个.

我的问题是我需要将表头添加到表格显示的每个新页面,但我还有其他信息写入此pdf,必须没有表头,所以我不能使用重写头方法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></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>

我想到了以下解决方案:如果不知怎的话,我可以将这个html拆分为行并逐行写入pdf,以便当我在页脚上书写然后添加页面时我可以回滚,但是有一个主要问题,我得到各种警告,因为html表标签没有关闭.
这是我的代码

$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,$rows[$i]->outertext,'</table>',$autopadding=false);

   if ($pdf->getY() > $pdf->getPageHeight() - 30) {
       $pdf->rollbackTransaction(true);
       $pdf->AddPage();
       $pdf->writeHTMLCell($w=0,$autopadding=false);
       $pdf->writeHTMLCell($w=0,$autopadding=false);
        }
    }
   }


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

快速浏览了这2篇文章,也许有人会发现这个方便:
Some paging with multicell
Another paging with multicell.我发现这个值得一个例子作为解决方案逻辑,但与tcpdf一起使用的方法在我看来有点奇怪.

解决方法

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

但是我发现了一些问题:

>您的事务包装整个文档,回滚应该将文档重置为空.
>您的警告可能是由于您传递的部分HTML代码无法渲染而无法写入HTMLCell. (你只传递< table>或< / table>单独使用时,无法呈现)
>如果在表标记中包装每一行,则列不会排列.最好的办法是将该页面上的行包装在表格块中.

尝试这些更改,让我知道它是如何进行的

$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,$html_buffer.$rows[$i]->outertext.'</table>',$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,$html_buffer.'</table>',$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,$autopadding=false);
        // Final commit
        $pdf->commitTransaction(); 
    }
}
$pdf->setAutoPageBreak(true,30);

这会将页面上的所有内容包装在表格块中,以便列在该页面上排成一行. (但是,它们在页面之间可能仍然不同)另外,我让你能够像你想要的那样为每个新页面添加你自己的标题.只需将$html_header更改为您自己的标题代码即可.

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

脚本宝典总结

以上是脚本宝典为你收集整理的php – TCPDF在多个页面上拆分html表全部内容,希望文章能够帮你解决php – TCPDF在多个页面上拆分html表所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: