Search code examples
phpyii2mpdfyii2-basic-app

Yii2 ~ Bad Request (#400) Missing required parameters: id while creating pdf


I'm trying to create an PDF of an invoice using the Yii2 mPDF extention, I can create a pdf just fine but I want to pass the view data into the Pdf which doesn't seem to work.

The controller:

public function actionMpdfDemo1($id) {
  $model = Facturen::findOne($id);
    $content = $this->renderPartial('factuur', [
        'model' => $model,
        'company' => $company,
    ]);
    $pdf = new Pdf([
        'mode' => Pdf::MODE_UTF8, // leaner size using standard fonts
        'format' => Pdf::FORMAT_A4,
        'orientation' => Pdf::ORIENT_PORTRAIT,
        'content' => $content,
        'options' => [
            'title' => 'Factuur',
            'subject' => 'Generating PDF files via yii2-mpdf extension has never been easy'
        ],
        'methods' => [
            'SetHeader' => ['Generated By: Krajee Pdf Component||Generated On: ' .  date("l j F Y")],
            'SetFooter' => ['|Page {PAGENO}|'],
        ]
    ]);
    return $pdf->render();
}

The view:

<?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use app\models\Facturen;
use helpers\ArrayHelper;
use app\models\Producten;
/* @var $this yii\web\View */
/* @var $model app\models\Facturen */
/* @var $modelProducten app\models\Producten */

$this->title = 'Factuur: '. $model->factuur_id;
$this->params['breadcrumbs'][] = ['label' => 'Factures', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="facturen-view">

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

    <p>
        <?= Html::a('Update', ['update', 'id' => $model->factuur_id], ['class' => 'btn btn-primary']) ?>
        <?= Html::a('Delete', ['delete', 'id' => $model->factuur_id], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
        ]) ?>
    </p>

    <?= DetailView::widget([
        'model' => $model,
        'modelProducten' => $modelProducten,
        'attributes' => [
            'factuur_id',
            'company.company_name',
            'person.Contactpersoon',
//            'person.last_name',
            'product.product_id',
            'product.product_name',
            'product.amount',
            'product.price',
            'date',
        ],
    ]) ?>
<p>
<?php        echo Html::a('<i class="fa glyphicon glyphicon-hand-up"></i> Factuur genereren', ['/facturen/mpdf-demo-1'], [
        'class'=>'btn btn-danger',
        'target'=>'_blank',
        'data-toggle'=>'tooltip',
        'title'=>'Will open the generated PDF file in a new window'
        ]); ?>
    </p>
<?php var_dump($model)?>
</div>

and the page where I create my "factuur"

<?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use app\models\Facturen;
use helpers\ArrayHelper;
use app\models\Producten;
/* @var $this yii\web\View */
/* @var $model app\models\Facturen */
/* @var $modelProducten app\models\Producten */
$this->title = 'Factuur: '?>
<div class="container">
    <div class="row">
        <div class="col-xs-12">
            <div class="invoice-title">
                <h3 class="pull-right" style="float: right;">Factuurnummer # <?php echo $model->factuur_id; ?></h3>
            </div>
            <hr>
            <div class="row">
                <div class="col-xs-6">
                    <address>
                        <strong>Bedrijsnaam: </strong><?php echo $model->company->company_name; ?><br>
                        <strong>Contactpersoon: </strong><?php echo $model->person->Contactpersoon;?><br>
                    </address>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-6">
                    <address>
                        <strong>Betaal Methode:</strong><br>
                        Visa ending **** 4242<br>
                        [email protected]
                    </address>
                </div>
                <div style="float: right;" class="col-xs-6 text-right">
                    <address>
                        <strong>Factuurdatum:</strong><br>
                        <?php echo $model->date; ?><br><br>
                    </address>
                </div>
            </div>
       <.....>

This is the view, you see a var dump. some not set fields which is a problem too, but I made a question about that already:

enter image description here

This is Kinda what i'm going for but with content depending on the id:

enter image description here


Solution

  • Missing $id in action parameter.

    Html::a('<i class="fa glyphicon glyphicon-hand-up"></i> Factuur genereren', 
        ['/facturen/mpdf-demo-1', 'id' => $model->factuur_id],
        [
           'class'=>'btn btn-danger',
           'target'=>'_blank',
           'data-toggle'=>'tooltip',
           'title'=>'Will open the generated PDF file in a new window'
        ]
    );