简体   繁体   English

如何在使用 mpdf 创建 pdf 文件之前查看 pdf 文件内容

[英]How to see the pdf file contents before creating the pdf file using mpdf

Link to my JsFiddle ( Desired Output )链接到我的 JsFiddle(期望输出

Target : The HTML I get there is exactly what I need in my PDF but when I try to generate a PDF using mPdf library in my codeigniter controller, the Output looks terrible.目标:我得到的 HTML 正是我在 PDF 中需要的,但是当我尝试在我的 codeigniter 控制器中使用 mPdf 库生成 PDF 时, 输出看起来很糟糕。

Problem : I want to find a way to debug what is getting inside this PDF file and what is causing the output to go wrong.问题:我想找到一种方法来调试此 PDF 文件中的内容以及导致输出出错的原因。 I want to see the generated HTML inside the PDF before it turns out to become a file.我想在结果成为文件之前在 PDF 中查看生成的 HTML。

My PHP code to create a PDF :我创建 PDF 的 PHP 代码

$mpdf = new Mpdf();
$style1 = file_get_contents(base_url('public/admin/css/formstyles.css')); // external css
$style2 = file_get_contents(base_url('public/admin/bootstrap/css/bootstrap.min.css'));
$mpdf->WriteHTML($style1,\Mpdf\HTMLParserMode::HEADER_CSS);
$mpdf->WriteHTML($style2,\Mpdf\HTMLParserMode::HEADER_CSS);
$mpdf->WriteHTML($data['template'],\Mpdf\HTMLParserMode::HTML_BODY);
// I want to check the TOBE PDF output so that I can see what is wrong with my content here
$mpdf->Output($path.$company_id.'/'.$template_id.'.pdf', \Mpdf\Output\Destination::FILE); // opens in browser

Any Help is appreciated.任何帮助表示赞赏。

If i can understand you then you can do it as following ( i've checked with this way):如果我能理解你,那么你可以按照以下方式进行(我已经用这种方式检查过):

$mpdf = new Mpdf();
$style1 = file_get_contents(base_url('public/admin/css/formstyles.css')); // external css
$style2 = file_get_contents(base_url('public/admin/bootstrap/css/bootstrap.min.css'));
$mpdf->WriteHTML($style1,\Mpdf\HTMLParserMode::HEADER_CSS);
$mpdf->WriteHTML($style2,\Mpdf\HTMLParserMode::HEADER_CSS);
$mpdf->WriteHTML($data['template'],\Mpdf\HTMLParserMode::HTML_BODY);

// capture the output into buffer
ob_start();
$mpdf->Output($path.$company_id.'/'.$template_id.'.pdf', \Mpdf\Output\Destination::FILE); // opens in browser

// holds the buffer into a variable
$html = ob_get_contents(); 
ob_get_clean();

// creates a html file with contents at root
file_put_contents('htmlFile.html', $html); 

And, If you want to see the output on browser of the pdf without creating a file then you've to use the below code:而且,如果您想在不创建文件的情况下在 pdf 浏览器上查看输出,则必须使用以下代码:

$mpdf->Output($path.$company_id.'/'.$template_id.'.pdf', \Mpdf\Output\Destination::INLINE); // Sends output inline to browser

Or you can also use或者你也可以使用

$mpdf->Output($path.$company_id.'/'.$template_id.'.pdf', "I"); // Sends output inline to browser

So whenever you change anything of the pdf file's code, just refresh the generated pdf in browser, you'll see the changes.因此,每当您更改 pdf 文件的任何代码时,只需在浏览器中刷新生成的 pdf,您就会看到更改。

You can get more idea about output patterns of mpdf from here https://mpdf.github.io/reference/mpdf-functions/output.html你可以从这里得到更多关于 mpdf 输出模式的想法https://mpdf.github.io/reference/mpdf-functions/output.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM