Cgridview To Pdf

Hi All,

I have been wanting to use a PDF extension so that I can convert my CGridView to PDF and export it. Tried quiet a few extensions like PHPExcel, yii-pdf but I can’t get it working in the right manner.

Any thought on good PDF extensions to use. Please share.




I mostly use yii-pdf works fine for me.

could be something small what exactly you trying to do?


I just tried yii-pdf and it works fine. Good extension and as per what I wanted. Thanks a lot for sharing.


Hi, alirz23. I could not understand how to convert Cgridview to PDF by using yii-pdf. I have follow the instruction as given here and I get stucked. How do I make use of this extension? I wish to click on a button and it will direct me to another page which display the Cgridview in a PDF form. Please guide me. Thanks in advance.

Here’s my code:

public function actionIndex()


	    # mPDF

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

        # You can easily override default constructor's params

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

        # render (full page)

        $mPDF1->WriteHTML($this->render('index', array(), true));

        # Load a stylesheet

        $stylesheet = file_get_contents(Yii::getPathOfAlias('webroot.css') . '/main.css');

        $mPDF1->WriteHTML($stylesheet, 1);

        # renderPartial (only 'view' of current controller)

        $mPDF1->WriteHTML($this->renderPartial('index', array(), true));

        # Renders image

        $mPDF1->WriteHTML(CHtml::image(Yii::getPathOfAlias('webroot.css') . '/bg.gif' ));

        # Outputs ready PDF


        # HTML2PDF has very similar syntax

        $html2pdf = Yii::app()->ePdf->HTML2PDF();

        $html2pdf->WriteHTML($this->renderPartial('index', array(), true));



		$dataProvider=new CActiveDataProvider('Course');






/* @var $this CourseController */

/* @var $model Course */






	array('label'=>'List Course', 'url'=>array('index')),

	array('label'=>'Create Course', 'url'=>array('create')),


Yii::app()->clientScript->registerScript('search', "



	return false;


$('.search-form form').submit(function(){

	$('#course-grid').yiiGridView('update', {

		data: $(this).serialize()


	return false;




<h1>Manage Courses</h1>


You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>

or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.


<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?><br/>

<div class="search-form" style="display:none">

<?php $this->renderPartial('_search',array(


)); ?>

</div><!-- search-form -->


<?php //the id is to print the following data ?>

<div id="PRINT"> 

<?php $this->widget('zii.widgets.grid.CGridView', array(























)); ?>



    $this->widget('ext.mPrint.mPrint', array(

    //the title of the document. Defaults to the HTML title

    'title' => 'HTML Element To Pdf',


	//tooltip message of the print icon. Defaults to 'print'

    'tooltip' => 'HTML PDF',


	//text which will appear beside the print icon. Defaults to NULL

    'text' => 'Print Results',


	'element' => '#PRINT', //the element to be printed.

    'exceptions' => array( //the element/s which will be ignored



    'publishCss' => true, //publish the CSS for the whole page?

    'id' => 'PRINT_BUTTON_ID',