【问题标题】:Dompdf gives me an empty pdfDompdf 给了我一个空的 pdf
【发布时间】:2021-10-31 22:18:08
【问题描述】:

我使用 composer 安装了dompdf

composer require dompdf/dompdf

在我的控制器中我有这个

use Dompdf\Dompdf;

....

function actionPdf() {
   $dompdf = new DOMPDF();
   $dompdf->loadHtml('<h1>hello world</h1>');
   $dompdf->setPaper('A4', 'landscape');
   $dompdf->render();
}

但我得到的只是一个空白 PDF。当我查看app.log 时,我看到了这个

2021-09-02 20:20:41 [127.0.0.1][262][-][error][yii\web\HeadersAlreadySentException] yii\web\HeadersAlreadySentException: Headers already sent in  on line 0. in C:\xampp\htdocs\yii2\vendor\yiisoft\yii2\web\Response.php:373
Stack trace:
#0 C:\xampp\htdocs\yii2\vendor\yiisoft\yii2\web\Response.php(346): yii\web\Response->sendHeaders()
#1 C:\xampp\htdocs\yii2\vendor\yiisoft\yii2\base\Application.php(398): yii\web\Response->send()
#2 C:\xampp\htdocs\yii2\frontend\web\index.php(64): yii\base\Application->run()
#3 {main}

任何想法如何解决这个问题?谢谢

【问题讨论】:

标签: pdf yii2 dompdf


【解决方案1】:

不确定正确的 Yii 方式,但在我的情况下这是可行的:

注意最后的exit();

// instantiate and use the dompdf class
$dompdf_options = new \Dompdf\Options();
$dompdf_options->setDpi(150);

$dompdf = new Dompdf($dompdf_options);
$dompdf->loadHtml($html);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4');
      
// Render the HTML as PDF
$dompdf->render();

$attachment=false;

$options=[
    "Attachment" => $attachment
];

// Output the generated PDF to Browser
$dompdf->stream('Informe de vehículo', $options);

exit();

我猜这个问题是由 Yii 渲染数据的方式引起的。但是通过调用 exit() 可以防止任何这种情况发生。

【讨论】:

    猜你喜欢
    • 2011-08-27
    • 2016-03-15
    • 1970-01-01
    • 2020-07-17
    • 2012-11-16
    • 2013-02-17
    • 2020-06-02
    • 2012-09-13
    • 2021-10-08
    相关资源
    最近更新 更多