mPDF 结果来自 PHP,通过缓冲区使用变量

mPDF results from PHP with variables via buffer

似乎无法让它工作,缓冲区中的所有内容都显示但没有生成 PDF,如果它全部在 php 中作为 $html = 'Hello World' 它生成正常吗?

-- 修改后的问题 -- 包含在生成的 PDF 中显示证书背景图像的示例代码 - 结果为空白 PDF...

div.php运行 的屏幕截图。

-- 修改后的整个代码示例 --

<?php error_reporting(E_ALL);?>
<?php 
include $_SERVER['DOCUMENT_ROOT'].'/TestCert/mpdf/mpdf.php';
$mpdf=new mPDF();
//include ('div.html'); 
ob_start();  
include ('div.php'); 
?>


<?php 
$html = ob_get_contents();
ob_end_clean();
// LOAD a stylesheet 1
$stylesheet = file_get_contents('style.css');
$mpdf->WriteHTML($stylesheet,1);    // The parameter 1 tells that this is css/style only and no body/html/text
$mpdf->WriteHTML($html,2);
$mpdf->Output();
exit; 
?>

--CSS--

H1 {
font-size:14pt;
font-family:arial;
text-align: center;
}

H2 {
font-size:14pt;
font-family:arial;
text-align: center;
}

.certificate {
background-image: url("cert.png");
background-size: 100% 100%;
height: 594px;
width: 1056px;
padding: 50px;
position: relative;
}

.certDate {
font-size:14pt;
font-family:Trebuchet MS;
font-weight:normal; 
position: absolute;  
bottom: 50px; 
right: 170px;   
}

.certHead {
font-size:40pt;
font-family:Trebuchet MS; 
position: absolute; 
top: 130px; 
left: 0; 
width: 100%;  
}

.certBody {
font-size:28pt;
font-family:Trebuchet MS;
font-weight:normal;
position: absolute; 
top: 220px; 
left: 0; 
width: 100%;  
}

--div.php--

<?php
global $current_user;
$FName = "john";
$LName = "doe";
$FullName = "John Doe";
?>  

<HTML>
<BODY>

<SCRIPT>

var namedata = <?php echo json_encode($FName . ' ' . $LName); ?>;

    document.write("<div class='certificate'>");
    document.write("<H2 class='certHead'>Certificate of Completion</H2>");
    document.write("<H2 class='certBody'>This Certifies That</br>");
    //document.write("" + "John Doe" + "<br><br>");
    document.write("" + namedata + "<br></br>");
    document.write("Has Successfully Completed The<br>");
    document.write("<I>" + "Triage System Course" + "</I></H2></br>");
    document.write("<H2 class='certDate'>September 11, 2015</H2>");
    document.write("</div>");   
    document.write('<center> <input type=button onClick="window.print()" value="Print This Page"/> </center>');

    </SCRIPT>

</BODY>
</HTML>

您正在使用 Javascript 生成内容。这由您的浏览器解释,而不是由 mPDF 解释(它只是将 HTML+CSS 转换为 PDF)。

尝试在没有 Javascript 的情况下在 PHP 中生成 HTML,应该可以。

我唯一能解决的问题是将样式内嵌在 <div> 中而不是从外部 style.css 文件中调用它...不知道为什么一个比另一个好,但是mPDF 似乎不喜欢这个 class 的风格(其他的都很好...)

这个有效:

<div style = "background-image: url('cert.png'); 
    background-size: 100% 100%; 
    height: 594px; 
    width: 1056px; 
    padding: 50px; 
    position: relative;">
Hello World
<div>

这不是:

<div class="certificate">
Hello World
</div>