<?php
	error_reporting(E_ERROR);
	set_time_limit(0);
	date_default_timezone_set('Europe/London');
?>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

	<title>PHPExcel Reader Example #01</title>

</head>
<body>

	<h1>PHPExcel Reader Example #01</h1>
	<h2>Simple File Reader using PHPExcel_IOFactory::load()</h2>
	<?php
		define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
		include 'includes/PHPExcel/IOFactory.php';
		
		$PDFrendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
		$PDFrendererLibraryPath = 'includes/MPDF/';
		$JPrendererName = PHPExcel_Settings::CHART_RENDERER_JPGRAPH;
		$JPrendererLibraryPath = 'includes/jpgraph/src';


		if (!PHPExcel_Settings::setPdfRenderer($PDFrendererName, $PDFrendererLibraryPath)) {
			die("Error loading PDF-Lib!");
		}
		if (!PHPExcel_Settings::setChartRenderer($JPrendererName, $JPrendererLibraryPath)) {
			die("Errorloading JPGraph!");
		}

		


		
		$inputFileName = 'resulting_excel/test.xlsx';

		
		$objReader = PHPExcel_IOFactory::createReader("Excel2007");
		$objReader->setIncludeCharts(TRUE);
		
		$objPHPExcel = $objReader->load($inputFileName);



		foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
			$sheetName = $worksheet->getTitle();
			echo 'Worksheet: ' , $sheetName , EOL;
	
			$chartNames = $worksheet->getChartNames();
			if(empty($chartNames)) {
				echo '    There are no charts in this worksheet' , EOL;
			} else {
				natsort($chartNames);
				foreach($chartNames as $i => $chartName) {
					$chart = $worksheet->getChartByName($chartName);
					if (!is_null($chart->getTitle())) {
						$caption = '"' . implode(' ',$chart->getTitle()->getCaption()) . '"';
					} else {
						$caption = 'Untitled';
					}
					echo '    ' , $chartName , ' - ' , $caption , EOL;
					echo str_repeat(' ',strlen($chartName)+3);
					$groupCount = $chart->getPlotArea()->getPlotGroupCount();
					if ($groupCount == 1) {
						$chartType = $chart->getPlotArea()->getPlotGroupByIndex(0)->getPlotType();
						echo '    ' , $chartType , EOL;
					} else {
						$chartTypes = array();
						for($i = 0; $i < $groupCount; ++$i) {
							$chartTypes[] = $chart->getPlotArea()->getPlotGroupByIndex($i)->getPlotType();
						}
						$chartTypes = array_unique($chartTypes);
						if (count($chartTypes) == 1) {
							$chartType = 'Multiple Plot ' . array_pop($chartTypes);
							echo '    ' , $chartType , EOL;
						} elseif (count($chartTypes) == 0) {
							echo '    *** Type not yet implemented' , EOL;
						} else {
							echo '    Combination Chart' , EOL;
						}
					}
				}
			}
		}

		$outputFileName = str_replace('.xlsx', '.pdf', basename($inputFileName));
	
		echo date('H:i:s') , " Write Tests to HTML file " , EOL;
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
		$objWriter->setIncludeCharts(TRUE);
		$objWriter->save($outputFileName);
		echo date('H:i:s') , " File written to " , $outputFileName , EOL;
	
		$objPHPExcel->disconnectWorksheets();
		unset($objPHPExcel);



//		$objPHPExcel->setActiveSheetIndex(16);
//		//echo date('H:i:s') , " Hide grid lines" , EOL;
//		$objPHPExcel->getActiveSheet()->setShowGridLines(false);
//		
//		//echo date('H:i:s') , " Set orientation to landscape" , EOL;
//		//$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
//		
//		
//		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
//		$objWriter->setSheetIndex(16);
//		$objWriter->save(str_replace('.php', '_'.$rendererName.'.pdf', __FILE__));






		// Echo done
		echo date('H:i:s') , " Done writing files" , EOL;
		echo 'File has been created in ' , getcwd() , EOL;



	?>
<body>
</html>