Yii2~Bad Request(#400)缺少必需参数:创建pdf时的id

时间:2017-09-13 09:44:48

标签: php yii2 mpdf yii2-basic-app

我试图使用Yii2 mPDF扩展创建发票的PDF,我可以创建一个pdf就好但我想将视图数据传递给似乎无法正常工作的Pdf。

控制器:

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();
}

观点:

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

以及我创建&#34; factuur&#34;

的页面
<?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>
                        jsmith@email.com
                    </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>
       <.....>

这是视图,你看到一个var转储。有些人没有设置字段也是一个问题,但我已经提出了一个问题:

enter image description here

这是我的目标,但内容取决于ID:

enter image description here

1 个答案:

答案 0 :(得分:0)

在动作参数中缺少$id

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'
    ]
);