问题
I need to generate pdf from my view, Im using kartik mPDF,
Controller :
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();
}
Getting error Undefined variable: model
. I tried as shown above but getting same error.
View:
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:
In your actionInvocePrint you don't have a $model assigned
below is a sample of a my working 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,
As you can see in this portion of code ..the model is obtained before all and then with this $model is defined a renderPartial with the proper view ..
for passing the $id you action should be
public function actionInvoicesPrint($id)
and for this you URL call should be
Url::to(['/your-controller/Invoices-print' , 'id' => $your_id]);
回答2:
Complete Working Solution
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();
}
IN view to trigger controller 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'
]);
?>
来源:https://stackoverflow.com/questions/36502971/yii2-how-to-add-model-and-model-id-in-mpdf