Core.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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_PDF
  23. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  26. */
  27. /**
  28. * PHPExcel_Writer_PDF_Core
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer_PDF
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. abstract class PHPExcel_Writer_PDF_Core extends PHPExcel_Writer_HTML
  35. {
  36. /**
  37. * Temporary storage directory
  38. *
  39. * @var string
  40. */
  41. protected $_tempDir = '';
  42. /**
  43. * Font
  44. *
  45. * @var string
  46. */
  47. protected $_font = 'freesans';
  48. /**
  49. * Orientation (Over-ride)
  50. *
  51. * @var string
  52. */
  53. protected $_orientation = NULL;
  54. /**
  55. * Paper size (Over-ride)
  56. *
  57. * @var int
  58. */
  59. protected $_paperSize = NULL;
  60. /**
  61. * Temporary storage for Save Array Return type
  62. *
  63. * @var string
  64. */
  65. private $_saveArrayReturnType;
  66. /**
  67. * Paper Sizes xRef List
  68. *
  69. * @var array
  70. */
  71. protected static $_paperSizes = array(
  72. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER
  73. => 'LETTER', // (8.5 in. by 11 in.)
  74. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER_SMALL
  75. => 'LETTER', // (8.5 in. by 11 in.)
  76. PHPExcel_Worksheet_PageSetup::PAPERSIZE_TABLOID
  77. => array(792.00, 1224.00), // (11 in. by 17 in.)
  78. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LEDGER
  79. => array(1224.00, 792.00), // (17 in. by 11 in.)
  80. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LEGAL
  81. => 'LEGAL', // (8.5 in. by 14 in.)
  82. PHPExcel_Worksheet_PageSetup::PAPERSIZE_STATEMENT
  83. => array(396.00, 612.00), // (5.5 in. by 8.5 in.)
  84. PHPExcel_Worksheet_PageSetup::PAPERSIZE_EXECUTIVE
  85. => 'EXECUTIVE', // (7.25 in. by 10.5 in.)
  86. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A3
  87. => 'A3', // (297 mm by 420 mm)
  88. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4
  89. => 'A4', // (210 mm by 297 mm)
  90. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4_SMALL
  91. => 'A4', // (210 mm by 297 mm)
  92. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A5
  93. => 'A5', // (148 mm by 210 mm)
  94. PHPExcel_Worksheet_PageSetup::PAPERSIZE_B4
  95. => 'B4', // (250 mm by 353 mm)
  96. PHPExcel_Worksheet_PageSetup::PAPERSIZE_B5
  97. => 'B5', // (176 mm by 250 mm)
  98. PHPExcel_Worksheet_PageSetup::PAPERSIZE_FOLIO
  99. => 'FOLIO', // (8.5 in. by 13 in.)
  100. PHPExcel_Worksheet_PageSetup::PAPERSIZE_QUARTO
  101. => array(609.45, 779.53), // (215 mm by 275 mm)
  102. PHPExcel_Worksheet_PageSetup::PAPERSIZE_STANDARD_1
  103. => array(720.00, 1008.00), // (10 in. by 14 in.)
  104. PHPExcel_Worksheet_PageSetup::PAPERSIZE_STANDARD_2
  105. => array(792.00, 1224.00), // (11 in. by 17 in.)
  106. PHPExcel_Worksheet_PageSetup::PAPERSIZE_NOTE
  107. => 'LETTER', // (8.5 in. by 11 in.)
  108. PHPExcel_Worksheet_PageSetup::PAPERSIZE_NO9_ENVELOPE
  109. => array(279.00, 639.00), // (3.875 in. by 8.875 in.)
  110. PHPExcel_Worksheet_PageSetup::PAPERSIZE_NO10_ENVELOPE
  111. => array(297.00, 684.00), // (4.125 in. by 9.5 in.)
  112. PHPExcel_Worksheet_PageSetup::PAPERSIZE_NO11_ENVELOPE
  113. => array(324.00, 747.00), // (4.5 in. by 10.375 in.)
  114. PHPExcel_Worksheet_PageSetup::PAPERSIZE_NO12_ENVELOPE
  115. => array(342.00, 792.00), // (4.75 in. by 11 in.)
  116. PHPExcel_Worksheet_PageSetup::PAPERSIZE_NO14_ENVELOPE
  117. => array(360.00, 828.00), // (5 in. by 11.5 in.)
  118. PHPExcel_Worksheet_PageSetup::PAPERSIZE_C
  119. => array(1224.00, 1584.00), // (17 in. by 22 in.)
  120. PHPExcel_Worksheet_PageSetup::PAPERSIZE_D
  121. => array(1584.00, 2448.00), // (22 in. by 34 in.)
  122. PHPExcel_Worksheet_PageSetup::PAPERSIZE_E
  123. => array(2448.00, 3168.00), // (34 in. by 44 in.)
  124. PHPExcel_Worksheet_PageSetup::PAPERSIZE_DL_ENVELOPE
  125. => array(311.81, 623.62), // (110 mm by 220 mm)
  126. PHPExcel_Worksheet_PageSetup::PAPERSIZE_C5_ENVELOPE
  127. => 'C5', // (162 mm by 229 mm)
  128. PHPExcel_Worksheet_PageSetup::PAPERSIZE_C3_ENVELOPE
  129. => 'C3', // (324 mm by 458 mm)
  130. PHPExcel_Worksheet_PageSetup::PAPERSIZE_C4_ENVELOPE
  131. => 'C4', // (229 mm by 324 mm)
  132. PHPExcel_Worksheet_PageSetup::PAPERSIZE_C6_ENVELOPE
  133. => 'C6', // (114 mm by 162 mm)
  134. PHPExcel_Worksheet_PageSetup::PAPERSIZE_C65_ENVELOPE
  135. => array(323.15, 649.13), // (114 mm by 229 mm)
  136. PHPExcel_Worksheet_PageSetup::PAPERSIZE_B4_ENVELOPE
  137. => 'B4', // (250 mm by 353 mm)
  138. PHPExcel_Worksheet_PageSetup::PAPERSIZE_B5_ENVELOPE
  139. => 'B5', // (176 mm by 250 mm)
  140. PHPExcel_Worksheet_PageSetup::PAPERSIZE_B6_ENVELOPE
  141. => array(498.90, 354.33), // (176 mm by 125 mm)
  142. PHPExcel_Worksheet_PageSetup::PAPERSIZE_ITALY_ENVELOPE
  143. => array(311.81, 651.97), // (110 mm by 230 mm)
  144. PHPExcel_Worksheet_PageSetup::PAPERSIZE_MONARCH_ENVELOPE
  145. => array(279.00, 540.00), // (3.875 in. by 7.5 in.)
  146. PHPExcel_Worksheet_PageSetup::PAPERSIZE_6_3_4_ENVELOPE
  147. => array(261.00, 468.00), // (3.625 in. by 6.5 in.)
  148. PHPExcel_Worksheet_PageSetup::PAPERSIZE_US_STANDARD_FANFOLD
  149. => array(1071.00, 792.00), // (14.875 in. by 11 in.)
  150. PHPExcel_Worksheet_PageSetup::PAPERSIZE_GERMAN_STANDARD_FANFOLD
  151. => array(612.00, 864.00), // (8.5 in. by 12 in.)
  152. PHPExcel_Worksheet_PageSetup::PAPERSIZE_GERMAN_LEGAL_FANFOLD
  153. => 'FOLIO', // (8.5 in. by 13 in.)
  154. PHPExcel_Worksheet_PageSetup::PAPERSIZE_ISO_B4
  155. => 'B4', // (250 mm by 353 mm)
  156. PHPExcel_Worksheet_PageSetup::PAPERSIZE_JAPANESE_DOUBLE_POSTCARD
  157. => array(566.93, 419.53), // (200 mm by 148 mm)
  158. PHPExcel_Worksheet_PageSetup::PAPERSIZE_STANDARD_PAPER_1
  159. => array(648.00, 792.00), // (9 in. by 11 in.)
  160. PHPExcel_Worksheet_PageSetup::PAPERSIZE_STANDARD_PAPER_2
  161. => array(720.00, 792.00), // (10 in. by 11 in.)
  162. PHPExcel_Worksheet_PageSetup::PAPERSIZE_STANDARD_PAPER_3
  163. => array(1080.00, 792.00), // (15 in. by 11 in.)
  164. PHPExcel_Worksheet_PageSetup::PAPERSIZE_INVITE_ENVELOPE
  165. => array(623.62, 623.62), // (220 mm by 220 mm)
  166. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER_EXTRA_PAPER
  167. => array(667.80, 864.00), // (9.275 in. by 12 in.)
  168. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LEGAL_EXTRA_PAPER
  169. => array(667.80, 1080.00), // (9.275 in. by 15 in.)
  170. PHPExcel_Worksheet_PageSetup::PAPERSIZE_TABLOID_EXTRA_PAPER
  171. => array(841.68, 1296.00), // (11.69 in. by 18 in.)
  172. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4_EXTRA_PAPER
  173. => array(668.98, 912.76), // (236 mm by 322 mm)
  174. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER_TRANSVERSE_PAPER
  175. => array(595.80, 792.00), // (8.275 in. by 11 in.)
  176. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4_TRANSVERSE_PAPER
  177. => 'A4', // (210 mm by 297 mm)
  178. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER_EXTRA_TRANSVERSE_PAPER
  179. => array(667.80, 864.00), // (9.275 in. by 12 in.)
  180. PHPExcel_Worksheet_PageSetup::PAPERSIZE_SUPERA_SUPERA_A4_PAPER
  181. => array(643.46, 1009.13), // (227 mm by 356 mm)
  182. PHPExcel_Worksheet_PageSetup::PAPERSIZE_SUPERB_SUPERB_A3_PAPER
  183. => array(864.57, 1380.47), // (305 mm by 487 mm)
  184. PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER_PLUS_PAPER
  185. => array(612.00, 913.68), // (8.5 in. by 12.69 in.)
  186. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4_PLUS_PAPER
  187. => array(595.28, 935.43), // (210 mm by 330 mm)
  188. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A5_TRANSVERSE_PAPER
  189. => 'A5', // (148 mm by 210 mm)
  190. PHPExcel_Worksheet_PageSetup::PAPERSIZE_JIS_B5_TRANSVERSE_PAPER
  191. => array(515.91, 728.50), // (182 mm by 257 mm)
  192. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A3_EXTRA_PAPER
  193. => array(912.76, 1261.42), // (322 mm by 445 mm)
  194. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A5_EXTRA_PAPER
  195. => array(493.23, 666.14), // (174 mm by 235 mm)
  196. PHPExcel_Worksheet_PageSetup::PAPERSIZE_ISO_B5_EXTRA_PAPER
  197. => array(569.76, 782.36), // (201 mm by 276 mm)
  198. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A2_PAPER
  199. => 'A2', // (420 mm by 594 mm)
  200. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A3_TRANSVERSE_PAPER
  201. => 'A3', // (297 mm by 420 mm)
  202. PHPExcel_Worksheet_PageSetup::PAPERSIZE_A3_EXTRA_TRANSVERSE_PAPER
  203. => array(912.76, 1261.42) // (322 mm by 445 mm)
  204. );
  205. /**
  206. * Create a new PHPExcel_Writer_PDF
  207. *
  208. * @param PHPExcel $phpExcel PHPExcel object
  209. */
  210. public function __construct(PHPExcel $phpExcel)
  211. {
  212. parent::__construct($phpExcel);
  213. $this->setUseInlineCss(TRUE);
  214. $this->_tempDir = PHPExcel_Shared_File::sys_get_temp_dir();
  215. }
  216. /**
  217. * Get Font
  218. *
  219. * @return string
  220. */
  221. public function getFont()
  222. {
  223. return $this->_font;
  224. }
  225. /**
  226. * Set font. Examples:
  227. * 'arialunicid0-chinese-simplified'
  228. * 'arialunicid0-chinese-traditional'
  229. * 'arialunicid0-korean'
  230. * 'arialunicid0-japanese'
  231. *
  232. * @param string $fontName
  233. */
  234. public function setFont($fontName)
  235. {
  236. $this->_font = $fontName;
  237. return $this;
  238. }
  239. /**
  240. * Get Paper Size
  241. *
  242. * @return int
  243. */
  244. public function getPaperSize()
  245. {
  246. return $this->_paperSize;
  247. }
  248. /**
  249. * Set Paper Size
  250. *
  251. * @param string $pValue Paper size
  252. * @return PHPExcel_Writer_PDF
  253. */
  254. public function setPaperSize($pValue = PHPExcel_Worksheet_PageSetup::PAPERSIZE_LETTER)
  255. {
  256. $this->_paperSize = $pValue;
  257. return $this;
  258. }
  259. /**
  260. * Get Orientation
  261. *
  262. * @return string
  263. */
  264. public function getOrientation()
  265. {
  266. return $this->_orientation;
  267. }
  268. /**
  269. * Set Orientation
  270. *
  271. * @param string $pValue Page orientation
  272. * @return PHPExcel_Writer_PDF
  273. */
  274. public function setOrientation($pValue = PHPExcel_Worksheet_PageSetup::ORIENTATION_DEFAULT)
  275. {
  276. $this->_orientation = $pValue;
  277. return $this;
  278. }
  279. /**
  280. * Get temporary storage directory
  281. *
  282. * @return string
  283. */
  284. public function getTempDir()
  285. {
  286. return $this->_tempDir;
  287. }
  288. /**
  289. * Set temporary storage directory
  290. *
  291. * @param string $pValue Temporary storage directory
  292. * @throws PHPExcel_Writer_Exception when directory does not exist
  293. * @return PHPExcel_Writer_PDF
  294. */
  295. public function setTempDir($pValue = '')
  296. {
  297. if (is_dir($pValue)) {
  298. $this->_tempDir = $pValue;
  299. } else {
  300. throw new PHPExcel_Writer_Exception("Directory does not exist: $pValue");
  301. }
  302. return $this;
  303. }
  304. /**
  305. * Save PHPExcel to PDF file, pre-save
  306. *
  307. * @param string $pFilename Name of the file to save as
  308. * @throws PHPExcel_Writer_Exception
  309. */
  310. protected function prepareForSave($pFilename = NULL)
  311. {
  312. // garbage collect
  313. $this->_phpExcel->garbageCollect();
  314. $this->_saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
  315. PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  316. // Open file
  317. $fileHandle = fopen($pFilename, 'w');
  318. if ($fileHandle === FALSE) {
  319. throw new PHPExcel_Writer_Exception("Could not open file $pFilename for writing.");
  320. }
  321. // Set PDF
  322. $this->_isPdf = TRUE;
  323. // Build CSS
  324. $this->buildCSS(TRUE);
  325. return $fileHandle;
  326. }
  327. /**
  328. * Save PHPExcel to PDF file, post-save
  329. *
  330. * @param resource $fileHandle
  331. * @throws PHPExcel_Writer_Exception
  332. */
  333. protected function restoreStateAfterSave($fileHandle)
  334. {
  335. // Close file
  336. fclose($fileHandle);
  337. PHPExcel_Calculation::setArrayReturnType($this->_saveArrayReturnType);
  338. }
  339. }