Create PDF Files Using mPDF In Yii Framework 2.0

mPDF is a PHP class Which is used to create a PDF files from the HTML content in yii2.0.

Install mPDF

Install mPDF Using Composer

To include the mPDF package into yii2.0 application folder, add the following code into 'composer.json' file and run the command 'composer update'. Now you can see the mPDF packages in your application.

    "require": {
        "php": ">=5.4.0",
        "yiisoft/yii2": "*",
        "yiisoft/yii2-bootstrap": "*",
        "yiisoft/yii2-swiftmailer": "*",
In command prompt
composer update
(Or) You can add the mPDF package through the command line using below code. We don't need to add code in composer.json. It will be added automatically When we use this method.

php composer.phar require mpdf/mpdf "dev-master"
Now got to 'vendor/composer/autoload_classmap.php' file, All the preload class will be set on this file.

Install mPDF Manually

First goto the mPDF library path ''. Download the .zip file and unzip it into application 'Vendor' folder. Now change the file path as mpdf/mpdf/ and See the below image for file structure. [caption id="attachment_1818" align="aligncenter" width="908"]mPDF In Yii2.0 mPDF In Yii2.0[/caption] Now go to 'vendor/composer/autoload_namespaces.php' file and add the below line into array

'mPDF' => array($vendorDir . '/mpdf/mpdf'),
Note: Manual configuration will be automatically removed If you update the composer.json. Be careful on composer handling.

Create PDF Using mPDF

Now I will create a function called 'createMPDF' in sitecontroller.php and will make it as pdf file.


use app\models\ContactForm;
use mPDF;
class SiteController extends Controller
	public function actionCreateMPDF(){
		$mpdf=new mPDF();
		//return $this->renderPartial('mpdf');
	public function actionSamplePdf() {
        $mpdf = new mPDF;
        $mpdf->WriteHTML('Sample Text');
	public function actionForceDownloadPdf(){
		$mpdf=new mPDF();
        $mpdf->Output('MyPDF.pdf', 'D');
[caption id="attachment_1820" align="aligncenter" width="580"]MPDF View MPDF View[/caption]
  • Ajith

    need more options regarding mpdf

  • Helly Helly

    can you tell me how can i incluce my customm css and bootstrap.css file in this extension

    • Mpdf not works well with bootstrap! ( i never figure out!) recomend you to read the documentation and make a simple css custom!

      • Alvaro Lordelo

        Or you just need to include it on Mpdf…
        although it can be done on multiple ways….You need to understand partial-render and render
        and to include @media in css…typography etc…

      • Joe

        How to show the view.php be the report.php (contents like in a view, just a little custom) then render to pdf?

  • Helly Helly

    its working fine for the simple text . but when i include a page with dynamic content, then its is not wiorking fine. It doens’t include bootstrap.css and also one more file with custom css. Please help me

  • fucking THANKS!

  • Maxim

    How can I use landscape page orientation?

  • bijan

    not working in live server? Class ‘mPDF’ not found. any help?

  • sonyta son

    i want to create PDF 5000 records how can i do?