来自PHP的mPDF结果通过缓冲区带有变量 [英] mPDF results from PHP with variables via buffer

查看:89
本文介绍了来自PHP的mPDF结果通过缓冲区带有变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎无法使它正常工作,缓冲区中的所有内容都显示出来,但没有生成PDF,如果它全部在PHP中以$html = 'Hello World'的形式生成,会很好吗?

Cannot seem to get this to work, everything from the buffer is showing but no PDF generated, if its all in the php as $html = 'Hello World' it generates fine?

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

-- REVISED QUESTION -- included example code to display certificate background image in generated PDF - results are blank PDF...

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

screen shot of div.php run by itself.

-修订后的完整代码示例-

-- REVISED ENTIRE CODE EXAMPLE --

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

推荐答案

我能做的唯一解决方法是将样式内联到<div>中,而不是从外部style.css文件中调用它……不是确定为什么一个可以替代另一个,但是mPDF似乎不喜欢这一类的样式(其他所有都可以正常工作...)

only resolution I was able to make work was to put the style inline in the <div> and not call it from external style.css file... not sure why one works over the other but mPDF doesn't seem to like this one class in the style (all the others work fine...)

这有效:

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

这篇关于来自PHP的mPDF结果通过缓冲区带有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆