Core.php 14 KB

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