Faisal Qayyum
Faisal Qayyum

Reputation: 132

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. But while clicking on the button I am unable to see the downloaded 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.

Any help would be highly appreciated.

Upvotes: 2

Views: 1539

Answers (2)

Maarten van Middelaar
Maarten van Middelaar

Reputation: 1721

Your link triggers a get request. Your if statement looks for a post variable.

isset($_POST['issue_pdf']) should be isset($_GET['issue_pdf'])

Otherwise I would recommend using a different action for the download: actionDownloadPDF($issue_pdf).

Upvotes: 0

ekaratas
ekaratas

Reputation: 92

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

Upvotes: 1

Related Questions