我有一个Web应用程序,它使用CGridView根据用户输入的搜索参数显示$ model中的过滤数据。
我的目标是让用户能够点击按钮将过滤后的数据导出为PDF。
为了实现这一点,我使用了扩展名yii-pdf.
我是否需要传递存储在CActiveDataProvider数组中的信息才能在单独的控制器中使用并查看PDF创建?
使用相同的控制器/操作并将数据渲染到视图中然后可以导出为PDF似乎更容易。但是,唉,我无法弄清楚如何做到这一点。
提前致谢。
_search查看:
<div class="wide form">
<?php
$form = $this->beginWidget('GxActiveForm', array(
'action' => Yii::app()->createUrl($this->route),
'method' => 'get',
));
?>
<!-- Search Fields here -->
<!-- Search and Export to PDF buttons -->
<div class="row buttons">
<?php echo GxHtml::submitButton(Yii::t('app', 'Search')); ?>
<?php echo GxHtml::button(Yii::t('app', 'PDF'), array('id' => 'exportToPdf')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- search-form -->
_pdf查看
<div class="pdfContainer" id="pdfPage">
<div class="pdfHeader">
<img src="images/logo.png" style="float:left; text-align: top;"></img>
<div class="pdfTitle" style="text-align: right; float: right;">
<h3>Service Report</h3>
</div>
</div><!-- header -->
<?php echo "Prepared by: ".Yii::app()->user->name."\n" ?>
<?php **//Possibly echo contents here?** ?>
<div class="clear"></div>
<div class="pdfFooter">
</div><!-- footer -->
</div><!-- page -->
</div>
控制器/动作
public function actionPdf(){
$this->layout='pdf';
/* mPDF */
$mPDF1 = Yii::app()->ePdf->mpdf();
/* render (full page) */
$mPDF1->WriteHTML($this->render('pdf', array(), true));
/* renderPartial (only 'view' of current controller) */
$mPDF1->WriteHTML($this->renderPartial('pdf', array(), true));
/* Outputs ready PDF */
$mPDF1->Output();
}
管理员查看:
<?php
Yii::app()->clientScript->registerScript('search', "
$('#exportToPdf').click(function(){
window.location = '". $this->createUrl('Weeklyservicereport/pdf') . "?' + $(this).parents('form').serialize() + '&export=true';
return false;
});
$('.search-form form').submit(function(){
$('#weeklyservicereport-grid').yiiGridView('update', {
data: $(this).serialize()
});
return false;
});
");
?>
<div class="search-form" style="display:block">
<?php $this->renderPartial('_search', array('model' => $model,)); ?>
</div> <!-- Search Form -->
/* Gridview Widget */
<?php $this->widget('application.components.widgets.tlbExcelView', array(
'id' => 'weeklyservicereport-grid',
'dataProvider' => $model->search(),
.....
.....
'columns'=>array(
/* Column names */
),
));
?>
答案 0 :(得分:3)
你已经非常接近解决方案了,我将在这里写下你可以使用的逻辑。
查看:_pdf_header.php
<div class="pdfHeader">
<img src="images/logo.png" style="float:left; text-align: top;"></img>
<div class="pdfTitle" style="text-align: right; float: right;">
<h3>Service Report</h3>
</div>
</div>
查看:_pdf_footer.php
<div class="pdfFooter">
<h5>Your Footer Page</h5>
</div>
查看:_gridview.php
<?php $this->widget('application.components.widgets.tlbExcelView', array(
'id' => 'weeklyservicereport-grid',
'dataProvider' => $dataProvider,
'columns' => array( /*YOUR COLUMNS */ ),
));
?>
在你的actionPdf中,你只是按照@ineersa:
的建议渲染部分视图public function actionPdf(){
$this->layout = 'pdf';
$model = new Model();
$model->attributes = $_GET['Model']; /* to execute the filters (if is the case) */
$dataProvider = $model->search();
/* if yu want to ignore the pagination and retrieve all records */
$dataProvider->pagination = false;
$mPDF1 = Yii::app()->ePdf->mpdf();
$mPDF1->WriteHTML($this->renderPartial('_pdf_header', array(), true));
$mPDF1->WriteHTML($this->renderPartial('_gridview', array('dataProvider' => $dataProvider), true));
$mPDF1->WriteHTML($this->renderPartial('_pdf_footer', array(), true));
$mPDF1->Output();
}
参考:
http://www.yiiframework.com/forum/index.php/topic/15677-printing-a-cgridview/