Yii2: how to add model and model id in mPDF

北城以北 提交于 2019-12-13 19:02:14

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!