How to Create PDF file Using mpdf and PHP

PDF is common format of file to share or read data on web.We have already shared tutorial to convert html data into pdf and best pdf viewer.

Now here I am creating a simple php application which are generating PDF file using mpdf library. The mpdf is awesome PHP library that help to convert text into rich UI pdf format using PHP. The mPDF library help 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 will add watermark text with in pdf file.The mPDF providing method to add watermark text and image.There are two steps process to create pdf using php.

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

Step 2. Create a 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 index.php file.

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

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

You can download source code and see Demo from below link.