Yii2 pdf
- By Preneesh AV --
- 19-Feb-2018 --
- 37 Comments
Yii 2.0: yii2-mpdf A great PDF component for Yii2 based on the mPDF library. The yii2-mpdf extension is Yii2 wrapper for the mPDF library. The mPDF library helps to generate PDF files from UTF-8 encoded HTML. Installation The preferred way to install this extension is through composer. Either run: $ php composer.phar require kartik-v/yii2-mpdf"*" or add: "kartik-v/yii2-mpdf": "*" to the require section of your composer.json file. Usage 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 kartik\mpdf\Pdf; public function actionReport() { // get your HTML raw content without any layouts or scripts $content = $this->renderPartial('_reportView'); // setup kartik\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/kartik-v/yii2-mpdf/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(); }
