PHPExcel:“无法读取文件"将Excel转换为PDF(.xlsx转换为.pdf)时出错 [英] PHPExcel: "Impossible to read file" error Converting Excel to PDF (.xlsx to .pdf)

查看:282
本文介绍了PHPExcel:“无法读取文件"将Excel转换为PDF(.xlsx转换为.pdf)时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只有一个电子表格的xlsx.我使用PHPExcel通过以下代码将其转换为pdf:

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:添加了渲染器

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

推荐答案

我认为Mark正在从事某项工作.当我遇到这样的错误时,我将从顶部开始,然后向下进行.即中间xlsx文件正确吗?

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?

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

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).

乔伊

这篇关于PHPExcel:“无法读取文件"将Excel转换为PDF(.xlsx转换为.pdf)时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆