Yii 2.0: Yii2 and Pdf files with Mpdf


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.

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>');

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 :)

Total 2 comments

#19251 report it
hajdar at 2015/04/28 05:17am
Real example from active record

Thank you for the package Would you please provide a real example from active record.

#18423 report it
claudio.ferrari at 2014/10/27 06:19pm
Good job

Hi, thank you for sharing it; good job !

Leave a comment

Please to leave your comment.

Write new article