2

我必须用 TCPDF 生成 PDF,我的信息来自我的数据库,所以我必须编写 PHP 变量。我知道该怎么做。

但我想使用以下方式编写 CSS + xHTML(作为我看到的 TCPDF 站点的示例):

$html = <<<EOF

<style>
  <!-- My CSS -->
</style>

  <!-- My xHTML -->

EOF;

$pdf->writeHTML('$html', '');

当我尝试打开 PDF 时出现错误。我读过我必须用'\'转义'$'。在此之后,我没有错误,但我的变量的内容没有出现在我的 PDF 中。

编辑 :

@Sarfraz:如果我这样做,我不知道该怎么做,它不起作用?

$html = <<<EOF

    <style>
      <!-- My CSS -->
    </style>

      <!-- My xHTML -->
      <span><?php echo $myVar; ?></span>

    EOF;

    $pdf->writeHTML('$html', '');
4

4 回答 4

4

要获得更精细的结果:

输出最终的 PDF:
当您创建完所有必需的单元格、图像、链接、文本等后,您必须调用该Output()方法才能真正获得动态创建的 PDF。这可以不带任何参数,在这种情况下将 PDF 发送到浏览器,但更常见的是,开发人员指定生成的 PDF 的文件名和目的地。目的地可以是四个值之一,它们是:

I: send the file inline to the browser.  
D: send to the browser and force a file download with the name given by name.  
F: save to a local file with the name given by name.  
S: return the document as a string.

您可以看到我的代码将目标值设置为F:

$pdf->Output(”./pdfs/example.pdf”, “F”);

引用自:http ://www.akamarketing.com/blog/109-php-to-pdf-conversion-with-tcpdf.html

于 2011-08-19T20:39:12.073 回答
2

确保没有空格和缩进,EOF;否则会出错。

{}此外,如果您在 heredoc 语法转义中有任何变量,请将它们放入{$somevar}

于 2010-08-04T08:12:11.313 回答
2

我有同样的问题。我通过试验自己提到的事情得到了解决方案,如下所示:

请使用连接将 $html 字符串分成几部分。这肯定会解决问题。例如我这样使用:

$html = 'HTML CONTENT BREAKS HERE'.$variable_name.'HTML CONTENT CONTINUES HERE';

通常,大多数开发人员会在 $html 值内使用 PHP 变量,

$html = 'HTML CONTENT echo php variable HTML CONTENT';
于 2011-08-16T21:30:34.847 回答
1

Instead of EOF tags , make use of single(') or double quote(") that will also help.

于 2019-01-30T14:23:46.773 回答