How to Generate a PDF in Yii2 Using Mpdf

  Add to Bookmark

This tutorial will show you how to generate a styled PDF using Mpdf in Yii2, entirely from the controller — no changes to the global config.


Step 1: Install Mpdf

Run the following command to install Mpdf via Composer:

composer require mpdf/mpdf

Step 2: Add the PDF Action in Controller

Open your controller (e.g., SiteController.php) and add this action:

public function actionReport()
{
    $mpdf = new \Mpdf\Mpdf();

    $html = $this->renderPartial('_pdf_content');

    $mpdf->DefHTMLFooterByName(
        'Customfooter',
        '<table style="width:100%;vertical-align:top;border:none; border-top:3px solid #203864; margin-bottom:1px;text-align:center">
            <tr>
                <td style="vertical-align: top;text-align:left;width:30%">
                    <img width="130px" style="padding-top:1px;padding-bottom:2px; padding-left:10px;" src="https://dynamicduniya.com/assets/img/logo.png">
                </td>
                <td style="font-size:20px; vertical-align: middle;width:30%;"></td>
                <td style="font-size:20px; vertical-align: middle;width:30%; text-align:right; font-weight:normal; padding-right:55px;">{PAGENO}</td>
            </tr>
        </table>'
    );

    $mpdf->SetProtection(['print']);
    $mpdf->SetAuthor("Dynamic Duniya");
    $mpdf->SetTitle('First PDF');
    $mpdf->mb_enc = "utf=8";
    $mpdf->allow_charset_conversion = false;

    $mpdf->WriteHTML("p, td { font-family: freeserif; }", \Mpdf\HTMLParserMode::HEADER_CSS);
    $mpdf->WriteHTML($html);

    $mpdf->Output('First PDF File.pdf', 'I');
    exit;
}

Step 3: Create the PDF View File

Create a view file named _pdf_content.php inside @app/views/site/ and add the following content:


<?php
/* @var $this yii\web\View */
?>
<body style="background-color:#14344d;">

    <h1 style="text-align: center; margin-top: 150px; color:white;">
        Congratulations! You Created Your First PDF
    </h1>

    <!-- Page Break -->
    <div style="page-break-before:always">&nbsp;</div>

    <!-- Second Page Content with Custom Footer -->
    <h1 style="text-align: center; margin-top: 150px; color:white;">
        This is Second Page With Custom Footer
    </h1>
    
    <sethtmlpagefooter name="Customfooter" value="1" />

</body>

What You Learn in This Tutorial

1. How to Add Page Break in Mpdf

To add a new page in your PDF, simply use this inside your HTML:

<div style="page-break-before:always">&nbsp;</div>

This ensures that the content after this tag appears on a new page.


2. How to Add Custom Footer in Mpdf

Define a footer using:

$mpdf->DefHTMLFooterByName('Customfooter', '...HTML CONTENT...');

And include it in the HTML using:

<sethtmlpagefooter name="Customfooter" value="1" />

This will display the footer on that page and all following pages.


Step 4: View Your PDF

Open this URL in your browser to view the result:

http://localhost:8080/index.php?r=site/report

You now have a PDF with multiple pages, custom styling, and a dynamic footer.