How to use mPDF In Yiiframework 2

Hello there, Today I would like to show you how to integrate mPDF library with Yii framework 2.First, let me give you some basic information about mPDF. mPDF is a PHP Class by using this we can easily generate PDF files from HTML templates so you can style your HTML template as you wish. Here are the simple steps to use mPDF with the YII2 framework.

You can install in tow way – using composer or you can install manually.

Install mPDF Using Composer

Require this package in your composer.json by adding "mpdf/mpdf":"*" or you can directly run php composer.phar require mpdf/mpdf "dev-master" or composer require mpdf/mpdf "dev-master" it will update your composer.json and downloads mPDF library.

Next, update Composer from the Terminal

Install mPDF Manually

Download mPDF from : and Extract it to your vendor/ folder of your yii2 application. Now rename and change the file path as mpdf/mpdf/ and See the below image for file structure.

 How to use mPDF In Yiiframework 2

Now go to vendor/composer/autoload_namespaces.php file and add the below line to array

How to use – Create PDF Using mPDF

Below is simple example – you just need to import mPDF by using use mPDF; and the crate object for mPDF. using generated object you call all of mPDF methods.


I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets. Protection Status