how to convert html to pdf in php using Html2Pdf?

Today, We want to share with you how to convert html to pdf in php?.In this post we will show you how to easily convert web pages and raw HTML documents to PDF in your PHP applications., hear for how to convert HTML with CSS styling into a PDF file. we will give you demo and example for implement.In this post, we will learn about html to pdf using php with an example.

Convert HTML To PDF In PHP

here simple example for “Follow the steps for convert HTML to PDF in PHP”.

Please note that html2pdf works with PHP versions from 5.6 to 7.4 which gives it a best and easy scope for old any PHP websites too.

Step 1 – Install HTML2PDF
Use Composer to install the package with the bellow command

composer require spipu/html2pdf

Step 2 – Require the package in your PHP code

use Spipu\Html2Pdf\Html2Pdf;

Step 3 – Convert inline HTML to PDF
Add the bellow source code to your function or similar.

Notice there are two main output lines, one of which is commented out, you can add the β€˜D’ parameter to force the download directly, but for this example, We have instructed the PDF to load in the browser like as a chrome first.

$pdfConverter = new Html2Pdf();
$pdfConverter->writeHTML('<h1 style="color:pink;">Pakainfo PDF</h1> <br/> <p>Convert this HTML to PDF please!</p>');
$pdfConverter->output('pakainfo.pdf); // Generate as well as  load the PDF in the browser.

// $pdfConverter->output('pakainfo.pdf, 'D'); 

And last step to free Generate the PDF execution and force download direct or immediately.

1) use MPDF !

$mpdf=new mPDF();
$mpdf->WriteHTML('<p style="color:red;">Welcome To Pakainfo<br/>Free Download Tamilrokers new upcooming Movies step by step</p>');
$mpdf->Output();   exit;

2) Use pdfToHtml !


if (!file_exists($dest_fname)) { 
	mkdir($dest_fname, 0777, true);

$result= passthru("pdftohtml $source $dest_fname/new_filename",$log);

I hope you get an idea about Convert HTML to PDF.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.