From f7268cdd656e01d5e3d4fb86046dc5ff636add0f Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Mon, 5 Nov 2012 00:50:21 +0000 Subject: [PATCH] Initial (as yet untested) code to include Excel charts in HTML and PDF output --- Classes/PHPExcel/Writer/HTML.php | 130 ++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 3 deletions(-) diff --git a/Classes/PHPExcel/Writer/HTML.php b/Classes/PHPExcel/Writer/HTML.php index 15aedc06..14e765d4 100644 --- a/Classes/PHPExcel/Writer/HTML.php +++ b/Classes/PHPExcel/Writer/HTML.php @@ -48,6 +48,14 @@ class PHPExcel_Writer_HTML implements PHPExcel_Writer_IWriter { */ private $_sheetIndex = 0; + /** + * Write charts that are defined in the workbook? + * Identifies whether the Writer should write definitions for any charts that exist in the PHPExcel object; + * + * @var boolean + */ + private $_includeCharts = false; + /** * Pre-calculate formulas * @@ -62,6 +70,13 @@ class PHPExcel_Writer_HTML implements PHPExcel_Writer_IWriter { */ private $_imagesRoot = '.'; + /** + * embed images, or link to images + * + * @var boolean + */ + private $_embedImages = FALSE; + /** * Use inline CSS? * @@ -516,7 +531,7 @@ class PHPExcel_Writer_HTML implements PHPExcel_Writer_IWriter { * @return string * @throws PHPExcel_Writer_Exception */ - private function _writeImageTagInCell(PHPExcel_Worksheet $pSheet, $coordinates) { + private function _writeImageInCell(PHPExcel_Worksheet $pSheet, $coordinates) { // Construct HTML $html = ''; @@ -543,7 +558,66 @@ class PHPExcel_Writer_HTML implements PHPExcel_Writer_IWriter { $filename = htmlspecialchars($filename); $html .= PHP_EOL; - $html .= ' ' . PHP_EOL; + if ((!$this->_embedImages) || ($this->_isPdf)) { + $imageData = $filename; + } else { + $imageDetails = getimagesize($filename); + if ($fp = fopen($filename,"rb", 0)) { + $picture = fread($fp,filesize($filename)); + fclose($fp); + // base64 encode the binary data, then break it + // into chunks according to RFC 2045 semantics + $base64 = chunk_split(base64_encode($picture)); + $imageData = 'data:'.$imageDetails['mime'].';base64,' . $base64; + } else { + $imageData = $filename; + } + } + $html .= '' . PHP_EOL; + } + } + } + + // Return + return $html; + } + + /** + * Generate chart tag in cell + * + * @param PHPExcel_Worksheet $pSheet PHPExcel_Worksheet + * @param string $coordinates Cell coordinates + * @return string + * @throws PHPExcel_Writer_Exception + */ + private function _writeChartInCell(PHPExcel_Worksheet $pSheet, $coordinates) { + // Construct HTML + $html = ''; + + // Write charts + foreach ($pSheet->getChartCollection() as $chart) { + if ($chart instanceof PHPExcel_Chart) { + $chartCoordinates = $chart->getTopLeftPosition(); + if ($chartCoordinates['cell'] == $coordinates) { + $chartFileName = tempnam(PHPExcel_Shared_File::sys_get_temp_dir()); + if (!$chart->render($chartFileName)) { + return; + } + + $html .= PHP_EOL; + $imageDetails = getimagesize($chartFileName); + if ($fp = fopen($chartFileName,"rb", 0)) { + $picture = fread($fp,filesize($chartFileName)); + fclose($fp); + // base64 encode the binary data, then break it + // into chunks according to RFC 2045 semantics + $base64 = chunk_split(base64_encode($picture)); + $imageData = 'data:'.$imageDetails['mime'].';base64,' . $base64; + + $html .= '' . PHP_EOL; + + unlink($chartFileName); + } } } } @@ -1153,7 +1227,12 @@ class PHPExcel_Writer_HTML implements PHPExcel_Writer_IWriter { $html .= '>'; // Image? - $html .= $this->_writeImageTagInCell($pSheet, $coordinate); + $html .= $this->_writeImageInCell($pSheet, $coordinate); + + // Chart? + if ($this->_includeCharts) { + $html .= $this->_writeChartInCell($pSheet, $coordinate); + } // Cell data $html .= $cellData; @@ -1233,6 +1312,26 @@ class PHPExcel_Writer_HTML implements PHPExcel_Writer_IWriter { return $this; } + /** + * Get embed images + * + * @return boolean + */ + public function getEmbedImages() { + return $this->_embedImages; + } + + /** + * Set embed images + * + * @param boolean $pValue + * @return PHPExcel_Writer_HTML + */ + public function setEmbedImages($pValue = '.') { + $this->_embedImages = $pValue; + return $this; + } + /** * Get use inline CSS? * @@ -1374,4 +1473,29 @@ class PHPExcel_Writer_HTML implements PHPExcel_Writer_IWriter { $this->_spansAreCalculated = true; } + /** + * Write charts in workbook? + * If this is true, then the Writer will write definitions for any charts that exist in the PHPExcel object. + * If false (the default) it will ignore any charts defined in the PHPExcel object. + * + * @return boolean + */ + public function getIncludeCharts() { + return $this->_includeCharts; + } + + /** + * Set write charts in workbook + * Set to true, to advise the Writer to include any charts that exist in the PHPExcel object. + * Set to false (the default) to ignore charts. + * + * @param boolean $pValue + * + * @return PHPExcel_Writer_Excel2007 + */ + public function setIncludeCharts($pValue = false) { + $this->_includeCharts = (boolean) $pValue; + return $this; + } + }