【问题标题】:Unable to downlaod pdf using DOMPDF无法使用 DOMPDF 下载 pdf
【发布时间】:2017-03-06 18:31:44
【问题描述】:

我检查了大量资源,但我的问题无法解决。我试图通过包含 PHP File 来生成 PDF。但现在我陷入了“无法流式传输 pdf:标头已发送”错误。我还压缩了我的代码并删除了空格。 这是我的代码。

<?php
 //ob_start();
// include autoloader
require_once 'dompdf/autoload.inc.php';

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$return = include 'download_pdf.php';
$return = stripslashes($return);
$dompdf->loadHtml($return);

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
//$dompdf->stream();

// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("codex",array("Attachment"=>0));
?>

【问题讨论】:

  • $return = include 'download_pdf.php'; 这不会将信息加载到 $return 除非您在该 php 文件中有 return ,您可能会在此处回显某些内容,这将导致您所说的确切错误.
  • 是的,我没有重新调整任何变量。我只是为一个 php 文件创建 html 并包含该 php 文件。所以请向我提出解决此问题的任何建议
  • 另一件事是,当我回显返回变量时,我可以在浏览器中看到整个 html,但遇到错误“标头已发送”

标签: php pdf dompdf


【解决方案1】:

尝试使用output buffering

替换

$return = include 'download_pdf.php';

ob_start();
include 'download_pdf.php';
$return  = ob_get_contents();
ob_end_clean();

【讨论】:

  • 我试试这个。但它给了我以下错误。致命错误:未捕获的 Dompdf\Exception:找不到第 1 行,请使用 D:\xampp\htdocs\convert_html_to_pdf_using_php\dompdf\src\Cellmap.php:417 中的 HTML 代码在跟踪器中提出问题 堆栈跟踪: #0
  • @KshitijSoni 看起来这个答案解决了你原来的问题,你应该继续接受它。最初的问题是内容被发送到浏览器(可能来自您的包含)并且使用输出缓冲通过在将输出发送到浏览器之前捕获输出来解决这个问题。
  • @KshitijSoni 至于您的评论,听起来您有一个与文档结构相关的新问题。您应该针对新问题开始一个新问题。
猜你喜欢
  • 1970-01-01
  • 2014-09-05
  • 2023-04-09
  • 2015-10-30
  • 2018-07-19
  • 2014-10-26
  • 1970-01-01
  • 2016-04-02
  • 2017-09-21
相关资源
最近更新 更多