Convert HTML To PDF In PHP Using Mpdf

By : PHPErrorCode July 18, 2017 1283 Views php

In this tutorial you will learn how to easily generate PDF file on html file in your PHP applications.

I need to generate a pdf file in PHP. There are several PHP libraries which can be used to generate PDF. But after a little research I found an interesting library, mPDF which can convert HTML code to PDF keeping the structure.

mPDF is a PHP class which generates PDF files from UTF-8 encoded HTML. It is based on FPDF and HTML2FPDF with a number of enhancements.

mPDF is a smart library which consider the CSS attached to the HTML. Not only CSS, mPDF takes care of almost all the HTML tags like, form tags, tables, images, lists etc...

Step 1 download mPDF library on this Link... Download

Extract the download file into your folder

Step 2 Create A New File test.php And Place The Code Below Into It.


$pdfFileName = 'demo.pdf';
<!DOCTYPE html>
    <title>html 2 PDF</title>
    <style type="text/css">
	    text-align: center;
	    border: 1px solid
	    <td colspan="2">html to pdf demo</td>
$HTMLoutput = ob_get_contents();

$mpdf = new mPDF();

In this tutorial, If you need any kind of assistance regarding generating PDF using mPDF about the code or would like to contribute to the discussion, please leave a comment below.