Phpexcel Save PDF

PHPExcel Save PDF

You need DomPDF for rendering the PDF. Check if you have the DomPDF library, otherwise download and implement it as follow:

$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
$rendererLibrary = 'domPDF0.6.0beta3';
$rendererLibraryPath = dirname(__FILE__). 'libs/classes/dompdf' . $rendererLibrary;

Error when i wont to save file as PDF using PHPExcel

PHPExcel doesn't have a PDF Rendering library built-in as part of the code/distribution. Instead, it allows you to use any of 3 different 3rd party PDF libraries (tcPDF, DomPDF or mPDF).

You have to install that PDF Rendering library separately, and then you need to tell PHPExcel which PDF library you have installed before you can save a file as PDF.

This is explained in the PHPExcel documentation and shown in the examples like 21pdf.php

phpexcel export selected sheet to pdf

By default, only the first worksheet will be generated for html/pdf output unless you tell the Writer which sheet to display before saving:

$objWriter->setSheetIndex(1);

(for the second sheet)

$objWriter->setSheetIndex($object->getActiveSheetIndex());

(for the current active worksheet)

$objWriter->setSheetIndex($object->getIndex($object->getSheetByName($sheet_name)));

(for a named worksheet)

How to configure phpexcel for pdf

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

phpexcel save as pdf

Checking the documentation for the tcPDF library, EMF format images are supported via the ImageMagick extension (if the extension is loaded in PHP) and on a Windows platform (apparently ImageMagick only supports EMF on Windows platforms).

PHP Excel: Saving created PDF to remote server and not user downlaod

Discard the headers.... they're all about telling the web browser to expect a PDF file, but you're not sending anything back to the browser (let alone a PDF file).

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

is telling PHPExcel to send the generated PDF directly to the web browser.... you say you don't want to do that, but to save to a remote server instead.

You'll need to intercept the results from $objWriter->save() so it doesn't go to the browser. Capture it using output buffering:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
ob_start();
$objWriter->save('php://output');
$myPdfData = ob_get_contents();
ob_end_clean();

Then you've captured the PDF datastream in $myPdfData and can then do:

file_put_contents($file_location, $myPdfData);

Note that this is likely to be memory-hungry



Related Topics



Leave a reply



Submit