pszirmai / mpdf
yii2 mpdf extension
0.1
2025-03-10 09:24 UTC
Requires
- php: >=7.4.0
- mpdf/mpdf: ^8.2
This package is not auto-updated.
Last update: 2025-07-29 08:27:23 UTC
README
Widget Like Usage
The component can be used straightforward in a manner similar to any widget to render your HTML content as PDF. For example, you can call the component simply like below in your controller action:
use pszirmai\mpdf\Pdf;
public function actionReport() {
// get your HTML raw content without any layouts or scripts
$content = $this->renderPartial('_reportView');
// setup pszirmai\mpdf\Pdf component
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_CORE,
// A4 paper format
'format' => Pdf::FORMAT_A4,
// portrait orientation
'orientation' => Pdf::ORIENT_PORTRAIT,
// stream to browser inline
'destination' => Pdf::DEST_BROWSER,
// your html content input
'content' => $content,
// format content from your own css file if needed or use the
// enhanced bootstrap css built by Krajee for mPDF formatting
'cssFile' => '@vendor/pszirmai/yii2-mpdf/src/assets/kv-mpdf-bootstrap.min.css',
// any css to be embedded if required
'cssInline' => '.kv-heading-1{font-size:18px}',
// set mPDF properties on the fly
'options' => ['title' => 'Krajee Report Title'],
// call mPDF methods on the fly
'methods' => [
'SetHeader'=>['Krajee Report Header'],
'SetFooter'=>['{PAGENO}'],
]
]);
// return the pdf output as per the destination setting
return $pdf->render();
}
Global Component
You can also setup the widget as a global component for use across your application with defaults preset. For example, setup the following in the components section of your Yii application configuration file:
use pszirmai\mpdf\Pdf;
// ...
'components' => [
// setup Krajee Pdf component
'pdf' => [
'class' => Pdf::classname(),
'format' => Pdf::FORMAT_A4,
'orientation' => Pdf::ORIENT_PORTRAIT,
'destination' => Pdf::DEST_BROWSER,
// refer settings section for all configuration options
]
]
Once you have setup the component, you can refer it across your application easily:
$pdf = Yii::$app->pdf;
$pdf->content = $htmlContent;
return $pdf->render();