简体   繁体   中英

How to see the pdf file contents before creating the pdf file using mpdf

Link to my JsFiddle ( Desired Output )

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.

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. I want to see the generated HTML inside the PDF before it turns out to become a file.

My PHP code to create a PDF :

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

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

You can get more idea about output patterns of mpdf from here https://mpdf.github.io/reference/mpdf-functions/output.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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