html2pdf = $html 2pdf->WriteHTML($VARIABLE);导致问题的线路


html2pdf = $html2pdf->WriteHTML($VARIABLE); line causing problems

我有一个表单,它接受用户输入,发布到自己并将我的php代码存储到一个字符串变量中,并将该字符串数据保存到服务器上的.html文件中。 我想取消.html文件,而是使用 html2pdf 保存到.pdf,但我遇到了一个问题。

我的php文件中有以下代码:

require_once(dirname(__FILE__).'/html2pdf_v4.03/html2pdf.class.php');
$html2pdf = new HTML2PDF('P','Letter','en');
$html2pdf->WriteHTML($current);
$html2pdf->Output('/var/www/Inspection/Quote/exemple.pdf');

当我注释掉第三行"$html 2pdf->WriteHTML($current);"时我收到"TCPDF 错误:某些数据已经输出,无法发送 PDF 文件"的错误。

当我注释掉第三行和第四行时,我页面的其余部分可以毫无问题地执行。我只是不明白我的.pdf。

我看过一些关于在写行之前冲洗掉一些东西的帖子,但没有一个足够清晰,让我可以完成。

另外,如果我使用简化得多的代码,我可以正确生成pdf。我不知道为什么这是可能的。

以下是有效的方法:

$content = "
<page>
<h1>Exemple d'utilisation</h1>
<br>
Ceci est un <b>exemple d'utilisation</b>
de <a href='http://html2pdf.fr/'>HTML2PDF</a>.<br>
</page>";
 require_once(dirname(__FILE__).'/html2pdf_v4.03/html2pdf.class.php');
$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');

*编辑*$current =

 <br><div align="center"><H1><center>Sales Order: <font color="red" style="bold"> 1 3
    </font></center></h1>
    </div>
    <hr width="85%">
    <table width="800" align="center">
    <tr>
    <td valign="top" width="50%" align="center">
    <table frame="border">
    <th bgcolor="cccccc" colspan="2" align="center">General Order Information</th>
        <tr>
        <td align="center" nowrap="nowrap" width="40%">Order Num:</td>
        <td width="60%">1</td>
    </tr>   
        <tr>
        <td align="center" nowrap="nowrap" width="40%">Order Rev:</td>
        <td width="20%">3</td>
    </tr>        
    <tr>
        <td align="center" nowrap="nowrap" width="40%">Quote Num:</td>
        <td width="20%">1</td>
    </tr>       
        <tr>
        <td align="center" nowrap="nowrap" width="40%">Order Date:</td>
        <td width="60%">1970-01-01</td>
    </tr>           
        <tr>
        <td align="center" nowrap="nowrap" width="40%">Customer Name:</td>
        <td width="60%">1</td>
    </tr>
        <tr>
        <td align="center" nowrap="nowrap" width="40%">Customer Buyer:</td>
        <td width="60%">1</td>
    </tr>
    <tr>
        <td align="center" nowrap="nowrap" width="40%">Sold By:</td>
        <td width="60%">ED </td>
    </tr>
    <tr>
        <td align="center" nowrap="nowrap" width="40%">Sales Rep:</td>
        <td width="60%">DR</td>
    </tr>
        <tr>
        <td align="center" nowrap="nowrap" width="40%">Prog Mgr:</td>
        <td width="60%">VJ</td>
    </tr>
    </table>
    </td>
    <!-- Start of Shipping Data -->
    <td valign="top" width="50%">
    <table align="center" frame="Border">
    <th bgcolor="cccccc" colspan="2" align="center">Shipping Information</th>
    <tr>
    <td align="center" nowrap="nowrap" >Terms:</td>
    <td align="left">1</td>
    </tr>
    <tr>
    <td align="center" nowrap="nowrap" >FOB:</td>
    <td align="left">1</td>
    </tr>
    <tr>
    <td align="center" nowrap="nowrap" >Shipping Co:</td>
    <td align="left">1</td>
    </tr>
    <tr>
    <td nowrap="nowrap" align="center">Ship Acct Num:</td>
    <td align="left">1</td>
    </tr>
    <tr>
    <td nowrap="nowrap" align="center">Shipping Method:</td>
    <td align="left">1</td>
    </tr>
    <tr>
    <td nowrap="nowrap" align="center" valign="top">Order Comments:</td>
    <td>1</td>
    </tr>
        <tr>
        <td nowrap="nowrap" align="center" width="40%">Cust SO Upload:</td>
        <td width="60%">The file 1-1-3.jpg has been uploaded, and Quote 1 has been added to the Quote directory </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    <hr width="85%">  <table align="center" width="85%" cellpadding="3" cellspacing="0"             frame="border" rules="all">
    <tr>
    <td bgcolor="cccccc" align="center"><h5>Line</h5></td>
    <td bgcolor="cccccc" align="center"><h5>Cust PN / Rev</td>
    <td bgcolor="cccccc" align="center"><h5>PN / Rev</td>
    <td bgcolor="cccccc" align="center"><h5>Description</h5></td>
    <td bgcolor="cccccc" align="center"><h5>Qty</h5></td>
    <td bgcolor="cccccc" align="center"><h5>UOM</h5></td>
    <td bgcolor="cccccc" align="center"><h5>Sale Price</h5></td>
    <td bgcolor="cccccc" align="center"><h5>Line Total</h5></td>
    <td bgcolor="cccccc" align="center"><h5>Required Date</h5></td>
    <td bgcolor="cccccc" align="center"><h5>Program</h5></td>
    </tr>  <tr>
    <td width="3px" valign="top" align="center">
    1 </td> <td nowrap="nowrap" valign="top" align="center">
    1 - 1
    </td>
    <td nowrap="nowrap" valign="top" align="center">
    1 - 1
    </td>
    <td nowrap="nowrap" valign="top" align="center">
    1
    </td>
    <td nowrap="nowrap" valign="top" align="center">
    1
    </td>
    <td nowrap="nowrap" valign="top" align="center">
    1
    </td>
    <td nowrap="nowrap" valign="top" align="center">
    1.00
    </td>
    <td nowrap="nowrap" valign="top" align="center">
    1.00
    </td>
    <td nowrap="nowrap" valign="top" align="center">
    1970-01-01
    </td>
    <td nowrap="nowrap" valign="top" align="center">
    1
    </td>
    </tr> <tr>
    <td align="right" colspan="10" bgcolor="cccccc">
    <b>The Order Total is 1.00</b>
    </td>
    </tr>
    </table>

wkhtmltopdf是我想远离的东西,因为它需要一个x服务器。如果可能的话,我想避免的事情。

提前谢谢。

您应该查看创建 PDF 的所有 php 脚本,然后检查没有任何类型的"备用行"或白线,然后再检查

<?php

标记。祝你好运!

在编写PDF之前,您必须非常小心,不要输出(也称为渲染)任何内容。

错误消息"我收到"TCPDF 错误:某些数据已经输出,无法发送 PDF 文件"的错误通常是由于您在编写 PDF 之前已经回显了一些内容。

你注意到其他人说"刷新某些东西"的事情与"输出缓冲"有关 - 这是你应该在html2pdf/TCPDF中绝对使用的技术。

输出缓冲的工作原理如下:

ob_start() //starts output buffering
echo "<p>String you want to write to your PDF</p>";
$stringForPdf = ob_get_contents(); // this now contains the the above string
ob_end_clean(); // close and clean the output buffer.
html2pdf->WriteHtml($stringForPdf);

您可以在此处参考完整文档:输出缓冲控制

此外,请确保您的 PHP 文件中没有结束 PHP 标签:?>,因为将输出以下每个空格。

如果您提供更多的代码,并且在剪断后,我也许可以进一步分析这一点。

如果你想

使用WKHTMLTOPDF,但不必安装它,你可以使用Sprint PDF。这是一项将HTML转换为PDF的在线服务,并通过RESTful API进行。