Create a pdf from php / html using mpdf

This article will explain, how to create a pdf file using mpdf from html / php.

Install php if you are working on local ubuntu machine, on shared hosting mostly php will have been installed.

Below command will install php5 on ubuntu 14.04,

 sudo apt-get install php5

If you are using ubuntu 16.04, it comes with php7, which we can install using below steps,

sudo apt-get install libapache2-mod-php
sudo apt-get install php-mbstring

Download mpdf code from website,

 cd /var/www/html
 git clone https://github.com/lynxbee/mpdf.git

Here, we will try to create a sample example in localhost, so we will need to move this code to localhost,

READ  HTML Tag formatter, CSS Minifier, CSS Formatter, JSON Validator tools

$ vim mpdfexample.php

<?php
include("mpdf/mpdf.php");
$mpdf = new mPDF();
$mpdf->WriteHTML('<h1>Hello world!</h1>');
$mpdf->Output();
?>

Now, open this php file at localhost in browser as,

http://localhost/mpdfexample.php

Leave a Reply

Your email address will not be published. Required fields are marked *