PHPExcel: “Impossible to read file” error Converting Excel to PDF (.xlsx to .pdf)

拜拜、爱过 提交于 2019-12-06 02:28:03

问题


I have a xlsx with only one spreadsheet. I use PHPExcel to convert it to a pdf through the following code:

        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');

The problem is that when i try to open the returned file i get the error message "Impossible to read file".

EIDT: Renderer added

        $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'
                );
        }

回答1:


I think Mark is on to something. When I run into errors like this, I start at the top and work down. i.e. Is the intermediate xlsx file correct?

I would probably also write some test code with REALLY simple xls files, or csv files that uses the PHPExcel library, and you should figure out what's wrong while you try to get those to work. Same with the pdf renderer, I would try a different one (if one is available).

Joey



来源:https://stackoverflow.com/questions/22797737/phpexcel-impossible-to-read-file-error-converting-excel-to-pdf-xlsx-to-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!