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
$mPDF1->Output();
# HTML2PDF has very similar syntax
$html2pdf = Yii::app()->ePdf->HTML2PDF();
$html2pdf->WriteHTML($this->renderPartial('index', array(), true));
$html2pdf->Output();
$dataProvider=new CActiveDataProvider('Course');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
<?php
/* @var $this CourseController */
/* @var $model Course */
$this->breadcrumbs=array(
'Courses'=>array('index'),
'Manage',
);
$this->menu=array(
array('label'=>'List Course', 'url'=>array('index')),
array('label'=>'Create Course', 'url'=>array('create')),
);
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$('#course-grid').yiiGridView('update', {
data: $(this).serialize()
});
return false;
});
");
?>
<h1>Manage Courses</h1>
<p>
You may optionally enter a comparison operator (<b><</b>, <b><=</b>, <b>></b>, <b>>=</b>, <b><></b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?><br/>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); ?>
</div><!-- search-form -->
<br/>
<?php //the id is to print the following data ?>
<div id="PRINT">
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'course-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'code',
'code2',
'name',
'type',
'credit_hour',
/*
'prerequisite',
'sem_session',
'total_student',
'day',
'start',
'end',
*/
array(
'class'=>'CButtonColumn',
),
),
)); ?>
</div>
<?php
$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
'.summary',
),
'publishCss' => true, //publish the CSS for the whole page?
'id' => 'PRINT_BUTTON_ID',
));
?>