Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 421
ahmednawazbutt Avatar asked Jan 31 '26 10:01

ahmednawazbutt


1 Answers

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

like image 94
Towsif Avatar answered Feb 02 '26 02:02

Towsif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!