DOMPDF:内联 PHP 脚本不起作用
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经回答了自己的问题,即为什么代码不能按预期工作。幸运的是,从 beta 2 开始,有一种方法可以在 DOMPDF 0.6.0 中生成您想要的内容。尝试以下代码(不需要内联脚本):
如您所见,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):
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.
请参阅此错误报告
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