yii How to Creating Pdf Report / Excel Report

Published on Author Code Father

Please create new function in your models or you can see in the below:

In your model | e.g. : models/Items.php

public function getMyreport()
        $sql="SELECT * FROM items where CREATED_DATE >= '$from' and CREATED_DATE <= '$until' order by ITEM_ID desc "; // your sql here
        $dataReportItem=new CSqlDataProvider($sql,array(
            'keyField' => 'ITEM_ID',
        return $dataReportItem;

And than you have to add this code in action index.

In your controller | e.g. : controller/ItemsController.php

public function actionIndex(){
if (!Yii::app()->user->isGuest){

            'dataReportItem' =>$ dataReportItem,

                        $mPDF1 = Yii::app()->ePdf->mpdf();
                        ), true));


and now in your view/index.php you can add the code or you can see the code in the below:

In your view | e.g. : view/items/index.php

<form method="post">
                    <td>Date From</td>
                    <td>Date To</td>
                    <td><input type="date" name="from" value="<?=isset($_GET['from']) ? CHtml::encode($_GET['from']) : '' ; ?>" /></td>
                    <td><input type="date" name="until" value="<?=isset($_GET['until']) ? CHtml::encode($_GET['until']) : '' ; ?>" /></td>
        <input type="submit" value="Report to Excel" name="'ExcelReport'">
        <input type="submit" value="Report to Pdf" name="'PdfReport'">

remain in view but you have to create new php files and use this code:

In your view | e.g. : view/items/view_item_report.php

<h3>Items | Periode <?php echo $_REQUEST['from']." - ".$_REQUEST['until'];?></h3>
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
    'fixedHeader' => true,
    'headerOffset' => 40,
    'type' => 'striped',
    'dataProvider' => $dataReportItem,
    'responsiveTable' => true,
    'template' => "{items}",
            'CREATED_DATE', // This is a reference for parms / parameters
<div align="left">
    <b>Printed By : <? echo Yii::app()->user->name;?><br/>
Printed At : <? echo date("d/m/Y H:i:s");?></b>
            <div align="right">Copyright &COPY; <?php echo date('Y'); ?> By Jsource</div>