【发布时间】:2018-03-21 20:35:27
【问题描述】:
当我生成 pdf 时,内联 php 脚本没有执行,只有 html 部分被打印在 pdf 上。我已经设置了 $isPhpEnabled =True 。
<?php
// include autoloader
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$abc="This is the php text";
$html = <<<'ENDHTML'
<html>
<head>
<style>
h1{color:#555555;width:100%;border-bottom:2px solid powderblue;}
</style>
</head>
<body>
<h1 >Name : <script type="text/php"> echo $abc; </script></h1>
</body>
</html>
ENDHTML;
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream("dompdf_out.pdf", array("Attachment" => false));
exit(0);
?>
【问题讨论】:
-
你的意思是
$abc没有打印吗? -
@ParitoshMahale 是的。
-
@ParitoshMahale 如何在 html 代码中执行脚本,因为我想生成动态内容?
-
类似
$a = array('one','two','three','four','five'); $abc="This is the php text"; $html = " <html> <head> <style> h1{color:#555555;width:100%;border-bottom:2px solid powderblue;} </style> </head> <body> "; foreach($a as $value){ $html .= '<h1>'.$value.'</h1>'; } $html .='</body></html>'; echo $html; -
嵌入式(内联)脚本不会创建文档内容。它的主要目的是提供一种在 PDF 渲染过程中访问 PDF 后端(CPDF、PDFLib)的方法。但我们建议您避免使用嵌入式脚本,因为它会打开一个潜在的安全漏洞,并且您可以使用其他方法进行几乎相同的操作。根据您的描述,您应该只生成 HTML,然后将最终文档提供给 Dompdf。
标签: php dompdf html-to-pdf