我有一个只有一个电子表格的xlsx。我使用PHPExcel通过以下代码将其转换为pdf:
error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
require_once 'phpExcel/PHPExcel/IOFactory.php';
require_once 'phpExcel/PHPExcel.php';
$inputFileName = 'doc/ModUnico';
$excel2 = PHPExcel_IOFactory::createReader('Excel2007');
$excel2 = $excel2->load($inputFileName.'.xlsx');
$excel2->setActiveSheetIndex(0);
$excel2->getActiveSheet()->setCellValue('H5', '4');
$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$objWriter->save($inputFileName.'_.xlsx');
$objPHPexcel = PHPExcel_IOFactory::load($inputFileName.'_.xlsx');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="test.pdf"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'PDF');
$objWriter->writeAllSheets();
$objWriter->setPreCalculateFormulas(false);
$objWriter->save('php://output');
问题是,当我尝试打开返回的文件时,我收到错误消息“无法读取文件”。
EIDT:渲染器添加
$rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
$rendererLibrary = 'mpdf.php';
$rendererLibraryPath = dirname(__FILE__).'/MPDF57/' . $rendererLibrary;
if (!PHPExcel_Settings::setPdfRenderer(
$rendererName,
$rendererLibraryPath
)) {
die(
'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
'<br />' .
'at the top of this script as appropriate for your directory structure'
);
}
答案 0 :(得分:1)
我认为Mark正在做点什么。当我遇到这样的错误时,我从顶部开始工作。即中间的xlsx文件是否正确?
我可能还会用一些简单的xls文件或使用PHPExcel库的csv文件编写一些测试代码,你应该在尝试让它们工作时弄清楚什么是错的。与pdf渲染器相同,我会尝试另一个(如果有的话)。
乔伊