Yii2 and Pdf files with Mpdf

You are viewing revision #1 of this wiki article.
This is the latest version of this article.

I am still new to composer and packagist, so I want to share how I am creating the pdf files because for me was not as simple as it should be.

First of all we will intall mpdf with composer :

php composer.phar require mpdf/mpdf "dev-master"

Now, in a controller we will call / use / import the library. In this case for testing purpose we will use the SiteController controller.

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;

#We will include the pdf library installed by composer
use mPDF;


class SiteController extends Controller {

Now, lets create and action for testing our library.

public function actionPdf() {
         
        $mpdf = new mPDF;
        $mpdf->WriteHTML('<p>Hallo World</p>');
        $mpdf->Output();
        exit;
    }

Now if we access our action through a website we should have a pdf as the following :

Pdf Result yii2 mpdf

Hope that this works for you :)