How to convert HTML to PDF in PHP using html2pdf


PDF format is one useful format.PDF format that can be used for invoices, ebooks, payment receipts or etc. We can convert HTML and CSS code to PDF. In this tutorial, we will create a PDF from HTML code using PHP. We will learn how to convert HTML to PDF in PHP using the html2pdf library. The html2pdf is a widely used library. You can easily convert HTML and CSS to PDF with html2pdf.

How to convert HTML and CSS to PDF in PHP using html2pdf –

The html2pdf is a readymade library. You have to install the html2pdf library in your project folder. After that, we install the vendor using composer and command line which is a very easy process. We will create another PHP file and will create HTML and CSS code in that file. We will get data (HTML + CSS + PHP ) from the data file.
How to install the html2pdf library -
You can easily install the html2pdf library by following these –
1. Go to the link and download the html2pdf library in a zip file. This is the html2pdf library GITHU link. You can download the source code in the zip file
2. Extract the zip file in the project folder where you want to use it.
3. Rename folder name from html2pdf-master to html2pdf .
4. You have to use Composer to install Html2Pdf.

How to install composer and use of command line in the project folder?

In this step, you will need to install composer. If you want to install composer on your computer then you have to download the composer exe file and install it.
Before downloading composer, make sure you are using a local server or if you are working on the live server then you will not need to download composer.
Now, run a command in the same folder where the PHPMailer folder is available.
1. Search run on your computer and enter cmd.

Run Command Line


2. After this, a command prompt will open.
3. Here, you have to move the project directory.
Like - Commands
cd c:
Now you are in c drive.
cd xampp\htdocs\mypdf
Here, mypdf is a project folder name. You can set your project folder where the html2pdf library is available.
Now, run a command to install the composer

 composer require spipu/html2pdf

After the command is successful, a vendor folder will be created in the project folder.
Let’s create an index file.

index.php

writeHTML($html_code); $html2pdf->output(); ?>

In the code above, we included the vendor.php file from the vendor folder also we included the data.php file. In the data file, we will create HTML, CSS, and PHP code.
After that, we got data (HTML code after executed ) from the data.php and saved it in the $html_code variable.
Let’s create a data.php file –

data.php

      .title < text-size: 30px; background-color: red; padding: 20px; color: #fff; >table < width: 100%; border: 2px solid #ddd; >th, td 

Programming & Development


Website Title Website Logo Website URL Category

This PDF is created with HTML,CSS and PHP code using HTML2PDF .

HTML code to PDF using PHP .


This is computer generated pdf.If you have any issue with it , you can contact on Techno Smarter .

In the code above, you can see that, we created HTML, CSS, and PHP code. You can add more HTML, and CSS as well as PHP code.
This is one of the best way to get data from the other file and save it in a variable. If you don’t want to get data from the data.php file then you can write HTML code in the same index.php file like this –

This is HTML and CSS .'; $html2pdf->writeHTML($html_code); $html2pdf->output(); ?>

Execute the code and you will see that the HTML, CSS, and PHP code execute and convert into the PDF. You can fetch data from the database and display it in PDF using PHP and html2pdf.