使用mPDF将php文件转换为pdf文件

使用mPDF将php文件转换为pdf文件,php,mpdf,Php,Mpdf,我刚刚开始使用mPDF。我一开始就被卡住了。我试图包含我的动态php文件,并使用mPDF将其转换为pdf文件。这是我的方法:这是我将文件转换为pdf的函数 <?php include('MPDF57/mpdf.php'); include('template1.php'); $html= "template1.php"; $mpdf=new mPDF(); $mpdf->SetDisplayMode('fullpage'); $mpdf->WriteHTML(file_g

我刚刚开始使用mPDF。我一开始就被卡住了。我试图包含我的动态php文件,并使用mPDF将其转换为pdf文件。这是我的方法:这是我将文件转换为pdf的函数

<?php 

include('MPDF57/mpdf.php');
include('template1.php');
$html= "template1.php";
$mpdf=new mPDF(); 
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML(file_get_contents($html));
$mpdf->Output('result.pdf','F');
exit;

?>


My template.php文件只是一个html发票表布局,其中有几个内容来自数据库,如:用户地址、发票表等。。。我确实希望将template.php文件中的html布局和内容转换为pdf


但它不会将文件输出为pdf文件。这里缺少什么?

如果
template1.php
中包含php代码,则
文件获取内容
函数不会执行它,因为它会将文件内容作为常规文本读取。您需要在包含之前打开输出缓冲区,获取缓冲区的内容并将其用于生成pdf。大概是这样的:

<?php 

include 'MPDF57/mpdf.php';
ob_start();  // start output buffering
include 'template1.php';
$content = ob_get_clean(); // get content of the buffer and clean the buffer
$mpdf = new mPDF(); 
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($content);
$mpdf->Output('result.pdf'); // output as inline content

您想转换.php文件的源文件还是.php文件生成的内容?My template.php文件只是一个html发票表布局,其中有几个内容来自数据库:用户地址、发票tble等等。。。我确实希望将html布局和内容转换为pdf
,其中有多个内容来自数据库作为
,因此,它有一个应该执行的php代码
file\u get\u contents($html)
按原样读取文件,而不运行其中的.php代码。在包含('template1.php')之前使用
'ob_start()
,在之后获取缓冲区的内容,如果需要,输出并将其转换为pdf。您可以添加代码吗?
ob_start();包括('template1.php')$content=ob_get_clean()。。。您的代码最多$mpdf->WriteHTML($content)
ob\u get\u clean()
似乎失败了,但是
ob\u getContents()
起作用了,但是clear函数失败了?@BarclayVision try
ob\u end\u clean()
ob\u get\u contents()
之后,这就是我在新问题中尝试询问的内容-类似的代码,但未能返回PDF@BarclayVision检查
ob_get_level()
提供给您的内容-可能包括添加额外缓冲级别的文件。同时禁用缓冲,检查您将从include进入浏览器的输出。
ob\u get\u level()
gives:HTML包含无效的UTF-8字符2mPDF错误: