kartik-v / yii2-mpdf

A Yii2 wrapper component for the mPDF library which generates PDF files from UTF-8 encoded HTML.
http://demos.krajee.com/mpdf
Other
161 stars 150 forks source link

DEST_BROWSER shows cached PDF #29

Closed Xoduz closed 5 years ago

Xoduz commented 8 years ago

There seems to be a cache-related problem (tested in Chrome 48, Opera 35 and IE 11) when using mpdf with destination DEST_BROWSER. The PDF file that opens in the browser doesn't get re-generated from the current view, but shows the previous PDF that was generated for the view - until I manually hit refresh in the browser tab that contains said PDF. This does not happen when destination is DEST_DOWNLOAD, it always reflects the current state of the view in question.

Here's my setup:

Relevant Yii2 packages: mPDF 6.0 (20/12/2014) yii2-mpdf 1.0.0

View:

<?= Html::a('<i class="fa fa-print"></i> Print', ['/projects/project-print', 'id' => $model->id], [
        'class'=>'btn btn-default',
        'target'=>'_blank',
    ]); ?>

Projects Controller:

public function actionProjectPrint() {
    $id = $_GET['id'];
    $pdfContent = Yii::$app->runAction('projects/printView', ['id' => $id]);

    $pdf = new Pdf([
        'mode' => Pdf::MODE_CORE,
        'format' => Pdf::FORMAT_A4,
        'marginTop' => 45,
        'marginHeader' => 9,
        'orientation' => Pdf::ORIENT_PORTRAIT,
        'destination' => Pdf::DEST_BROWSER,
        'content' => $pdfContent,
        'cssFile' => './css/mpdf_bootstrap.css',
        'options' => ['title' => 'Test'],
        'methods' => [
            'SetFooter'=>['{PAGENO}'],
        ]
    ]);

    return $pdf->render();
}
Xoduz commented 8 years ago

Probably a browser caching issue. Worked around this by intercepting the button click with a javascript on click-event and adding a timestamp parameter to the url.

almirb commented 7 years ago

Hi!

Try to put this lines before rendering your PDF to the browser:

Yii::$app->response->format` = \yii\web\Response::FORMAT_RAW;
Yii::$app->response->headers->add('Content-Type', 'application/pdf');

:)