我有一个excel文件,我想用php excel将文件转换为pdf。使用下面的代码:将excel文件转换为pdf,使用phpexcel返回空文件
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
。
你能帮我找到代码的错误吗?
仍然是一样的错误 –
当我把$ objWriter = PHPExcel_IOFactory :: createWriter($ objPHPExcel,'PDF');在标题之前,它没有给net :: ERR_INVALID_RESPONSE,但显示另一个错误无法加载PDF渲染库 –
使用required_once加载PDF库 –