How to configure phpexcel for pdf

advertisements

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 working super, but now i want to offer the user the chance to choose between downloading the file in .xls or in .pdf.

I'm using Codeigniter and WAMP.

I downloaded PHPExcel, and it's on my C: directory.

Later i copied Classes: PHPExcel (folder) and PHPExcel.php to my codeigniter's third party folder.

Then, inside Codeigniter's Application, Libraries, i created a file named pdf.php and copied in there the code from the original PHPExcel docs (21pdf.php)

pdf.php

/** PHPExcel_IOFactory */
require_once dirname(__FILE__) . '/../Classes/PHPExcel/IOFactory.php';
$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
$rendererLibrary = 'tcPDF5.9';
$rendererLibraryPath = '/php/libraries/PDF/' . $rendererLibrary;

if (!PHPExcel_Settings::setPdfRenderer(
    $rendererName,
    $rendererLibraryPath
)) {
die(
    'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
    EOL .
    'at the top of this script as appropriate for your directory structure'
);
}

I don't really know how to configure it, my brain is burned right now. Any help would be very appreciated!


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      : [email protected]
*
*  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 :)