我将wkhtmltopdf与php一起使用,并且效果很好。现在,我想将一些变量从php文件添加到html,但是我没有找到解决方案。
PHP文件:
<?php
require '/path/vendor/autoload.php';
use mikehaertl\wkhtmlto\Pdf;
$pdf = new Pdf(array(
'no-outline',
'margin-top' => 0,
'margin-right' => 0,
'margin-bottom' => 0,
'margin-left' => 0,
// Default page options
'disable-smart-shrinking',
));
if($_GET['file'] == 'public'){
$pdf = new Pdf('template_public.html');
}else{
$pdf = new Pdf('template_pro.html');
}
$pdf->send();
?>
HTML文件:
<html>
<head>
<title>Generated PDF file</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<div>
{title}
</div>
</body>
</html>
如何替换HTML文件中的{title}?
在给定参数数组的情况下,这应该用其值替换html文件中的所有{variables}。
$params = ["title"=>"title_val", "content" => "something", "key" => "value"];
$pdf = new Pdf();
$html = file_get_contents($templateFile);
foreach($params as $key=>$value) {
$html = str_replace("{".$key."}", $value, $html);
}
//Renders the pdf directly from the html string, instead of loading the file directly
$pdf->addPage($html);
$pdf->send();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句