Yii2 Tips 19 : Membuat Output View ke PDF
Membuat output pdf menggunakan kartik yii2 mpdf : Langkah 1 : Install yii2-mpdf extension Pada folder utama aplikasi, ketik
1 |
composer require kartik-v/yii2-mpdf "*" |
Tunggu sampai instalasi selesai Langkah 2 : Menambahkan action pada Controller Disini kita akan menambahkan actionReport pada SiteController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public function actionReport(){ $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'=>['PDF Header'], 'SetFooter'=>['{PAGENO}'], ] ]); // return the pdf output as per the destination setting return $pdf->render(); } |
Jangan lupa menambahkan
1 |
use kartik\mpdf\Pdf; |
Langkah 3 : Menambahkan File View Pada contoh ini kita akan membuat Read more about Yii2 Tips 19 : Membuat Output View ke PDF[…]