暫無描述

36chartreadwritePDF.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /** Error reporting */
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', TRUE);
  5. ini_set('display_startup_errors', TRUE);
  6. date_default_timezone_set('Europe/London');
  7. define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
  8. date_default_timezone_set('Europe/London');
  9. /**
  10. * PHPExcel
  11. *
  12. * Copyright (C) 2006 - 2014 PHPExcel
  13. *
  14. * This library is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Lesser General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2.1 of the License, or (at your option) any later version.
  18. *
  19. * This library is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with this library; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. *
  28. * @category PHPExcel
  29. * @package PHPExcel
  30. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  31. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  32. * @version ##VERSION##, ##DATE##
  33. */
  34. /** Include path **/
  35. set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/../Classes/');
  36. /** PHPExcel_IOFactory */
  37. include 'PHPExcel/IOFactory.php';
  38. // Change these values to select the Rendering library that you wish to use
  39. // for PDF files, and its directory location on your server
  40. //$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
  41. $rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
  42. //$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
  43. //$rendererLibrary = 'tcPDF5.9';
  44. $rendererLibrary = 'mPDF5.4';
  45. //$rendererLibrary = 'domPDF0.6.0beta3';
  46. $rendererLibraryPath = '/php/libraries/PDF/' . $rendererLibrary;
  47. if (!PHPExcel_Settings::setPdfRenderer(
  48. $rendererName,
  49. $rendererLibraryPath
  50. )) {
  51. die(
  52. 'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
  53. EOL .
  54. 'at the top of this script as appropriate for your directory structure'
  55. );
  56. }
  57. // Change these values to select the Rendering library that you wish to use
  58. // for Chart images, and its directory location on your server
  59. $rendererName = PHPExcel_Settings::CHART_RENDERER_JPGRAPH;
  60. $rendererLibrary = 'jpgraph3.5.0b1/src';
  61. $rendererLibraryPath = '/php/libraries/Charts/' . $rendererLibrary;
  62. if (!PHPExcel_Settings::setChartRenderer(
  63. $rendererName,
  64. $rendererLibraryPath
  65. )) {
  66. die(
  67. 'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
  68. EOL .
  69. 'at the top of this script as appropriate for your directory structure'
  70. );
  71. }
  72. $inputFileType = 'Excel2007';
  73. $inputFileNames = 'templates/36write*.xlsx';
  74. if ((isset($argc)) && ($argc > 1)) {
  75. $inputFileNames = array();
  76. for($i = 1; $i < $argc; ++$i) {
  77. $inputFileNames[] = dirname(__FILE__) . '/templates/' . $argv[$i];
  78. }
  79. } else {
  80. $inputFileNames = glob($inputFileNames);
  81. }
  82. foreach($inputFileNames as $inputFileName) {
  83. $inputFileNameShort = basename($inputFileName);
  84. if (!file_exists($inputFileName)) {
  85. echo date('H:i:s') , " File " , $inputFileNameShort , ' does not exist' , EOL;
  86. continue;
  87. }
  88. echo date('H:i:s') , " Load Test from $inputFileType file " , $inputFileNameShort , EOL;
  89. $objReader = PHPExcel_IOFactory::createReader($inputFileType);
  90. $objReader->setIncludeCharts(TRUE);
  91. $objPHPExcel = $objReader->load($inputFileName);
  92. echo date('H:i:s') , " Iterate worksheets looking at the charts" , EOL;
  93. foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
  94. $sheetName = $worksheet->getTitle();
  95. echo 'Worksheet: ' , $sheetName , EOL;
  96. $chartNames = $worksheet->getChartNames();
  97. if(empty($chartNames)) {
  98. echo ' There are no charts in this worksheet' , EOL;
  99. } else {
  100. natsort($chartNames);
  101. foreach($chartNames as $i => $chartName) {
  102. $chart = $worksheet->getChartByName($chartName);
  103. if (!is_null($chart->getTitle())) {
  104. $caption = '"' . implode(' ',$chart->getTitle()->getCaption()) . '"';
  105. } else {
  106. $caption = 'Untitled';
  107. }
  108. echo ' ' , $chartName , ' - ' , $caption , EOL;
  109. echo str_repeat(' ',strlen($chartName)+3);
  110. $groupCount = $chart->getPlotArea()->getPlotGroupCount();
  111. if ($groupCount == 1) {
  112. $chartType = $chart->getPlotArea()->getPlotGroupByIndex(0)->getPlotType();
  113. echo ' ' , $chartType , EOL;
  114. } else {
  115. $chartTypes = array();
  116. for($i = 0; $i < $groupCount; ++$i) {
  117. $chartTypes[] = $chart->getPlotArea()->getPlotGroupByIndex($i)->getPlotType();
  118. }
  119. $chartTypes = array_unique($chartTypes);
  120. if (count($chartTypes) == 1) {
  121. $chartType = 'Multiple Plot ' . array_pop($chartTypes);
  122. echo ' ' , $chartType , EOL;
  123. } elseif (count($chartTypes) == 0) {
  124. echo ' *** Type not yet implemented' , EOL;
  125. } else {
  126. echo ' Combination Chart' , EOL;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. $outputFileName = str_replace('.xlsx', '.pdf', basename($inputFileName));
  133. echo date('H:i:s') , " Write Tests to HTML file " , EOL;
  134. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
  135. $objWriter->setIncludeCharts(TRUE);
  136. $objWriter->save($outputFileName);
  137. echo date('H:i:s') , " File written to " , $outputFileName , EOL;
  138. $objPHPExcel->disconnectWorksheets();
  139. unset($objPHPExcel);
  140. }
  141. // Echo memory peak usage
  142. echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;
  143. // Echo done
  144. echo date('H:i:s') , " Done writing files" , EOL;
  145. echo 'Files have been created in ' , getcwd() , EOL;