PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/include/PHPExcel/Writer/PDF.php

https://bitbucket.org/sleininger/stock_online
PHP | 328 lines | 160 code | 36 blank | 132 comment | 12 complexity | c4ab5275fe71d5ac84dbf6a37817ca0e MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.7, 2012-05-19
  26. */
  27. /** Require FPDF library */
  28. $k_path_url = dirname(__FILE__) . '/PDF';
  29. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/PDF/tcpdf.php';
  30. /**
  31. * PHPExcel_Writer_PDF
  32. *
  33. * @category PHPExcel
  34. * @package PHPExcel_Writer
  35. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  36. */
  37. class PHPExcel_Writer_PDF extends PHPExcel_Writer_HTML implements PHPExcel_Writer_IWriter {
  38. /**
  39. * Temporary storage directory
  40. *
  41. * @var string
  42. */
  43. private $_tempDir = '';
  44. /**
  45. * Font
  46. *
  47. * @var string
  48. */
  49. private $_font = 'freesans';
  50. /**
  51. * Orientation (Over-ride)
  52. *
  53. * @var string
  54. */
  55. private $_orientation = null;
  56. /**
  57. * Paper size (Over-ride)
  58. *
  59. * @var int
  60. */
  61. private $_paperSize = null;
  62. /**
  63. * Paper Sizes xRef List
  64. *
  65. * @var array
  66. */
  67. private static $_paperSizes = array(
  68. // Excel Paper Size TCPDF Paper Size
  69. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER => 'LETTER', // (8.5 in. by 11 in.)
  70. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER_SMALL => 'LETTER', // (8.5 in. by 11 in.)
  71. PHPExcel_Worksheet_PageSetup::PAPERSIZE_TABLOID => array(792.00,1224.00), // (11 in. by 17 in.)
  72. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LEDGER => array(1224.00,792.00), // (17 in. by 11 in.)
  73. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LEGAL => 'LEGAL', // (8.5 in. by 14 in.)
  74. PHPExcel_Worksheet_PageSetup::PAPERSIZE_STATEMENT => array(396.00,612.00), // (5.5 in. by 8.5 in.)
  75. PHPExcel_Worksheet_PageSetup::PAPERSIZE_EXECUTIVE => 'EXECUTIVE', // (7.25 in. by 10.5 in.)
  76. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A3 => 'A3', // (297 mm by 420 mm)
  77. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4 => 'A4', // (210 mm by 297 mm)
  78. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4_SMALL => 'A4', // (210 mm by 297 mm)
  79. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A5 => 'A5', // (148 mm by 210 mm)
  80. PHPExcel_Worksheet_PageSetup::PAPERSIZE_B4 => 'B4', // (250 mm by 353 mm)
  81. PHPExcel_Worksheet_PageSetup::PAPERSIZE_B5 => 'B5', // (176 mm by 250 mm)
  82. PHPExcel_Worksheet_PageSetup::PAPERSIZE_FOLIO => 'FOLIO', // (8.5 in. by 13 in.)
  83. PHPExcel_Worksheet_PageSetup::PAPERSIZE_QUARTO => array(609.45,779.53), // (215 mm by 275 mm)
  84. PHPExcel_Worksheet_PageSetup::PAPERSIZE_STANDARD_1 => array(720.00,1008.00), // (10 in. by 14 in.)
  85. PHPExcel_Worksheet_PageSetup::PAPERSIZE_STANDARD_2 => array(792.00,1224.00), // (11 in. by 17 in.)
  86. PHPExcel_Worksheet_PageSetup::PAPERSIZE_NOTE => 'LETTER', // (8.5 in. by 11 in.)
  87. PHPExcel_Worksheet_PageSetup::PAPERSIZE_NO9_ENVELOPE => array(279.00,639.00), // (3.875 in. by 8.875 in.)
  88. PHPExcel_Worksheet_PageSetup::PAPERSIZE_NO10_ENVELOPE => array(297.00,684.00), // (4.125 in. by 9.5 in.)
  89. PHPExcel_Worksheet_PageSetup::PAPERSIZE_NO11_ENVELOPE => array(324.00,747.00), // (4.5 in. by 10.375 in.)
  90. PHPExcel_Worksheet_PageSetup::PAPERSIZE_NO12_ENVELOPE => array(342.00,792.00), // (4.75 in. by 11 in.)
  91. PHPExcel_Worksheet_PageSetup::PAPERSIZE_NO14_ENVELOPE => array(360.00,828.00), // (5 in. by 11.5 in.)
  92. PHPExcel_Worksheet_PageSetup::PAPERSIZE_C => array(1224.00,1584.00), // (17 in. by 22 in.)
  93. PHPExcel_Worksheet_PageSetup::PAPERSIZE_D => array(1584.00,2448.00), // (22 in. by 34 in.)
  94. PHPExcel_Worksheet_PageSetup::PAPERSIZE_E => array(2448.00,3168.00), // (34 in. by 44 in.)
  95. PHPExcel_Worksheet_PageSetup::PAPERSIZE_DL_ENVELOPE => array(311.81,623.62), // (110 mm by 220 mm)
  96. PHPExcel_Worksheet_PageSetup::PAPERSIZE_C5_ENVELOPE => 'C5', // (162 mm by 229 mm)
  97. PHPExcel_Worksheet_PageSetup::PAPERSIZE_C3_ENVELOPE => 'C3', // (324 mm by 458 mm)
  98. PHPExcel_Worksheet_PageSetup::PAPERSIZE_C4_ENVELOPE => 'C4', // (229 mm by 324 mm)
  99. PHPExcel_Worksheet_PageSetup::PAPERSIZE_C6_ENVELOPE => 'C6', // (114 mm by 162 mm)
  100. PHPExcel_Worksheet_PageSetup::PAPERSIZE_C65_ENVELOPE => array(323.15,649.13), // (114 mm by 229 mm)
  101. PHPExcel_Worksheet_PageSetup::PAPERSIZE_B4_ENVELOPE => 'B4', // (250 mm by 353 mm)
  102. PHPExcel_Worksheet_PageSetup::PAPERSIZE_B5_ENVELOPE => 'B5', // (176 mm by 250 mm)
  103. PHPExcel_Worksheet_PageSetup::PAPERSIZE_B6_ENVELOPE => array(498.90,354.33), // (176 mm by 125 mm)
  104. PHPExcel_Worksheet_PageSetup::PAPERSIZE_ITALY_ENVELOPE => array(311.81,651.97), // (110 mm by 230 mm)
  105. PHPExcel_Worksheet_PageSetup::PAPERSIZE_MONARCH_ENVELOPE => array(279.00,540.00), // (3.875 in. by 7.5 in.)
  106. PHPExcel_Worksheet_PageSetup::PAPERSIZE_6_3_4_ENVELOPE => array(261.00,468.00), // (3.625 in. by 6.5 in.)
  107. PHPExcel_Worksheet_PageSetup::PAPERSIZE_US_STANDARD_FANFOLD => array(1071.00,792.00), // (14.875 in. by 11 in.)
  108. PHPExcel_Worksheet_PageSetup::PAPERSIZE_GERMAN_STANDARD_FANFOLD => array(612.00,864.00), // (8.5 in. by 12 in.)
  109. PHPExcel_Worksheet_PageSetup::PAPERSIZE_GERMAN_LEGAL_FANFOLD => 'FOLIO', // (8.5 in. by 13 in.)
  110. PHPExcel_Worksheet_PageSetup::PAPERSIZE_ISO_B4 => 'B4', // (250 mm by 353 mm)
  111. PHPExcel_Worksheet_PageSetup::PAPERSIZE_JAPANESE_DOUBLE_POSTCARD => array(566.93,419.53), // (200 mm by 148 mm)
  112. PHPExcel_Worksheet_PageSetup::PAPERSIZE_STANDARD_PAPER_1 => array(648.00,792.00), // (9 in. by 11 in.)
  113. PHPExcel_Worksheet_PageSetup::PAPERSIZE_STANDARD_PAPER_2 => array(720.00,792.00), // (10 in. by 11 in.)
  114. PHPExcel_Worksheet_PageSetup::PAPERSIZE_STANDARD_PAPER_3 => array(1080.00,792.00), // (15 in. by 11 in.)
  115. PHPExcel_Worksheet_PageSetup::PAPERSIZE_INVITE_ENVELOPE => array(623.62,623.62), // (220 mm by 220 mm)
  116. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER_EXTRA_PAPER => array(667.80,864.00), // (9.275 in. by 12 in.)
  117. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LEGAL_EXTRA_PAPER => array(667.80,1080.00), // (9.275 in. by 15 in.)
  118. PHPExcel_Worksheet_PageSetup::PAPERSIZE_TABLOID_EXTRA_PAPER => array(841.68,1296.00), // (11.69 in. by 18 in.)
  119. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4_EXTRA_PAPER => array(668.98,912.76), // (236 mm by 322 mm)
  120. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER_TRANSVERSE_PAPER => array(595.80,792.00), // (8.275 in. by 11 in.)
  121. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4_TRANSVERSE_PAPER => 'A4', // (210 mm by 297 mm)
  122. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER_EXTRA_TRANSVERSE_PAPER => array(667.80,864.00), // (9.275 in. by 12 in.)
  123. PHPExcel_Worksheet_PageSetup::PAPERSIZE_SUPERA_SUPERA_A4_PAPER => array(643.46,1009.13), // (227 mm by 356 mm)
  124. PHPExcel_Worksheet_PageSetup::PAPERSIZE_SUPERB_SUPERB_A3_PAPER => array(864.57,1380.47), // (305 mm by 487 mm)
  125. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER_PLUS_PAPER => array(612.00,913.68), // (8.5 in. by 12.69 in.)
  126. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4_PLUS_PAPER => array(595.28,935.43), // (210 mm by 330 mm)
  127. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A5_TRANSVERSE_PAPER => 'A5', // (148 mm by 210 mm)
  128. PHPExcel_Worksheet_PageSetup::PAPERSIZE_JIS_B5_TRANSVERSE_PAPER => array(515.91,728.50), // (182 mm by 257 mm)
  129. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A3_EXTRA_PAPER => array(912.76,1261.42), // (322 mm by 445 mm)
  130. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A5_EXTRA_PAPER => array(493.23,666.14), // (174 mm by 235 mm)
  131. PHPExcel_Worksheet_PageSetup::PAPERSIZE_ISO_B5_EXTRA_PAPER => array(569.76,782.36), // (201 mm by 276 mm)
  132. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A2_PAPER => 'A2', // (420 mm by 594 mm)
  133. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A3_TRANSVERSE_PAPER => 'A3', // (297 mm by 420 mm)
  134. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A3_EXTRA_TRANSVERSE_PAPER => array(912.76,1261.42) // (322 mm by 445 mm)
  135. );
  136. /**
  137. * Create a new PHPExcel_Writer_PDF
  138. *
  139. * @param PHPExcel $phpExcel PHPExcel object
  140. */
  141. public function __construct(PHPExcel $phpExcel) {
  142. parent::__construct($phpExcel);
  143. $this->setUseInlineCss(true);
  144. $this->_tempDir = PHPExcel_Shared_File::sys_get_temp_dir();
  145. }
  146. /**
  147. * Set font. Examples:
  148. * 'arialunicid0-chinese-simplified'
  149. * 'arialunicid0-chinese-traditional'
  150. * 'arialunicid0-korean'
  151. * 'arialunicid0-japanese'
  152. *
  153. * @param string $fontName
  154. */
  155. public function setFont($fontName) {
  156. $this->_font = $fontName;
  157. return $this;
  158. }
  159. /**
  160. * Get Paper Size
  161. *
  162. * @return int
  163. */
  164. public function getPaperSize() {
  165. return $this->_paperSize;
  166. }
  167. /**
  168. * Set Paper Size
  169. *
  170. * @param int $pValue
  171. * @return PHPExcel_Writer_PDF
  172. */
  173. public function setPaperSize($pValue = PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER) {
  174. $this->_paperSize = $pValue;
  175. return $this;
  176. }
  177. /**
  178. * Get Orientation
  179. *
  180. * @return string
  181. */
  182. public function getOrientation() {
  183. return $this->_orientation;
  184. }
  185. /**
  186. * Set Orientation
  187. *
  188. * @param string $pValue
  189. * @return PHPExcel_Writer_PDF
  190. */
  191. public function setOrientation($pValue = PHPExcel_Worksheet_PageSetup::ORIENTATION_DEFAULT) {
  192. $this->_orientation = $pValue;
  193. return $this;
  194. }
  195. /**
  196. * Save PHPExcel to file
  197. *
  198. * @param string $pFilename Filename for the saved file
  199. * @throws Exception
  200. */
  201. public function save($pFilename = null) {
  202. // garbage collect
  203. $this->_phpExcel->garbageCollect();
  204. $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
  205. PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  206. // Open file
  207. $fileHandle = fopen($pFilename, 'w');
  208. if ($fileHandle === false) {
  209. throw new Exception("Could not open file $pFilename for writing.");
  210. }
  211. // Set PDF
  212. $this->_isPdf = true;
  213. // Build CSS
  214. $this->buildCSS(true);
  215. // Generate HTML
  216. $html = '';
  217. //$html .= $this->generateHTMLHeader(false);
  218. $html .= $this->generateSheetData();
  219. //$html .= $this->generateHTMLFooter();
  220. // Default PDF paper size
  221. $paperSize = 'LETTER'; // Letter (8.5 in. by 11 in.)
  222. // Check for paper size and page orientation
  223. if (is_null($this->getSheetIndex())) {
  224. $orientation = ($this->_phpExcel->getSheet(0)->getPageSetup()->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  225. $printPaperSize = $this->_phpExcel->getSheet(0)->getPageSetup()->getPaperSize();
  226. $printMargins = $this->_phpExcel->getSheet(0)->getPageMargins();
  227. } else {
  228. $orientation = ($this->_phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getOrientation() == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  229. $printPaperSize = $this->_phpExcel->getSheet($this->getSheetIndex())->getPageSetup()->getPaperSize();
  230. $printMargins = $this->_phpExcel->getSheet($this->getSheetIndex())->getPageMargins();
  231. }
  232. // Override Page Orientation
  233. if (!is_null($this->_orientation)) {
  234. $orientation = ($this->_orientation == PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE) ? 'L' : 'P';
  235. }
  236. // Override Paper Size
  237. if (!is_null($this->_paperSize)) {
  238. $printPaperSize = $this->_paperSize;
  239. }
  240. if (isset(self::$_paperSizes[$printPaperSize])) {
  241. $paperSize = self::$_paperSizes[$printPaperSize];
  242. }
  243. // Create PDF
  244. $pdf = new TCPDF($orientation, 'pt', $paperSize);
  245. $pdf->setFontSubsetting(false);
  246. // Set margins, converting inches to points (using 72 dpi)
  247. $pdf->SetMargins($printMargins->getLeft() * 72,$printMargins->getTop() * 72,$printMargins->getRight() * 72);
  248. $pdf->SetAutoPageBreak(true,$printMargins->getBottom() * 72);
  249. // $pdf->setHeaderMargin($printMargins->getHeader() * 72);
  250. // $pdf->setFooterMargin($printMargins->getFooter() * 72);
  251. $pdf->setPrintHeader(false);
  252. $pdf->setPrintFooter(false);
  253. $pdf->AddPage();
  254. // Set the appropriate font
  255. $pdf->SetFont($this->_font);
  256. $pdf->writeHTML($html);
  257. // Document info
  258. $pdf->SetTitle($this->_phpExcel->getProperties()->getTitle());
  259. $pdf->SetAuthor($this->_phpExcel->getProperties()->getCreator());
  260. $pdf->SetSubject($this->_phpExcel->getProperties()->getSubject());
  261. $pdf->SetKeywords($this->_phpExcel->getProperties()->getKeywords());
  262. $pdf->SetCreator($this->_phpExcel->getProperties()->getCreator());
  263. // Write to file
  264. fwrite($fileHandle, $pdf->output($pFilename, 'S'));
  265. // Close file
  266. fclose($fileHandle);
  267. PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  268. }
  269. /**
  270. * Get temporary storage directory
  271. *
  272. * @return string
  273. */
  274. public function getTempDir() {
  275. return $this->_tempDir;
  276. }
  277. /**
  278. * Set temporary storage directory
  279. *
  280. * @param string $pValue Temporary storage directory
  281. * @throws Exception Exception when directory does not exist
  282. * @return PHPExcel_Writer_PDF
  283. */
  284. public function setTempDir($pValue = '') {
  285. if (is_dir($pValue)) {
  286. $this->_tempDir = $pValue;
  287. } else {
  288. throw new Exception("Directory does not exist: $pValue");
  289. }
  290. return $this;
  291. }
  292. }