将excel文件转换为pdf,用phpexcel返回空文件
我有一个Excel文件,我想将文件转换为PDF使用PHP的Excel。 使用下面的代码:
namespace frontend\controllers; use PHPExcel; use PHPExcel_IOFactory; use PHPExcel_Settings; use PHPExcel_Style_Alignment; use yii\web\Controller; class ExportsController extends Controller { public function actionIndex() { $inputFile = \Yii::getAlias('@frontend') . '/controllers/matrix.xls'; $inputFileType = PHPExcel_IOFactory::identify($inputFile); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($inputFile); $objPHPExcel->setActiveSheetIndex(0); $rendererLibrary = 'DomPDF'; $rendererLibraryPath = \Yii::getAlias('@frontend') . '/components/Classes/PHPExcel/Writer/PDF/' . $rendererLibrary; $rendererLibraryPath .= '.php'; if (!PHPExcel_Settings::setPdfRenderer( $rendererLibrary, $rendererLibraryPath ) ) { die( 'NOTICE: Please set the $rendererName and $rendererLibraryPath values' . '<br />' . 'at the top of this script as appropriate for your directory structure' ); } header('Content-Type: application/pdf'); header('Content-Disposition: attachment;filename="01simple.pdf"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF'); $objWriter->save('php://output'); } }
当我评论最后两行代码时,它将返回空的PDF文件,否则会抛出以下错误: net::ERR_INVALID_RESPONSE
。
你能帮我find代码的错误吗?
在最后添加此代码
header('Content-Type: application/pdf'); header('Content-Disposition: attachment;filename="01simple.pdf"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF'); ob_end_clean(); $objWriter->save('php://output');
如果你的xls文件有超过3000条logging,DomPDF将会产生内存错误。