Pdf.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Writer;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\Shared\File;
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
  7. use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
  8. abstract class Pdf extends Html
  9. {
  10. /**
  11. * Temporary storage directory.
  12. *
  13. * @var string
  14. */
  15. protected $tempDir = '';
  16. /**
  17. * Font.
  18. *
  19. * @var string
  20. */
  21. protected $font = 'freesans';
  22. /**
  23. * Orientation (Over-ride).
  24. *
  25. * @var string
  26. */
  27. protected $orientation;
  28. /**
  29. * Paper size (Over-ride).
  30. *
  31. * @var int
  32. */
  33. protected $paperSize;
  34. /**
  35. * Temporary storage for Save Array Return type.
  36. *
  37. * @var string
  38. */
  39. private $saveArrayReturnType;
  40. /**
  41. * Paper Sizes xRef List.
  42. *
  43. * @var array
  44. */
  45. protected static $paperSizes = [
  46. PageSetup::PAPERSIZE_LETTER => 'LETTER', // (8.5 in. by 11 in.)
  47. PageSetup::PAPERSIZE_LETTER_SMALL => 'LETTER', // (8.5 in. by 11 in.)
  48. PageSetup::PAPERSIZE_TABLOID => [792.00, 1224.00], // (11 in. by 17 in.)
  49. PageSetup::PAPERSIZE_LEDGER => [1224.00, 792.00], // (17 in. by 11 in.)
  50. PageSetup::PAPERSIZE_LEGAL => 'LEGAL', // (8.5 in. by 14 in.)
  51. PageSetup::PAPERSIZE_STATEMENT => [396.00, 612.00], // (5.5 in. by 8.5 in.)
  52. PageSetup::PAPERSIZE_EXECUTIVE => 'EXECUTIVE', // (7.25 in. by 10.5 in.)
  53. PageSetup::PAPERSIZE_A3 => 'A3', // (297 mm by 420 mm)
  54. PageSetup::PAPERSIZE_A4 => 'A4', // (210 mm by 297 mm)
  55. PageSetup::PAPERSIZE_A4_SMALL => 'A4', // (210 mm by 297 mm)
  56. PageSetup::PAPERSIZE_A5 => 'A5', // (148 mm by 210 mm)
  57. PageSetup::PAPERSIZE_B4 => 'B4', // (250 mm by 353 mm)
  58. PageSetup::PAPERSIZE_B5 => 'B5', // (176 mm by 250 mm)
  59. PageSetup::PAPERSIZE_FOLIO => 'FOLIO', // (8.5 in. by 13 in.)
  60. PageSetup::PAPERSIZE_QUARTO => [609.45, 779.53], // (215 mm by 275 mm)
  61. PageSetup::PAPERSIZE_STANDARD_1 => [720.00, 1008.00], // (10 in. by 14 in.)
  62. PageSetup::PAPERSIZE_STANDARD_2 => [792.00, 1224.00], // (11 in. by 17 in.)
  63. PageSetup::PAPERSIZE_NOTE => 'LETTER', // (8.5 in. by 11 in.)
  64. PageSetup::PAPERSIZE_NO9_ENVELOPE => [279.00, 639.00], // (3.875 in. by 8.875 in.)
  65. PageSetup::PAPERSIZE_NO10_ENVELOPE => [297.00, 684.00], // (4.125 in. by 9.5 in.)
  66. PageSetup::PAPERSIZE_NO11_ENVELOPE => [324.00, 747.00], // (4.5 in. by 10.375 in.)
  67. PageSetup::PAPERSIZE_NO12_ENVELOPE => [342.00, 792.00], // (4.75 in. by 11 in.)
  68. PageSetup::PAPERSIZE_NO14_ENVELOPE => [360.00, 828.00], // (5 in. by 11.5 in.)
  69. PageSetup::PAPERSIZE_C => [1224.00, 1584.00], // (17 in. by 22 in.)
  70. PageSetup::PAPERSIZE_D => [1584.00, 2448.00], // (22 in. by 34 in.)
  71. PageSetup::PAPERSIZE_E => [2448.00, 3168.00], // (34 in. by 44 in.)
  72. PageSetup::PAPERSIZE_DL_ENVELOPE => [311.81, 623.62], // (110 mm by 220 mm)
  73. PageSetup::PAPERSIZE_C5_ENVELOPE => 'C5', // (162 mm by 229 mm)
  74. PageSetup::PAPERSIZE_C3_ENVELOPE => 'C3', // (324 mm by 458 mm)
  75. PageSetup::PAPERSIZE_C4_ENVELOPE => 'C4', // (229 mm by 324 mm)
  76. PageSetup::PAPERSIZE_C6_ENVELOPE => 'C6', // (114 mm by 162 mm)
  77. PageSetup::PAPERSIZE_C65_ENVELOPE => [323.15, 649.13], // (114 mm by 229 mm)
  78. PageSetup::PAPERSIZE_B4_ENVELOPE => 'B4', // (250 mm by 353 mm)
  79. PageSetup::PAPERSIZE_B5_ENVELOPE => 'B5', // (176 mm by 250 mm)
  80. PageSetup::PAPERSIZE_B6_ENVELOPE => [498.90, 354.33], // (176 mm by 125 mm)
  81. PageSetup::PAPERSIZE_ITALY_ENVELOPE => [311.81, 651.97], // (110 mm by 230 mm)
  82. PageSetup::PAPERSIZE_MONARCH_ENVELOPE => [279.00, 540.00], // (3.875 in. by 7.5 in.)
  83. PageSetup::PAPERSIZE_6_3_4_ENVELOPE => [261.00, 468.00], // (3.625 in. by 6.5 in.)
  84. PageSetup::PAPERSIZE_US_STANDARD_FANFOLD => [1071.00, 792.00], // (14.875 in. by 11 in.)
  85. PageSetup::PAPERSIZE_GERMAN_STANDARD_FANFOLD => [612.00, 864.00], // (8.5 in. by 12 in.)
  86. PageSetup::PAPERSIZE_GERMAN_LEGAL_FANFOLD => 'FOLIO', // (8.5 in. by 13 in.)
  87. PageSetup::PAPERSIZE_ISO_B4 => 'B4', // (250 mm by 353 mm)
  88. PageSetup::PAPERSIZE_JAPANESE_DOUBLE_POSTCARD => [566.93, 419.53], // (200 mm by 148 mm)
  89. PageSetup::PAPERSIZE_STANDARD_PAPER_1 => [648.00, 792.00], // (9 in. by 11 in.)
  90. PageSetup::PAPERSIZE_STANDARD_PAPER_2 => [720.00, 792.00], // (10 in. by 11 in.)
  91. PageSetup::PAPERSIZE_STANDARD_PAPER_3 => [1080.00, 792.00], // (15 in. by 11 in.)
  92. PageSetup::PAPERSIZE_INVITE_ENVELOPE => [623.62, 623.62], // (220 mm by 220 mm)
  93. PageSetup::PAPERSIZE_LETTER_EXTRA_PAPER => [667.80, 864.00], // (9.275 in. by 12 in.)
  94. PageSetup::PAPERSIZE_LEGAL_EXTRA_PAPER => [667.80, 1080.00], // (9.275 in. by 15 in.)
  95. PageSetup::PAPERSIZE_TABLOID_EXTRA_PAPER => [841.68, 1296.00], // (11.69 in. by 18 in.)
  96. PageSetup::PAPERSIZE_A4_EXTRA_PAPER => [668.98, 912.76], // (236 mm by 322 mm)
  97. PageSetup::PAPERSIZE_LETTER_TRANSVERSE_PAPER => [595.80, 792.00], // (8.275 in. by 11 in.)
  98. PageSetup::PAPERSIZE_A4_TRANSVERSE_PAPER => 'A4', // (210 mm by 297 mm)
  99. PageSetup::PAPERSIZE_LETTER_EXTRA_TRANSVERSE_PAPER => [667.80, 864.00], // (9.275 in. by 12 in.)
  100. PageSetup::PAPERSIZE_SUPERA_SUPERA_A4_PAPER => [643.46, 1009.13], // (227 mm by 356 mm)
  101. PageSetup::PAPERSIZE_SUPERB_SUPERB_A3_PAPER => [864.57, 1380.47], // (305 mm by 487 mm)
  102. PageSetup::PAPERSIZE_LETTER_PLUS_PAPER => [612.00, 913.68], // (8.5 in. by 12.69 in.)
  103. PageSetup::PAPERSIZE_A4_PLUS_PAPER => [595.28, 935.43], // (210 mm by 330 mm)
  104. PageSetup::PAPERSIZE_A5_TRANSVERSE_PAPER => 'A5', // (148 mm by 210 mm)
  105. PageSetup::PAPERSIZE_JIS_B5_TRANSVERSE_PAPER => [515.91, 728.50], // (182 mm by 257 mm)
  106. PageSetup::PAPERSIZE_A3_EXTRA_PAPER => [912.76, 1261.42], // (322 mm by 445 mm)
  107. PageSetup::PAPERSIZE_A5_EXTRA_PAPER => [493.23, 666.14], // (174 mm by 235 mm)
  108. PageSetup::PAPERSIZE_ISO_B5_EXTRA_PAPER => [569.76, 782.36], // (201 mm by 276 mm)
  109. PageSetup::PAPERSIZE_A2_PAPER => 'A2', // (420 mm by 594 mm)
  110. PageSetup::PAPERSIZE_A3_TRANSVERSE_PAPER => 'A3', // (297 mm by 420 mm)
  111. PageSetup::PAPERSIZE_A3_EXTRA_TRANSVERSE_PAPER => [912.76, 1261.42], // (322 mm by 445 mm)
  112. ];
  113. /**
  114. * Create a new PDF Writer instance.
  115. *
  116. * @param Spreadsheet $spreadsheet Spreadsheet object
  117. */
  118. public function __construct(Spreadsheet $spreadsheet)
  119. {
  120. parent::__construct($spreadsheet);
  121. $this->setUseInlineCss(true);
  122. $this->tempDir = File::sysGetTempDir();
  123. }
  124. /**
  125. * Get Font.
  126. *
  127. * @return string
  128. */
  129. public function getFont()
  130. {
  131. return $this->font;
  132. }
  133. /**
  134. * Set font. Examples:
  135. * 'arialunicid0-chinese-simplified'
  136. * 'arialunicid0-chinese-traditional'
  137. * 'arialunicid0-korean'
  138. * 'arialunicid0-japanese'.
  139. *
  140. * @param string $fontName
  141. *
  142. * @return Pdf
  143. */
  144. public function setFont($fontName)
  145. {
  146. $this->font = $fontName;
  147. return $this;
  148. }
  149. /**
  150. * Get Paper Size.
  151. *
  152. * @return int
  153. */
  154. public function getPaperSize()
  155. {
  156. return $this->paperSize;
  157. }
  158. /**
  159. * Set Paper Size.
  160. *
  161. * @param string $pValue Paper size see PageSetup::PAPERSIZE_*
  162. *
  163. * @return self
  164. */
  165. public function setPaperSize($pValue)
  166. {
  167. $this->paperSize = $pValue;
  168. return $this;
  169. }
  170. /**
  171. * Get Orientation.
  172. *
  173. * @return string
  174. */
  175. public function getOrientation()
  176. {
  177. return $this->orientation;
  178. }
  179. /**
  180. * Set Orientation.
  181. *
  182. * @param string $pValue Page orientation see PageSetup::ORIENTATION_*
  183. *
  184. * @return self
  185. */
  186. public function setOrientation($pValue)
  187. {
  188. $this->orientation = $pValue;
  189. return $this;
  190. }
  191. /**
  192. * Get temporary storage directory.
  193. *
  194. * @return string
  195. */
  196. public function getTempDir()
  197. {
  198. return $this->tempDir;
  199. }
  200. /**
  201. * Set temporary storage directory.
  202. *
  203. * @param string $pValue Temporary storage directory
  204. *
  205. * @throws WriterException when directory does not exist
  206. *
  207. * @return self
  208. */
  209. public function setTempDir($pValue)
  210. {
  211. if (is_dir($pValue)) {
  212. $this->tempDir = $pValue;
  213. } else {
  214. throw new WriterException("Directory does not exist: $pValue");
  215. }
  216. return $this;
  217. }
  218. /**
  219. * Save Spreadsheet to PDF file, pre-save.
  220. *
  221. * @param string $pFilename Name of the file to save as
  222. *
  223. * @throws WriterException
  224. *
  225. * @return resource
  226. */
  227. protected function prepareForSave($pFilename)
  228. {
  229. // garbage collect
  230. $this->spreadsheet->garbageCollect();
  231. $this->saveArrayReturnType = Calculation::getArrayReturnType();
  232. Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE);
  233. // Open file
  234. $fileHandle = fopen($pFilename, 'w');
  235. if ($fileHandle === false) {
  236. throw new WriterException("Could not open file $pFilename for writing.");
  237. }
  238. // Set PDF
  239. $this->isPdf = true;
  240. // Build CSS
  241. $this->buildCSS(true);
  242. return $fileHandle;
  243. }
  244. /**
  245. * Save PhpSpreadsheet to PDF file, post-save.
  246. *
  247. * @param resource $fileHandle
  248. */
  249. protected function restoreStateAfterSave($fileHandle)
  250. {
  251. // Close file
  252. fclose($fileHandle);
  253. Calculation::setArrayReturnType($this->saveArrayReturnType);
  254. }
  255. }