2013-07-05 202 views
0

enter image description here運行下面的代碼後,我沒有看到任何PDF下載,以及瀏覽器PDF輸出沒有打印。顯示空白頁。PHPExcel PDF Writer不工作

require_once 'Classes/PHPExcel/IOFactory.php'; 
$objPHPExcel = new PHPExcel(); 
$objPHPExcel->getActiveSheet()->setCellValue('c9','40'); 

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF'); 
//You need to include and print to PDF the entire worksheets contained in the workbook 
$objWriter->writeAllSheets(); 

//You need to assign a filename to the PDF file (write.pdf for example) 
header('Content-type:Application/pdf'); 
header('Content-Disposition: attachment;filename="export.pdf"'); 
$objWriter->save('php://output'); 

仍然存在代碼上的一個小錯誤。在瀏覽器中查看PDF時無法下載。請查找附件截圖。

最後上面的代碼中,其解決了這一問題

+0

標題(「內容類型:應用程序/ PDF');設置這個,看看它是否工作 – Satya

+0

,而不是下載write.pdf。 sample.php正在下載。以及在瀏覽器中顯示空白PDF頁面 – Bharanikumar

+0

您是否正在識別您正在使用PHPExcel的PDF渲染庫?因爲我沒有看到任何代碼! –

回答

2

你正在編寫的PDF到一個文件:

$objWriter->save('write.pdf'); 

這不會輸出任何東西到瀏覽器,或觸發下載。你需要在後面自己丟棄,例如

readfile('write.pdf'); 

或更改保存()調用

$objWriter->save('php://output'); 
+0

添加了屏幕截圖,其中解釋好一點 – Bharanikumar

+0

plz發表您的答案。 – Bharanikumar

+0

if if remove「header('Content-Disposition:attachment; filename =」export.pdf'');「然後應用程序PHP文件正在下載而不是PDF – Bharanikumar

0

頁眉應該首先創建PDF之前:試試這個:

require_once 'Classes/PHPExcel/IOFactory.php'; 
$objPHPExcel = new PHPExcel(); 
$objPHPExcel->getActiveSheet()->setCellValue('c9','40'); 
//Create the header first before Writing PDF 
header('Content-type:Application/pdf'); 
header('Content-Disposition: attachment;filename="export.pdf"'); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF'); 
//You need to include and print to PDF the entire worksheets contained in the workbook 
$objWriter->writeAllSheets(); 
$objWriter->save('php://output');