PDF document is a required feature for all modern applications.It is required for invoices,reports or any downloadable content.There are number of available third party libraries for creating PDF from HTML.One of those is the mPDF library.It is the most powerful and popular library which is used.In this guide we will learn how to generate PDF from HTML using PHP.This guide will also teach you how to optimize the performance of your pdf generation process.
Prerequisites
- HTML and PHP Knowledge :HTML and basic php knowledge is required to generate PDF .
2. Local installation of xampp :Local installation is required to test the functionality of PDF generation .
3.Composer : A dependency manager to install mPDF .
Why use mPDF ?
Before we dive in to this tutorial we should know why mPDF is so popular and widely used library.Check the below points for this:
1.Wide HTML/CSS support: It supports all the features of HTML and CSS .So the webpages with complex layouts are easily converted.The converted PDF donot loose any design accuracy.
2.Easy to use :It is simple to setup and use.
3.Multilingual support: It supports utf-8 so PDFs can be generated in Arabic,Hebrew,Chinese and many more.
4.Customizable Output: In PDFs header and footer can be added.Watermarks can also be included in PDFs .
5.Active Community Support : mPDF is widely used.You can find resources and support online if there are issues.
How To Convert Html to Pdf Using PHP
STEP 1: Check composer is installed or not
Let’s learn step by step on how to create PDF from HTML using PHP. mPDF can be installed using composer ,dependency manager.To check that composer is installed in your system follow below steps:
- Open your terminal
- Go to C:/xampp/htdocs using cd command
- write composer and press enter
If you see the below screen your composer is installed.If not goto this link https://getcomposer.org/download/ and download composer .
Screenshot below for checking composer is installed in PC

STEP 2: Install mPDF via composer
- Create a project folder say converthtml inside htdocs folder.
- Run the command cd converthtml in terminal
- Run composer require mpdf/mpdf in terminal
- mPDF will be installed in your project folder converthtml.
See the screenshot below:

Step 3: Create Basic HTML and convert it to PDF
We have installed mPDF.Now we need to create a basic html data.This HTML data will be genrated in PDF
Create index.php pagein project folder.Include mPDF library in it using require_once .Check the below code to include mPDF library in PHP page.
require_once __DIR__ . '/vendor/autoload.php';
Paste below line after the above code to create an object of mPDF.Check the below code:
$mpdf = new \Mpdf\Mpdf();
Create some html data and store it in php variable.Check the code below:
$html = '
<h1>Welcome PHPGURUACADEMY</h1>
<p>This is a basic example of converting HTML to PDF .</p>
<p><b>Bold Text</b>, <i>Italic Text</i>, <u>Underlined Text</u>, and more!</p>
';
Use mPDF library function WriteHTML() to pass html to mPDF.Check the below code
$mpdf->WriteHTML($html);
Now to output the pdf we will use Output() function of mPDF library.D option in function is used to force download the PDF file.Check the below code:
mpdf->Output('myfile.pdf', 'D');
The full code is below
<?php
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
// Your HTML content
$html = '
<h1>Welcome PHPGURUACADEMY</h1>
<p>This is a basic example of converting HTML to PDF .</p>
<p><b>Bold Text</b>, <i>Italic Text</i>, <u>Underlined Text</u>, and more!</p>
';
$mpdf->WriteHTML($html);
// Output the PDF to browser
$mpdf->Output('myfile.pdf', 'D'); // 'I' for inline display or 'D' to force download
Though mPDF is powerful it is resource-intensive when dealing with complex html data.FInd the below tips to optimize performance.
1.Optimize Images: Compress and optimize images to reduce it’s size.This can enhance performance.Large image s can hinder performance.
2.Increase PHP Memory Limit: To work with large documents increase the memory size in ini file of PHP. Code to increase memory limit is below
ini_set('memory_limit', '256M');
3.Keep CSS simple: Avoid inline css in html or deeply nested elemnts.Keep the structure clean.
4.Use Page Breaks: If your PDF is large use page breaks to add paginations and reduce memory usage.
Step 4: Test the code for PDF generation
We have created code for generating PDF.Now we need to test it.
Navigate to http://localhost/converthtml .As this will open in browser the file myfile.pdf willbe downloaded.
Check the screenshot below:

Check the screenshot for the downloaded pdf below:

Congratulations ! You have created PDF file from custom HTML.As you can see above the PDF has maintained css for bold,italics and underline text.So with this tutrial you learn how to create pdf using HTML.Now you can extend it and use complex HTML to generate PDFs.
Happy Coding!