PHPExcel:将 Excel 转换为 PDF(.xlsx 到 .pdf)时出现 "Impossible to read file"错误

标签 php pdf phpexcel xlsx

我有一个只有一个电子表格的 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'
                );
        }

最佳答案

我认为马克说到点子上了。当我遇到这样的错误时,我会从上到下开始工作。即中间 xlsx 文件是否正确?

我可能还会使用非常简单的 xls 文件或使用 PHPExcel 库的 csv 文件编写一些测试代码,您应该在尝试使它们正常工作时找出问题所在。与 pdf 渲染器相同,我会尝试不同的渲染器(如果可用的话)。

乔伊

关于PHPExcel:将 Excel 转换为 PDF(.xlsx 到 .pdf)时出现 "Impossible to read file"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22797737/

相关文章:

php - 无论我如何尝试都无法让 PHP 刷新工作

php - 安装 Phalcon PHP Devtools : "ERROR: Phalcon extension isn' t installed . ..虽然安装了模块

PHP函数将SQL作为html返回并抛出错误?

php - Linux PDF to HTML + Image Map + jpeg 图像文件

php - 如何每30条记录换行

php - Laravel 5.2 升级 - 找不到类 AuthServiceProvider

pdf - Jasperreports 标记属性和 PDF

java - Apache Batik 1.7 + Apache FOP 1.1 不在 Ubuntu 上呈现字体

PHPExcel - 列的复制范围

PHPExcel 将 Excel 内容转储到屏幕上