PHPExcel PDF Writer无法正常工作

时间:2013-07-05 15:58:46

标签: php phpexcel

enter image description here运行以下代码后,我没有看到任何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时无法下载。请找到附带的屏幕截图。

最后上面的片段解决了问题

2 个答案:

答案 0 :(得分:2)

您正在将PDF写入文件:

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

这不会向浏览器输出任何内容,也不会触发下载。你需要在以后自己抛弃它,例如

 readfile('write.pdf');

或将save()调用更改为

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

答案 1 :(得分: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');