如何使用kartik MPDF扩展名在Yii2中强制打开PDF

时间:2018-12-26 17:33:46

标签: php yii2 mpdf

我有Kartik mpdf,它不允许我查看pdf创建文件,但强迫我下载。我如何强制它在浏览器中打开而不是下载。

 $pdf = new Pdf([
        // set to use core fonts only
        'mode' => Pdf::MODE_CORE,
        // A4 paper format
        'format' => Pdf::FORMAT_A4,
        // 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/src/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' => "Sales report"],
        // call mPDF methods on the fly
        'methods' => [
            'SetHeader' => ["Sales Report"],
            'SetFooter' => ['{PAGENO}'],
        ]
    ]);
     return $pdf->render();

我也尝试过

Yii::$app->response->headers->set('Content-Type', 'application/pdf');
$file = $user.'.pdf';
Yii::$app->response->headers->set('Content-Disposition', 'inline;filename="'.$file.'"');

1 个答案:

答案 0 :(得分:0)

您需要将目标位置更改为Pdf::DEST_DOWNLOAD,,并 100%确定您不打印任何内容或修改标题。

$pdf = new Pdf([
        'mode' => Pdf::MODE_CORE,
        'format' => Pdf::FORMAT_A4,
        'orientation' => Pdf::ORIENT_LANDSCAPE,
        'destination' => Pdf::DEST_DOWNLOAD,
        'content' => $content,
        'cssFile' => '@vendor/kartik-v/yii2-mpdf/src/assets/kv-mpdf-bootstrap.min.css',
        'cssInline' => '.kv-heading-1{font-size:18px}',
        'options' => ['title' => "Sales report"],
        'methods' => [
            'SetHeader' => ["Sales Report"],
            'SetFooter' => ['{PAGENO}'],
        ]
    ]);

     return $pdf->render();