Download as pdf using phpExcel and tcpdf

Download phpexcel :

Download tcpdf :

    //data to make csv (may be from database, form, etc)
    $dbdata = array(
                array("name"=>"name1", "phone"=>"1010101001"),
                array("name"=>"name2", "phone"=>"1010101002")

    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
    $rendererLibraryPath = dirname(__FILE__).'/tcpdf'; 

    //setting column heading
    //setting column body
    $i=2; //starting from row 2 bcz row 1 set to header
    foreach($dbdata as $data) {
    // Rename sheet

    // Set active sheet index to the first sheet, so Excel opens this as the first sheet
if (!PHPExcel_Settings::setPdfRenderer(
	)) {
		'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
		'<br />' .
		'at the top of this script as appropriate for your directory structure'

// Redirect output to a client’s web browser (PDF)
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="01simple.pdf"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');


Author: bm on May 17, 2016
Category: php

Your comment:

Your Name


Last articles