如何在mPDF中添加模型和模型ID

如何在mPDF中添加模型和模型ID

本文介绍了Yii2:如何在mPDF中添加模型和模型ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我需要使用kartik mPDF从我的观点生成pdf,

I need to generate pdf from my view, Im using 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.我尝试了如上所示的操作,但收到了相同的错误.

Getting error Undefined variable: model. I tried as shown above but getting same error.

查看:

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

In your actionInvocePrint you don't have a $model assigned

以下是我的工作pdf示例(像您一样的kartik mPDF)

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,

正如您在代码的这一部分中所看到的那样.首先获取模型,然后使用此$ model定义一个具有正确视图的renderPartial ..

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 ..

要传递$ id,您的操作应为

for passing the $id you action should be

public function actionInvoicesPrint($id)

为此,您的URL调用应为

and for this you URL call should be

  Url::to(['/your-controller/Invoices-print' , 'id' => $your_id]);

这篇关于Yii2:如何在mPDF中添加模型和模型ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:33