PageRenderTime 42ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/add-ons/PHPExcel/PHPExcel/Writer/PDF.php

https://github.com/jcplat/console-seolan
PHP | 181 lines | 64 code | 28 blank | 89 comment | 9 complexity | 18ae0242abd0fee3220c221c93debc90 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2009 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Writer
  23. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.1, 2009-11-02
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  33. }
  34. /** PHPExcel_IWriter */
  35. require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/IWriter.php';
  36. /** PHPExcel_Writer_HTML */
  37. require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/HTML.php';
  38. /** PHPExcel_Cell */
  39. require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
  40. /** PHPExcel_RichText */
  41. require_once PHPEXCEL_ROOT . 'PHPExcel/RichText.php';
  42. /** PHPExcel_Shared_Drawing */
  43. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/Drawing.php';
  44. /** PHPExcel_HashTable */
  45. require_once PHPEXCEL_ROOT . 'PHPExcel/HashTable.php';
  46. /** PHPExcel_Shared_PDF */
  47. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/PDF.php';
  48. /**
  49. * PHPExcel_Writer_PDF
  50. *
  51. * @category PHPExcel
  52. * @package PHPExcel_Writer
  53. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  54. */
  55. class PHPExcel_Writer_PDF extends PHPExcel_Writer_HTML implements PHPExcel_Writer_IWriter {
  56. /**
  57. * Temporary storage directory
  58. *
  59. * @var string
  60. */
  61. private $_tempDir = '';
  62. /**
  63. * Create a new PHPExcel_Writer_PDF
  64. *
  65. * @param PHPExcel $phpExcel PHPExcel object
  66. */
  67. public function __construct(PHPExcel $phpExcel) {
  68. parent::__construct($phpExcel);
  69. $this->setUseInlineCss(true);
  70. $this->_tempDir = sys_get_temp_dir();
  71. }
  72. /**
  73. * Save PHPExcel to file
  74. *
  75. * @param string $pFileName
  76. * @throws Exception
  77. */
  78. public function save($pFilename = null) {
  79. // garbage collect
  80. $this->_phpExcel->garbageCollect();
  81. $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
  82. PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  83. // Open file
  84. $fileHandle = fopen($pFilename, 'w');
  85. if ($fileHandle === false) {
  86. throw new Exception("Could not open file $pFilename for writing.");
  87. }
  88. // Set PDF
  89. $this->_isPdf = true;
  90. // Build CSS
  91. $this->buildCSS(true);
  92. // Generate HTML
  93. $html = '';
  94. //$html .= $this->generateHTMLHeader(false);
  95. $html .= $this->generateSheetData();
  96. //$html .= $this->generateHTMLFooter();
  97. // Default PDF paper size
  98. $paperSize = 'A4';
  99. $orientation = 'P';
  100. // Check for overrides
  101. if (is_null($this->getSheetIndex())) {
  102. $orientation = $this->_phpExcel->getSheet(0)->getPageSetup()->getOrientation() == 'landscape' ? 'L' : 'P';
  103. } else {
  104. $orientation = $this->_phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation() == 'landscape' ? 'L' : 'P';
  105. }
  106. // Create PDF
  107. $pdf = new TCPDF($orientation, 'pt', $paperSize);
  108. $pdf->setPrintHeader(false);
  109. $pdf->setPrintFooter(false);
  110. $pdf->AddPage();
  111. // Set the appropriate font
  112. $pdf->SetFont('freesans');
  113. //$pdf->SetFont('arialunicid0-chinese-simplified');
  114. //$pdf->SetFont('arialunicid0-chinese-traditional');
  115. //$pdf->SetFont('arialunicid0-korean');
  116. //$pdf->SetFont('arialunicid0-japanese');
  117. $pdf->writeHTML($html);
  118. // Document info
  119. $pdf->SetTitle($this->_phpExcel->getProperties()->getTitle());
  120. $pdf->SetAuthor($this->_phpExcel->getProperties()->getCreator());
  121. $pdf->SetSubject($this->_phpExcel->getProperties()->getSubject());
  122. $pdf->SetKeywords($this->_phpExcel->getProperties()->getKeywords());
  123. $pdf->SetCreator($this->_phpExcel->getProperties()->getCreator());
  124. // Write to file
  125. fwrite($fileHandle, $pdf->output($pFilename, 'S'));
  126. // Close file
  127. fclose($fileHandle);
  128. PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  129. }
  130. /**
  131. * Get temporary storage directory
  132. *
  133. * @return string
  134. */
  135. public function getTempDir() {
  136. return $this->_tempDir;
  137. }
  138. /**
  139. * Set temporary storage directory
  140. *
  141. * @param string $pValue Temporary storage directory
  142. * @throws Exception Exception when directory does not exist
  143. * @return PHPExcel_Writer_PDF
  144. */
  145. public function setTempDir($pValue = '') {
  146. if (is_dir($pValue)) {
  147. $this->_tempDir = $pValue;
  148. } else {
  149. throw new Exception("Directory does not exist: $pValue");
  150. }
  151. return $this;
  152. }
  153. }