【问题标题】:DOMPDF: Inline PHP script not workingDOMPDF:内联 PHP 脚本不起作用
【发布时间】:2011-03-14 10:22:08
【问题描述】:

我对 DOMPDF 有疑问。我正在运行 PHP 5.3.3 并拥有最新的 DOMPDF 0.6.0 beta2。

我已经创建了这个 HTML 模板:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Printed document</title>
</head>
<body>

<script type="text/php">
echo "test";
if ( isset($pdf) ) {
    echo $PAGE_NUM;
}
</script>

</body>
</html>

这个文件用来渲染 PDF:

<?php
require_once("dompdf/dompdf_config.inc.php");

$templateFile = 'template.html';
$content = file_get_contents($templateFile); 
if ($content !== false) {
    $dompdf = new DOMPDF();
    $dompdf->load_html($content);
    $dompdf->render();
    $dompdf->stream("test.pdf");
}
?>

在 dompdf_config.inc.php 中,我已将 DOMPDF_ENABLE_PHP 设置为 true,但在渲染的 PDF 中仍然显示测试字符串或页数。为什么?

我在这里压缩了我的小例子: http://uploads.dennismadsen.com/pdf.zip

如果您需要更多信息,请告诉我。

【问题讨论】:

  • 好吧,看来第二遍不能回显任何变量——而只能使用 PDF 对象绘制文本。不知道我必须插入 PAGE_NUM 的位置,因为它不是页眉或页脚的一部分 - 因此我无法使用绘图。有什么方法可以在文档的自定义位置插入变量?

标签: php dompdf


【解决方案1】:

您已经回答了自己的问题,即为什么代码不能按预期工作。幸运的是,从 beta 2 开始,有一种方法可以在 DOMPDF 0.6.0 中生成您想要的内容。试试下面的代码(不需要内联脚本):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <title>Printed document</title>
  <style>
    .pagenum:before { content: counter(page); }
  </style>
</head>
<body>
  <p>You are currently reading page <span class="pagenum"></span>.</p>
</body>
</html>

如您所见,DOMPDF 现在支持基于 CSS 的计数器,默认实现页面计数器。只需使用类似于上述的样式/元素组合即可。

注意:我们还没有找到一种方法来提供使用这种方法的页面总数。

【讨论】:

    【解决方案2】:

    请参阅此错误报告中名为 issue121.php 的附件

    http://code.google.com/p/dompdf/issues/detail?id=121

    帮我在页脚中绘制 x 的第 1 页

    【讨论】:

      猜你喜欢
      • 2018-03-21
      • 2012-01-31
      • 2011-06-27
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      • 2018-10-23
      • 2011-01-10
      • 2014-05-17
      相关资源
      最近更新 更多