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 

<?php require 'vendor/autoload.php';
use Spipu\Html2Pdf\Html2Pdf;
$html2pdf = new Html2Pdf();
ob_start();
include('data.php');
$html_code = ob_get_clean();
$html2pdf->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 

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title></title>
	<style type="text/css">
		.title{
			text-size: 30px;
			background-color: red;
		padding: 20px;
		color: #fff;
		}
		table {
    width: 100%;
    border: 2px solid #ddd;
}

th, td {
    text-align: center;
    padding: 8px;
  border: 2px solid #ddd;  
}

	</style>
</head>
<body>
<h1 class="title"><img src="https://technosmarter.com/assets/images/logo.png">Programming & Development </h1>
<hr>
<?php 
	 $website ='Techno Smarter'; 
	 $logo='https://yt3.ggpht.com/ytc/AMLnZu9_N5U7lD9TLzpUbZFx5oCsQM2O7KyPEvHurZwHXg=s176-c-k-c0x00ffffff-no-rj-mo';
	 $url='https://technosmarter.com';
	 $category='PHP';
?> 
<table>
	<tr>
		<th> Website Title</th>
		<th> Website Logo </th>
		<th> Website URL</th>
		<th> Category</th>
		
	</tr>
	<tr>
		<td><?php echo $website ;?></td>
		<td><img src="<?php echo $logo ;?>"></td>
		<td><?php echo $url;?></td>
		<td><?php echo $category;?></td>

	</tr>
</table>
<h2 style="background:yellow;"> This PDF is created with HTML,CSS and PHP code using HTML2PDF ... </h2>
<h2 style="background:yellow;">HTML code to PDF using PHP ...</h2>
<hr>
<h4 style="background:blue;color: #fff;">This is computer generated pdf.If you have any issue with it , you can contact on Techno Smarter ...  </h4>

</body>
</html>

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 – 

<?php require 'vendor/autoload.php';
use Spipu\Html2Pdf\Html2Pdf;
$html2pdf = new Html2Pdf();
$html_code ='<h1 style="color:red;">This is HTML and CSS .</h1>'; 
$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. 


Please Share

Recommended Posts:-


Live Chat

Hello! How can we assist you today?