Yii2:如何在mPDF中添加模型和模型id


Yii2: how to add model and model id in mPDF

我需要从我的视图生成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,
        ]);
    } 

在您的actionInvocePrint中,您没有为分配$model

下面是我的工作pdf(kartik mPDF像你的)的样本

    $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]);

完整的工作解决方案

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视图触发控制器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'
]);
?>