mPDF来自PHP,通过缓冲区变量

时间:2015-10-29 12:10:54

标签: php mpdf

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

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

div.php自己运行的屏幕截图。

enter image description here

- 修订的整个代码示例 -

<?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>

2 个答案:

答案 0 :(得分:0)

您正在使用Javascript生成内容。这可以通过浏览器进行解释,但不能通过mPDF解释(只需将HTML + CSS翻译成PDF)。

尝试在没有Javascript的PHP中生成HTML,这应该可行。

答案 1 :(得分:0)

我能够完成工作的唯一解决方案是将样式内联到<div>而不是从外部style.css文件中调用它...不确定为什么一个在另一个上工作但是mPDF没有&似乎喜欢这个风格中的一个类(所有其他的工作正常......)

这有效:

<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>