How can I convert excel file to pdf using TCPDF?

4.7k views Asked by At

I need to generate a report with graphs in excel to pdf. I tried using phpexcel but it fails to load the charts. It only generates PDF with tables. Here's my code in phpexcel.

$newpdf = "report-".date('m_d_Y_H_i_s', strtotime('now')).".pdf";
$objPHPExcel1 = new PHPExcel();
$objPHPExcel1 = PHPExcel_IOFactory::load('csv/'.$filename);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setFitToWidth(true);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setFitToHeight(true);
$objPHPExcel1->getActiveSheet()->setShowGridLines(false);

header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="'.$newpdf.'"'); 
header('Cache-Control: max-age=0'); //no cache
$objWriter1 = new PHPExcel_Writer_PDF($objPHPExcel1); 
$objWriter1->save('php://output');
exit;

I also tried loading the contents from HTML using jchartfx for graphs without success. Any idea?
Thanks in advance.

1

There are 1 answers

0
Mark Baker On

PHPExcel can only read charts from Excel2007 (OfficeOpenXML .xlsx) files, and will only load them if you explicitly tell it to do so.

if (PHPExcel_IOFactory::identify('csv/'.$filename) == "Excel2007") {
    $objReader = PHPExcel_IOFactory::createReader("Excel2007");
    $objReader->setIncludeCharts(TRUE);
} else {
    die("Can't load charts");
}
$objPHPExcel = $objReader->load('csv/'.$filename);

When writing to a PDF file, you must have the relevant library installed (tcpdf in your case) and tell PHPExcel that you want to use it and the directory where tcpdf is installed.

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

You also need jpgraph installed, and again PHPExcel needs to know where it is installed

$rendererName = PHPExcel_Settings::CHART_RENDERER_JPGRAPH;
$rendererLibrary = 'jpgraph3.5.0b1/src';
$rendererLibraryPath = '/php/libraries/Charts/' . $rendererLibrary;

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

Finally, you need to tell the PDF Writer explicitly to generate the charts

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save($outputFileName);