首页 文章

kartik mpdf中未定义的变量模型 - yii2

提问于
浏览
0

我'm trying to use kartik mpdf to print data to a pdf file. I'面临着同样的问题 - yii2 basic printing a page in pdf . 我已经尝试了那里给出的解决方案,但仍然得到了错误 . 查看文件(按钮的代码) -

<?= Html::a('<i class="fa glyphicon glyphicon-print"></i> Print Salary Statement', ['/salary/salary/printsalarystatement'], [
            'class'=>'btn btn-primary', 
            'target'=>'_blank', 
            'data-toggle'=>'tooltip', 
            'title'=>'Will open the generated PDF file in a new window'
        ]);?>

调节器

public function actionPrintsalarystatement() {

        $pdf = new Pdf([
        'content'=>$this->renderPartial('_printSalarystatement'), [
            'model'=> $model,
            'mode'=> Pdf::MODE_CORE,
            'format'=> Pdf::FORMAT_A4,
            'orientation'=>Pdf::ORIENT_POTRAIT,
            'destination'=> Pdf::DEST_BROWSER,
            'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
            'cssInline'=> '.kv-heading-1{font-size:18px}',
            'options'=> ['title'=> 'Salary Statement'],
            'methods'=> [
                   'setHeader'=>['Generated on: '.date("r")],
                   'setFooter'=>['|page {PAGENO}|'],
                    ]
            ],

        ]);
        return $pdf->render();
    }

目前View.php -

<?= Html::a('<i class="fa glyphicon glyphicon-print"></i> Print Salary Statement', ['/salary/salary/printsalarystatement?id=s_id'], [
            'class'=>'btn btn-primary', 
            'target'=>'_blank', 
            'data-toggle'=>'tooltip', 
            'title'=>'Will open the generated PDF file in a new window'
        ]);?>

现任主持人

public function actionPrintsalarystatement($id) {

        //$model =  Salary::find()->where(['s_id' => $id]);
        $model = $this->findModel($id);
        $searchModel  = new SalarySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $data         = Salary::findOne($id);
        $content = $this->renderPartial('_printSalarystatement', ['model' => $model,'dataProvider' => $dataProvider,'searchModel'  => $searchModel,'data'=> $data]);
        $pdf = new Pdf([
            'mode'=> Pdf::MODE_CORE,
            'format'=> Pdf::FORMAT_A4,
            'destination'=> Pdf::DEST_BROWSER,
            'content'     => $content,

        ]);
        return $pdf->render();
    }

2 回答

  • 0

    你在哪里生成 $model ?你需要以某种方式将其传递或调用到函数中 .

  • 0
    public function actionPrintsalarystatement() {
    
         // you don't have the model for passing to the pdf object  
         // eg: 
          $model =  YourModel::find()->where(['your_column' => 'your_value']);
    
    
         $pdf = new Pdf([
          'content'=>$this->renderPartial('_printSalarystatement'), [
            'model'=> $model, // this $model is missing if you don assign a proper result to the this var like suggested above
            'mode'=> Pdf::MODE_CORE,
    

    根据您的评论:

    • 你不能找到没有的东西

    • 你有 $model = Salary::find()->where(['s_id' => '$id']); 这是错误的你必须以这种方式删除$ id周围的报价 $model = Salary::find()->where(['s_id' => $id]);

    如果您在调用actionPrintsalarystatement tye的页面中有$ id,则可以转到该操作

    例如:

    /salary/salary/printsalarystatement?id=your_id
    

    public function actionPrintsalarystatement($id) {
    

相关问题