【问题标题】:dompdf error Unable to stream pdf: headers already sentdompdf 错误无法流式传输 pdf:标头已发送
【发布时间】:2016-06-30 19:41:35
【问题描述】:

我的情况比其他人之前回答的要复杂一些。相同的代码,结果是不同的。

工作场景 - 第一步

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

点击缩略图,然后添加 TILL 4 张图片。

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

PDF 按预期生成。 =)

错误场景 - 第二步

执行与上述相同的操作,但现在,在缩略图中添加超过 6 个图像。

PDF 收到“无法流式传输 pdf:标头已发送错误”

你选择的图片无关紧要,同样的事情会发生。

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

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

提前致谢!

【问题讨论】:

  • 为什么使用ob_clean 等。我不认为这是推荐的方式。你能把你所有的echo 转换成$html .= ' 并最终使用它吗?如果您仍然收到错误,请告诉我

标签: php pdf dompdf


【解决方案1】:

您似乎遇到了输出缓冲区大小的限制。您可以通过检查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>';

【讨论】:

    【解决方案2】:

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

    谢谢。

    【讨论】:

      猜你喜欢
      • 2013-10-31
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多