7

我使用 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><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>

我想到了以下解决方案:如果我能以某种方式将这个 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, $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 篇文章,也许有人会觉得这很方便: Some paging with multicellAnother paging with multicell。我发现这 2 值得作为解决方案逻辑的示例,但与 tcpdf 一起使用的方法在我看来有点奇怪。

4

2 回答 2

6

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

然而,我注意到了一些问题:

  1. 您的事务包装了整个文档,回滚应该将文档重置为空。
  2. 您的警告很可能是由于您传递的部分 html 代码无法呈现到 writeHTMLCell 的事实引起的。(您只传递 <table> 或 </table> 单独时无法呈现)
  3. 如果您将每一行包装在表格标签中,您的列将不会排列。最好的办法是将该页面上的行包装在一个表格块中。

试试这些改变,让我知道结果如何

$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);

这会将页面上的所有内容包装在一个表格块中,因此列将在该页面上对齐。(它们可能仍然因页面而异)另外,我让您能够根据需要将自己的标题添加到每个新页面。只需将 $html_header 更改为您自己的标题行代码。

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

于 2012-06-27T08:55:19.700 回答
5

转到 TCPDF 并选择Example 48,您将看到一个带有<thead>元素的示例。

于 2013-07-07T18:41:22.360 回答