Shammir
Shammir

Reputation: 989

yii2 basic printing a page in pdf

please help. Hallo all, Am on verge of finishing a project created using yii2 basic. my problem is, I want to print a using with pdf as its output. I have tried yii2 mpdf and yii2-pdf without any success. I want to place the print button in the index.php of a view file. here's is my code snippet located at index view file.

<?= Html::a('<i class="fa glyphicon glyphicon-hand-up"></i>Property report',['/propertydetails/report'],
        [
         'class'=>'btn btn-danger',
         'target'=> '_blank',
         'data-toggle'=>'tooltip',
         'title'=>'Generate the pdf']); ?> 

And here's my controller

use kartik\mpdf\Pdf;

//function for reports
public function actionReport()
{
    //get your html raw content without layouts
   // $content = $this->renderPartial('view');
    //set up the kartik\mpdf\Pdf component
    $pdf = new Pdf([
        'content'=>$this->renderPartial('view'),
        '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'=> 'Paki Properties Reports'],
        'methods'=> [
               'setHeader'=>['Generated on: '.date("r")],
               'setFooter'=>['|page {PAGENO}|'],
                ]
        ]);
    return $pdf->render();
}

when I click the button I get this error

 PHP Notice – yii\base\ErrorException

Undefined variable: model

1. in C:\wamp\www\pakiSystems1\views\propertydetails\view.php at line 9
123456789101112131415161718

<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model app\models\Propertydetails */

$this->title = $model->propertyId;
$this->params['breadcrumbs'][] = ['label' => 'Propertydetails', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="propertydetails-view">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a('Update', ['update', 'id' => $model->propertyId], ['class' => 'btn btn-primary']) ?>

2. in C:\wamp\www\pakiSystems1\vendor\yiisoft\yii2\base\View.php – yii\base\View::renderPhpFile() at line 247
3. in C:\wamp\www\pakiSystems1\vendor\yiisoft\yii2\base\View.php – yii\base\View::renderFile() at line 149
4. in C:\wamp\www\pakiSystems1\vendor\yiisoft\yii2\base\Controller.php – yii\base\View::render() at line 398
5. in C:\wamp\www\pakiSystems1\controllers\PropertydetailsController.php – yii\base\Controller::renderPartial() at line 77
71727374757677787980818283

    public function actionReport()
    {
        //get your html raw content without layouts
       // $content = $this->renderPartial('view');
        //set up the kartik\mpdf\Pdf component
        $pdf = new Pdf([
            'content'=>$this->renderPartial('view'),
            '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}',

any help I will highly appreciate. Thanks in advance

Upvotes: 2

Views: 12185

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133400

In your controller there isn't the model. You must add this information eg:

$content = $this->renderPartial('_mpdf_report_scheda',  [
     'model' => $model,

     'mode'=> Pdf::MODE_CORE,
     'format'=> Pdf::FORMAT_A4,
     'orientation'=>Pdf::ORIENT_POTRAIT,
     'destination'=> Pdf::DEST_BROWSER,
     .....
           ]);

Upvotes: 2

Related Questions