2017-03-06 99 views
0

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

<?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)); 
?> 
+0

'$收益率=包括 'download_pdf.php';'这并不信息载入'$ return',除非你在那个php文件中有'return',否则你可能会回应一些会导致确切错误的东西你说你有。 – cmorrissey

+0

Yess我没有重新调用任何变量..我只是创建一个PHP文件的HTML,只包括该PHP文件..所以请建议我任何建议,以克服这个问题 –

+0

还有一件事是当我回显变量我可以在浏览器中看到整个HTML,但面临错误“头已发送” –

回答

0

尝试使用output buffering

更换

$return = include 'download_pdf.php'; 

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

我试试这个。但它给了我下面的错误。 致命错误:未被捕获的Dompdf \例外:无法找到第1行,请在D:\ xampp \ htdocs \ convert_html_to_pdf_using_php \ dompdf \ src \ Cellmap.php中使用HTML代码跟踪器中的问题:417堆栈跟踪:#0 –

+0

@KshitijSoni它看起来像这个答案解决了您的原始问题,您应该继续并接受它。最初的问题是内容被发送到浏览器(可能来自您的内容),并使用输出缓冲工作来捕获输出,然后将其发送到浏览器。 – BrianS

+0

@KshitijSoni至于你的评论,这听起来像你有一个新的问题有关你的文件的结构。你应该为新问题开始一个新的问题。 – BrianS