简体   繁体   English

PDF未下载按钮点击Yii2

[英]Pdf not downloading on the button click in Yii2

I have a view in which there are some records in it. 我认为其中包含一些记录。 I want to download them in a PDF . 我想以PDF下载它们。 But while clicking on the button I am unable to see the downloaded pdf . 但是在单击按钮时,我看不到下载的pdf

My action controller 我的动作控制器

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

My view 我的观点

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

On clicking of the Download PDF it doesn't goes to the if condition in my controller. 在单击“ Download PDF时,不会进入控制器中的if条件。

Any help would be highly appreciated. 任何帮助将不胜感激。

Your link triggers a get request. 您的链接触发获取请求。 Your if statement looks for a post variable. 您的if语句查找post变量。

isset($_POST['issue_pdf']) should be isset($_GET['issue_pdf']) isset($ _ POST ['issue_pdf'])应该设为isset($ _ GET ['issue_pdf'])

Otherwise I would recommend using a different action for the download: actionDownloadPDF($issue_pdf). 否则,我建议对下载使用其他操作:actionDownloadPDF($ issue_pdf)。

You should change your form as follow 您应该按照以下方式更改表格

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM