在Yii2上单击按钮时不下载Pdf

时间:2017-11-02 06:01:33

标签: php pdf yii2

我有一个视图,其中有一些记录。我想在PDF下载它们。但是在点击按钮时,我无法看到下载的pdf

我的动作控制器

public function actionViewpdf()
{

    $searchModel = new IssueMeters();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    if(isset($_POST['issue_pdf']))
    {
        $content = $this->render('viewpdf', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel
        ]);
        $pdf = new Pdf([
            // set to use core fonts only
            'mode' => Pdf::MODE_UTF8,
            // A4 papr format
            'format' => Pdf::FORMAT_A3,
            // portrait orientation
            'orientation' => Pdf::ORIENT_LANDSCAPE,
            // stream to browser inline
            'destination' => Pdf::DEST_BROWSER,
            // your html content input
            'content' => $content,
            // format content from your own css file if needed or use the
            // enhanced bootstrap css built by Krajee for mPDF formatting
            'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
            // any css to be embedded if required
            'cssInline' => '.kv-heading-1{font-size:18px}',
            // set mPDF properties on the fly
            'options' => ['title' => 'Accurate Survey'],
            // call mPDF methods on the fly
            'methods' => [
                'SetHeader' => ['Accurate (PVT) LTD.'],
                'SetFooter' => ['{PAGENO}'],
            ]
        ]);

        $pdf->filename = "Issue_Meter.pdf";
        // return the pdf output as per the destination setting
        return $pdf->render();
    }
    else{
        return $this->render('viewpdf', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel
        ]);
    }
}

我的观点

<div class="box-body">
       <form>
        <p>
            <a href="<?= URL::toRoute('issue/viewpdf') ?>" type="submit" class="btn btn-primary" id="dl" name="issue_pdf">Download PDF</a>
            <br/>
        </p>
        </form>  

      <?= GridView::widget([
         'dataProvider' => $dataProvider,
          'filterModel' => $searchModel,
        'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [
            'label' => 'Store',
            'value' => function ($d) {
               if(is_object($d->store))
                return $d->store->name;
               return ' - ';
            },
            'filter' => Html::activeDropDownList($searchModel,'store_id',\app\models\Stores::toArrayList(),['prompt' => "Stores", 'class'=>'form-control']),
        ],
         'meter_serial',
        [
            'label' => 'Issuer',
            'value' => function ($d) {
                if(is_object($d->user))
                return $d->user->username;
                return ' - ';
            },
            'filter' => Html::activeDropDownList($searchModel, 'issuer', \app\models\User::toArrayList(), ['prompt' => "Users", 'class' => 'form-control']),

        ],
        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>
    </div>

点击Download PDF后,它不会进入我控制器中的if状态。

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:0)

您的链接会触发获取请求。你的if语句会查找一个post变量。

isset($ _ POST ['issue_pdf'])应该是isset($ _ GET ['issue_pdf'])

否则我建议使用不同的下载操作:actionDownloadPDF($ issue_pdf)。

答案 1 :(得分:0)

您应该按照以下方式更改表单

$form = yii\widgets\ActiveForm::begin([]);
echo yii\helpers\Html::hiddenInput('issue_pdf', '1');
echo yii\helpers\Html::submitButton('Download Pdf')
yii\widgets\ActiveForm::end();