2017-03-06 69 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