0

在此处输入图像描述运行以下代码后,我在未打印的浏览器 PDF 输出中也看不到任何 PDF 下载。显示空白页。

    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 时无法下载。请找到随附的屏幕截图。

最后上面的代码片段解决了这个问题

4

2 回答 2

2

您正在将 PDF 写入文件:

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

这不会向浏览器输出任何内容,也不会触发下载。事后你需要自己把它扔掉,例如

 readfile('write.pdf');

或将 save() 调用更改为

 $objWriter->save('php://output');
于 2013-07-05T16:05:31.140 回答
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');
于 2017-06-23T05:28:08.977 回答