dompdf 错误无法流式传输 pdf:headers 已发送

dompdf error Unable to stream pdf: headers already sent

我的情况比之前回答的其他人稍微复杂一点。使用相同的代码,结果不同。

工作场景 - 第一步

转到http://renamchristofoletti.com/en/works/covers/

单击“缩略图”,添加至 4 张图像。

点击下载,然后点击下载作品集。

PDF 已按预期生成。 =)

错误方案 - 第 2 步

做和上面一样的事情,但是现在,在缩略图中添加超过 6 张图像。

PDF 得到一个"Unable to stream pdf: headers already sent error"

您选择的图像无关紧要,同样的事情也会发生。

DOMPDF 代码

ob_clean();
echo '<html><body>...'; // above html and css goes here

$images2 = str_replace(' ', "", $_COOKIE['rc_folio']);
$images = explode(',', $images2);
$images = array_filter($images);

foreach ( $images as $image ) {
    $img_id = get_image_id($image);

    $imglocal = wp_get_attachment_metadata( $img_id );

        $img_abs_path = ABSPATH . 'wp-content/uploads/' . $imglocal['file'];
        list($width, $height) = getimagesize($img_abs_path);
        if ($width > $height) {
            // Landscape
            echo '<div style="page-break-after: always; text-align: center;"><div class="content-line"></div><div class="content-line-2"></div><img src="' . $img_abs_path . '" style="width: 80%; max-width: 80%; height: auto; margin-top: 250px;" /></div>'; 
        } else {
            // Portrait or Square
            echo '<div style="page-break-after: always; text-align: center;"><div class="content-line"></div><div class="content-line-2"></div><img src="' . $img_abs_path . '" style="width: auto; height: 60%; max-height: 60%; margin-top: 180px;" /></div>';
        }
}
echo '</body></html>';

$html = ob_get_contents();
ob_get_clean();

$pdf_path = ABSPATH . 'wp-content/themes/moqueca/dompdf-master/dompdf_config.inc.php';
require_once( $pdf_path );

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper('letter', 'portrait');
$dompdf->render();
$dompdf->stream("renam_christofoletti_customfolio.pdf");
exit();

有谁能澄清一下我的想法吗?我已经尝试了许多其他解决方案,但没有任何效果。

提前致谢!

您似乎 运行 超出了输出缓冲区大小的限制。您可以通过检查 output_buffering 配置参数的值来检查是否存在大小限制:

echo ini_get('output_buffering');

如果是这种情况,您可以修改缓冲区大小:

ini_set('output_buffering', true); // no limit
ini_set('output_buffering', 12288); // 12KB limit

或者按照 Dharam 对你问题的评论中的说明,将内容直接推送到变量中,而不是依赖输出缓冲:

$html = '<html><body>...'; // above html and css goes here

$images2 = str_replace(' ', "", $_COOKIE['rc_folio']);
$images = explode(',', $images2);
$images = array_filter($images);

foreach ( $images as $image ) {
    $img_id = get_image_id($image);

    $imglocal = wp_get_attachment_metadata( $img_id );

        $img_abs_path = ABSPATH . 'wp-content/uploads/' . $imglocal['file'];
        list($width, $height) = getimagesize($img_abs_path);
        if ($width > $height) {
            // Landscape
            $html .= '<div style="page-break-after: always; text-align: center;"><div class="content-line"></div><div class="content-line-2"></div><img src="' . $img_abs_path . '" style="width: 80%; max-width: 80%; height: auto; margin-top: 250px;" /></div>'; 
        } else {
            // Portrait or Square
            $html .= '<div style="page-break-after: always; text-align: center;"><div class="content-line"></div><div class="content-line-2"></div><img src="' . $img_abs_path . '" style="width: auto; height: 60%; max-height: 60%; margin-top: 180px;" /></div>';
        }
}
$html .= '</body></html>';

案件破案。我刚刚更改了 html 的 echo .= 并且一切正常。

谢谢。