cakePHP使用PHPExcel保存为pdf

时间:2014-05-22 04:02:29

标签: cakephp pdf pdf-generation phpexcel mpdf

我一直在尝试使用以下方法将我的Excel文件保存为pdf:

PHPExcel - https://github.com/segy/PhpExcel

我的框架是cakePHP 2.3,我可以成功创建excel文件,但保存到pdf只是不起作用。阅读文档,尝试了示例,在互联网上搜索但没有骰子。

据我所知,上面的链接包括phpExcel 1.8和mPDF 5.4。我正在使用它的PHPExcel Helper。

我甚至无法从phpexcel工作中获得示例。

    $rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
    $rendererLibrary = 'mPDF5.4';
    $rendererLibraryPath = dirname(__FILE__).'/../../../libraries/PDF/' . $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'
        );
    }

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

如果我只能在https://github.com/segy/PhpExcel上使用PHPExcel生成一个hello world pdf,那就太棒了。

顺便说一句,我已经可以使用帮助程序生成.xlsx文档了,我只需要将它直接呈现给pdf。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

请尝试这样

在pdf

中编写如下所示的函数
       function pdfgenerate(){
            $objPHPexcel = PHPExcel_IOFactory::load('C:\Downloads\Report-1.xlsx');
            $rendererLibraryPath = dirname(dirname(__FILE__)).'/Vendor/mpdf';
            $rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;

            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'
             );
            }
            header('Content-Type: application/pdf');
            header('Content-Disposition: attachment;filename="'.$reportname.'.pdf"'); //tell browser what's the file name
            header('Cache-Control: max-age=0'); //no cache

            //$objPHPExcel->getActiveSheet()->setTitle('Orari');
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF'); 
            $objWriter->save('php://output');
       }