How to Create PDF file Using mpdf and PHP

PDF is the common format of the file to percentage or study records on web.we’ve already shared a tutorial to convert HTML facts into pdf and excellent pdf viewer.

Now here I am developing a simple Hypertext Preprocessor application which is generating PDF record using the mpdf library. The pdf is wonderful personal homepage library that assists to convert textual content into wealthy UI pdf format using PHP. The mPDF library assist to generates PDF files from UTF-8 encoded HTML.

There are following dependencies are:

  • mPDF 7.0 requires PHP ^5.6 || ~7.0.0 || ~7.1.0 || ~7.2.0
  • PHP mbstring and gd extensions must be enabled.
  • Require PHP zlib module for compression of output and embedded resources such as fonts, bcmath for generating barcodes or XML for character set conversion and SVG handling.

Create PDF File with Watermark Using PHP

We can add watermark textual content within pdf record.The mPDF presenting approach to add watermark text and photo.There is steps method to create pdf using PHP.

Step 1. Download the library from GitHub then extract it and paste into or(xampp/htdoc/projectname/mpdf) use composer command.composer require mpdf/mpdf

Step 2. Create an HTML content and assigned into PHP variable, You can generate dynamic but I am taking as a static HTML content.We will add below content into the fileindex.php.


$html = '<table width="60%" cellspacing="0">
 <td>Tiger Nixon</td>
 <td>Garrett Winters</td>
 <td>Ashton Cox</td>
 <td>Cedric Kelly</td>
 <td>Airi Satou</td>
 <td>Brielle Williamson</td>
 <td>Herrod Chandler</td>
 <td>Rhona Davidson</td>
 <td>Colleen Hurst</td>
 <td>Sonya Frost</td>

Step 3. We will include the library class mPDF at the end of the fileindex.php.


$mpdf = new mPDF();
//call watermark content aand image
$mpdf->showWatermarkText = true;
$mpdf->watermarkTextAlpha = 0.1;
//save the file put which location you need folder/filname
$mpdf->Output("phpflow.pdf", 'F');
//out put in browser below output function

I have created simple PDF file using php and static HTML content.You can modify pdf generation method as per your need.

Please follow and like us:

Leave a Comment

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