DOMPDF:内联 PHP 脚本不起作用

发布于 2024-10-21 19:56:25 字数 1188 浏览 1 评论 0原文

我的 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

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

I've a problem with DOMPDF. I'm running PHP 5.3.3 and have the newest DOMPDF 0.6.0 beta2.

I've created this HTML template:

<!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>

And this file to render the 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");
}
?>

In the dompdf_config.inc.php I've set DOMPDF_ENABLE_PHP to true, but still either the test-string or the number of pages is displayed in my rendered PDF. Why?

I've compressed my small example here:
http://uploads.dennismadsen.com/pdf.zip

Please tell me if you need further information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄意 2024-10-28 19:56:25

您已经回答了自己的问题,即为什么代码不能按预期工作。幸运的是,从 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 的计数器,带有页面计数器默认实现。只需使用与上面类似的样式/元素组合即可。

注意:我们尚未找到使用此方法提供总页数的方法

You've answered your own question as to why the code does not work as expected. Luckily, there is a way to produce what you want in DOMPDF 0.6.0 as of beta 2. Try out the following code (no need for inline script):

<!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>

As you can see DOMPDF now supports CSS-based counters, with a page counter implemented by default. Just use a style/element combination similar to the above.

Note: we haven't yet worked out a way to provide the total number of pages using this method.

单挑你×的.吻 2024-10-28 19:56:25

请参阅此错误报告

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

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

See the attachment named issue121.php on this bug report

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

Worked for me to draw page 1 of x in the footer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文