【问题标题】:html2pdf = $html2pdf->WriteHTML($VARIABLE); line causing problemshtml2pdf = $html2pdf->WriteHTML($VARIABLE);导致问题的线路
【发布时间】:2012-07-03 18:39:51
【问题描述】:

我有一个表单,它接受用户输入、发布到自身并将我的 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');

当我注释掉第三行“$html2pdf->WriteHTML($current);”我收到“TCPD 错误:某些数据已输出,无法发送 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');

* 编辑 * $当前 =

 <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 服务器。 . .如果可能的话,我想避免的事情。

提前致谢。

【问题讨论】:

  • 你真的有一个变量$current吗?在您的测试示例中,您使用$content
  • 而且您实际上并没有包含错误消息。
  • 我们能看到$current变量的内容吗?
  • 我确实有一个变量 $current... 它有点冗长。我已添加到原帖中。

标签: php html2pdf


【解决方案1】:

您应该查看创建 PDF 的所有 php 脚本,然后查看在您的

之前没有任何类型的“备用行”或白线
<?php

标签。 祝你好运!

【讨论】:

    【解决方案2】:

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

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

    您注意到其他人所说的“刷新某些东西”与“输出缓冲”有关 - 这是您绝对应该与 html2pdf/TCPPDF 一起使用的技术。

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

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

    您可以在此处参考完整文档:Output Buffering Control

    此外,请确保您的 PHP 文件中没有结束 PHP 标记:?>,因为后面的每个空格都会被输出。

    如果您在剪切前后提供更多代码,我也许可以进一步分析。

    【讨论】:

      【解决方案3】:

      如果您想使用 WKHTMLTOPDF 但无需安装它,您可以使用Sprint PDF。这是一项将 HTML 转换为 PDF 的在线服务,通过 RESTful API 进行。

      【讨论】:

      • 当 sprintpdf 决定关闭时,你的项目就死了
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 2022-08-10
      • 2011-07-17
      • 1970-01-01
      相关资源
      最近更新 更多