How to Generate a PDF in Yii2 Using Mpdf
Add to BookmarkThis 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"> </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"> </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.


Prepare for Interview
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
- Debugging in Python
- Unit Testing in Python
- Asynchronous Programming in PYthon
- Multithreading and Multiprocessing in Python
- Context Managers in Python
- Decorators in Python
Random Blogs
- Quantum AI – The Future of AI Powered by Quantum Computing
- How to Become a Good Data Scientist ?
- Where to Find Free Datasets for Your Next Machine Learning & Data Science Project
- Understanding HTAP Databases: Bridging Transactions and Analytics
- Why to learn Digital Marketing?
- What is YII? and How to Install it?
- How to Start Your Career as a DevOps Engineer
- Understanding OLTP vs OLAP Databases: How SQL Handles Query Optimization
- Store Data Into CSV File Using Python Tkinter GUI Library
- Downlaod Youtube Video in Any Format Using Python Pytube Library
- Government Datasets from 50 Countries for Machine Learning Training
- Extract RGB Color From a Image Using CV2
- AI Agents & Autonomous Systems – The Future of Self-Driven Intelligence
- Exploratory Data Analysis On Iris Dataset
- Generative AI - The Future of Artificial Intelligence
Datasets for Machine Learning
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset