Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php TCPDF在多个页面上拆分html表_Php_Html_Html Table_Tcpdf - Fatal编程技术网

Php TCPDF在多个页面上拆分html表

Php TCPDF在多个页面上拆分html表,php,html,html-table,tcpdf,Php,Html,Html Table,Tcpdf,我使用TCPDF输出html表,在我的示例中,这个表不是唯一需要修改的表,所以我需要一些规则来呈现我的页面。 到目前为止,TCPDF在我看来是一个功能强大的库,所以我希望不要尝试另一个 我的问题是,我需要将表格标题添加到表格显示的每个新页面,但我还有其他信息要写入此pdf,这些信息必须没有表格标题,因此我无法在tcpdf中使用“重写标题”方法 <table border="0" cellspacing="0" cellpadding="0" align="center" width="10

我使用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>
Nr.denumirecont.Pret unitar
法拉TVA(RON)Pret总计
法拉TVA(RON)1选通1.1 NI-Cazan mixt AIREX 25 2双组模块94456 999924.623002.462.275.377001.2 UNI-Cazan mixt AIREX 25 2双组模块94456 9999224.623002.462.275.377,00总截面1:4.924.550.754,00 2截面2 2.1ICI-Cazan冷凝单体75-JB81020076 99934.719003.471.865.281,00总截面2:281.861,00总截面1:4.924.550.754,00总截面2:3.471.865.281,00总截面(RON):8.396.416.035,00总截面(RON cu TVA inclus):10.411.555.883,40
我想到了以下解决方案:如果我能将这个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);
$pdf->setAutoPageBreak(false);
$pdf->startTransaction();
$html=新的简单html\U dom();
$html->load($data);
$single=$html->find(“#容器”,0);
若有($单){
$rows=$single->getElementsByTagName('tr');
$rows=$rows[0]->getElementsByTagName('tr');
如果($行)
对于($i=1;$iwriteHTMLCell($w=0,$h=0,$x='',$y='',$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='',$border=0,$ln=1,$fill=0,$reseth=true,$align='',$autopadding=false);
如果($pdf->getY()>$pdf->getPageHeight()-30){
$pdf->rollbackTransaction(true);
$pdf->AddPage();
$pdf->writeHTMLCell($w=0,$h=0,$x='',$y='',$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='',$border=0,$ln=1,$fill=0,$reseth=true,$align='',$autopadding=false);
}
}
}
$pdf->commitTransaction();
$pdf->setAutoPageBreak(true,30);
我快速浏览了这两篇文章,也许有人会发现这很方便:
还有。我发现这2个值得作为解决方案逻辑的一个例子,但在我看来,tcpdf使用的方法有点奇怪。

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

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

  • 事务会包装整个文档,回滚操作会将文档重置为零
  • 您的警告可能是由于您正在传递无法呈现WriteHTML单元格的部分html代码造成的。(仅传递或单独传递时无法呈现)
  • 如果将每一行包装在表标记中,则列不会对齐。最好的办法是将该页上的行包装在表块中
  • 试试这些变化,让我知道进展如何

    $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);
    
    $pdf->setAutoPageBreak(false);
    //$pdf->startTransaction();//已移动
    $html=新的简单html\U dom();
    $html->load($data);
    $single=$html->find(“#容器”,0);
    若有($单){
    $rows=$single->getElementsByTagName('tr');
    $rows=$rows[0]->getElementsByTagName('tr');
    如果($行){
    $pdf->startTransaction();//仅因为我们可能需要启动事务
    //对于html,这将启动html,并且可以选择在每个新页面上插入标题行作为第一行。
    $html_头='NameAge';
    $html\u buffer='.$html\u头;
    对于($i=1;$iwriteHTMLCell($w=0,$h=0,$x='',$y='',$html\u buffer.$rows[$i]>outertext.',$border=0,$ln=1,$fill=0,$reseth=true,$align='',$autoadding=false);
    如果($pdf->getY()<($pdf->getPageHeight()-30)){//请注意小于运算符
    //我们可能会添加更多的文本,所以请撤消该操作
    $pdf->rollbackTransaction(true);
    //并存储html
    $html\u buffer.=$rows[$i]->outertext;
    }否则{
    //我们超出了限制
    $pdf->rollbackTransaction(true);
    //写下最后一个已知的好表
    $pdf->writeHTMLCell($w=0,$h=0,$x='',$y='',$html\u buffer'',$border=0,$ln=1,$fill=0,$reseth=true,$align='',$autopadding=false);
    //添加新页面
    $pdf->AddPage();
    //结束此交易
    $pdf->commitTransaction();
    //启动新事务
    $pdf->startTransaction();
    //重置html缓冲区
    $html\u buffer='.$html\u头;
    //将最后一页无法容纳的行添加到html缓冲区
    $html\u buffer.=$rows[$i]->outertext;
    }
    }
    //我们的缓冲区中仍然有信息,它可以放在单个页面上
    $pdf->writeHTMLCell($w=0,$h=0,$x='',$y='',$html\u buffer'',$border=0,$ln=1,$fill=0,$reseth=true,$align='',$autopadding=false);
    //最后承诺
    $pdf->commitTransaction();
    }
    }
    $pdf->setAutoPageBreak(true,30);
    
    这会将页面上的所有内容包装在一个表块中,以便列在该页面上对齐。(但是,每一页的列可能仍然不同)此外,我给了您t