How to configure phpexcel for pdf

后端 未结 2 849
傲寒
傲寒 2020-12-17 05:16

i\'ve been searching in the official docs but i can\'t seem to put all the things together in my mind (i\'m new to programming). I managed to use the excel library and it\'s

相关标签:
2条回答
  • 2020-12-17 05:54

    Download tcpdf library from https://sourceforge.net/projects/tcpdf/files/ and extract to your porject folder (i have extracted in to xampp\htdocs\export\tcpdf)

    require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
    require_once 'PHPExcel/Classes/PHPExcel.php';
    
    // Create new PHPExcel object
    $objPHPExcel = new PHPExcel(); 
    
    $rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
    
    // tcpdf folder path
    $rendererLibraryPath = dirname(__FILE__).'/tcpdf'; 
    

    I found a simple snippet here http://wiki.workassis.com/download-as-pdf-using-phpexcel-and-tcpdf/ phpexcel with tcpdf library

    0 讨论(0)
  • 2020-12-17 06:05

    ok so now i have this thing working, took me several days but finally achieved it.

    In order to configure phpexcel in Codeigniter for generating pdf reports i had to:

    1). Download PHPExcel, and copy the content from the folder "Classes" to another folder in my framework Codeigniter (application/third_party). Inside of Classes folder, you will find another folder named "PHPExcel" and a .php file with the same name.

    2). Then, inside application/libraries i created a file excel.php with the class constructor:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    /* 
    *  ======================================= 
    *  Author     : Muhammad Surya Ikhsanudin 
    *  License    : Protected 
    *  Email      : mutofiyah@gmail.com 
    *   
    *  Dilarang merubah, mengganti dan mendistribusikan 
    *  ulang tanpa sepengetahuan Author 
    *  ======================================= 
    */  
    require_once APPPATH."/third_party/PHPExcel.php"; 
    
    class Excel extends PHPExcel { 
      public function __construct() { 
         parent::__construct(); 
       } 
     }
    

    3). nex to move on to pdf. First you should choose which library you want to use. I chose to use dompdf https://github.com/dompdf/dompdf and install this in application/libraries

    4). created a file name pdf.php (place it where you want) and in it i put this (based on the examples given in the official phpexcel docs: 21pdf.php and 01simple-download-pdf.php that you can find in the test folder in PHPExcel):

    $rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
    $rendererLibrary = 'dompdf_0-6-0_beta3';
    $rendererLibraryPath = dirname(__FILE__).'/'. $rendererLibrary;
    

    'dompdf_0-6-0_beta3' is the name of the folder that contains the library for rendering into pdf

    dirname(FILE).'/'. $rendererLibrary is the path for finding this 'dompdf_0-6-0_beta3' folder

    5). build the method in my admin.php to call to this function. What i did in the first place was to build the excel tables and to put some data. After that, i use a boolean to see if user wants in .xls or .pdf. If he wants in .pdf then the code for generating the report with that extension then the code is this:

    require_once (realpath(dirname(dirname(dirname(__FILE__))))."/libraries/pdf.php");
    
                $filename=$rDate.'_Reporte_Usuarios_front'.'.pdf'; 
                header('Content-Type: application/pdf');
                header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
                header('Cache-Control: max-age=0'); //no cache
                // $objWriter = new PHPExcel_Writer_PDF($objPHPExcel);
                $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'PDF'); 
                $objWriter->setSheetIndex(0);
                $objWriter->save('php://output');
    

    I posted this question when my error was that render library couldn't be found. That's why i was trying to use the phpexcel wrapper and not the propper library. So after that had to fight a little bit with the path but then made it.

    After that fixed, another error came up: 'Unable to load PDF Rendering library' in DomPDF (from PHPExcel). So i found out that this line:

    $pdfRendererClassFile = PHPExcel_Settings::getPdfRendererPath() . '/dompdf/dompdf_config.inc.php';
    

    i was missing the folder containing dompdf_config.inc.php, because complete path is:

    application/libraries/dompdf_0-6-0_beta3/dompdf/stuff

    so just added 'dompdf' and done :)

    0 讨论(0)
提交回复
热议问题