Yii2:如何在 mPDF 中添加模型和模型 id
Posted
技术标签:
【中文标题】Yii2:如何在 mPDF 中添加模型和模型 id【英文标题】:Yii2: how to add model and model id in mPDF 【发布时间】:2016-04-08 15:02:02 【问题描述】:我需要根据我的观点生成 pdf,我使用的是 kartik mPDF,
控制器:
public function actionInvoicesPrint()
$pdf = new Pdf([
'mode' => Pdf::MODE_CORE, // leaner size using standard fonts
'content' => $this->renderPartial('view', ['model' => $model, 'mode' => Pdf::MODE_CORE,
'format'=> Pdf::FORMAT_A4,
'orientation'=> Pdf::ORIENT_POTRAIT,
'destination'=> Pdf::DEST_BROWSER]),
]);
return $pdf->render();
收到错误Undefined variable: model
。我尝试如上所示,但得到同样的错误。
查看:
public function actionView($id)
$searchModel = new InvoicesSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$data = Invoices::findOne($id);
return $this->render('view', [
'model' => $this->findModel($id),
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
'data' => $data,
]);
【问题讨论】:
【参考方案1】:在您的 actionInvocePrint 中,您没有分配 $model
下面是我的工作 pdf 示例(kartik mPDF like your )
$model = $this->findModel($id);
// get your html raw content without any layouts or scripts
//$this->layout = 'pdfmain';
$content = $this->renderPartial('_mpdf_report_scheda', [
'model' => $model,
'imglink' => $imglink,
]);
if ($print )
$setJS = 'this.print();';
else
$setJS ='';
// setup kartik\mpdf\Pdf component
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_BLANK,
// 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,
正如您在这部分代码中看到的......首先获得模型,然后使用这个 $model 定义一个具有正确视图的 renderPartial ..
对于传递 $id 你的操作应该是
public function actionInvoicesPrint($id)
为此,您的 URL 调用应该是
Url::to(['/your-controller/Invoices-print' , 'id' => $your_id]);
【讨论】:
我可以在我的actionView()
中使用它而不创建actionInvoicesPrint()
吗?
因为你已经在类似的问题中提到了这个建议。我无法在actionInvoicesPrint()
中分配模型id
因为这个得到错误Undefined variable: id
如何分配id
为actionInvoicesPrint()
【参考方案2】:
完整的工作解决方案
public function actionInvoicesPrint($id)
$model = $this->findModel($id);
$searchModel = new InvoicesSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$data = Invoices::findOne($id);
$content = $this->renderPartial('view', ['model' => $model,'dataProvider' => $dataProvider,'searchModel' => $searchModel,'data'=> $data]);
$pdf = new Pdf([
// set to use core fonts only
'mode' => Pdf::MODE_BLANK,
// 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,
]);
return $pdf->render();
在视图中触发控制器actionInvoicesPrint()
<?php
echo Html::a('<i class="fa glyphicon glyphicon-hand-up"></i> Privacy Statement', ['/invoices/invoices-print', 'id' => $model->id], [
'class'=>'btn btn-danger',
'target'=>'_blank',
'data-toggle'=>'tooltip',
'title'=>'Will open the generated PDF file in a new window'
]);
?>
【讨论】:
以上是关于Yii2:如何在 mPDF 中添加模型和模型 id的主要内容,如果未能解决你的问题,请参考以下文章
如何在yii2中向kartik yii-mpdf添加自定义字体