domPDF CSS problem
The following code snippet is shown "Using DOMPDF to render PDF". Here,I have two questions:
1.where to add CSS code?
开发者_如何转开发2.how to make HTML code outside single code (see $html variable)?
Thanks in advance and really appreciate for any help.
<?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");
?>
DOMPDF reads HTML just like a web browser. So if you wanted to add some styles to the above code example you could created a head section:
$html =
'<html><head>' .
'<style>body { font-family: sans-serif; font-size: 150%; }</style>' .
'</head><body>'.
'<p>Hello World!</p>'.
'</body></html>';
or inline in the document:
$html =
'<html><body style="font-family: sans-serif; font-size: 150%;">'.
'<p>Hello World!</p>'.
'</body></html>';
you can dynamically generate an HTML file by using template engine(Smarty) or some other way. If Smarty is used then $smarty->fetch(template file name) which will generate the html code, and this can be used to create pdf. Be careful about using CSS, as all the css must be mentioned in the page.
Hai you can make html variable with style css like below and use to generate 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");
?>
Hi like below you can make html variable with style css in 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{
color:#272727;
font-size:14px;
font-family: 'dejavu sans';
line-height:0.9;!important
font-size:14px;
}
</head>
<body>
<div>
This is testing content
</div>
</body></html>
EOF;
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("filename.pdf");
?>
精彩评论