下面的代码片段显示了“使用DOMPDF来呈现PDF”。在这里,我有两个问题:
1.在哪里添加CSS代码?
2.如何使HTML代码在单个代码之外(参见$html变量)?
提前感谢,非常感谢您的帮助。
<?php
require_once("dompdf_config.inc.php");
$html =
'<html><body>'.
'<p>Hello World!</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("hello_world.pdf");
?>发布于 2011-08-10 18:09:04
DOMPDF像网页浏览器一样读取HTML。因此,如果您想在上面的代码示例中添加一些样式,可以创建一个head部分:
$html =
'<html><head>' .
'<style>body { font-family: sans-serif; font-size: 150%; }</style>' .
'</head><body>'.
'<p>Hello World!</p>'.
'</body></html>';或在文件中内联:
$html =
'<html><body style="font-family: sans-serif; font-size: 150%;">'.
'<p>Hello World!</p>'.
'</body></html>';发布于 2011-08-11 13:18:21
您可以使用模板引擎(Smarty)或其他方式动态生成HTML文件。如果使用Smarty,那么$smarty->fetch(模板文件名)将生成html代码,这可以用来创建pdf。在使用CSS时要小心,因为页面中必须提到所有css。
发布于 2012-09-17 08:22:35
可以使用如下样式的css创建html变量,并使用它生成pdf
<?php
$html = <<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
<style>
body,html{
margin: 0px;
padding: 0px;
color:#272727;
font-size:14px;
}
body,html
{
font-family: 'dejavu sans';
line-height:0.9;!important
font-size:14px;
}
</head>
<body>
<div>
</div>
</body></html>
EOF;
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("filename.pdf");
?>https://stackoverflow.com/questions/6872498
复制相似问题