AbstractRenderer.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PhpWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Writer\PDF;
  18. use PhpOffice\PhpWord\Exception\Exception;
  19. use PhpOffice\PhpWord\PhpWord;
  20. use PhpOffice\PhpWord\Settings;
  21. use PhpOffice\PhpWord\Writer\HTML;
  22. /**
  23. * Abstract PDF renderer
  24. *
  25. * @since 0.10.0
  26. */
  27. abstract class AbstractRenderer extends HTML
  28. {
  29. /**
  30. * Name of renderer include file
  31. *
  32. * @var string
  33. */
  34. protected $includeFile;
  35. /**
  36. * Temporary storage directory
  37. *
  38. * @var string
  39. */
  40. protected $tempDir = '';
  41. /**
  42. * Font
  43. *
  44. * @var string
  45. */
  46. protected $font;
  47. /**
  48. * Paper size
  49. *
  50. * @var int
  51. */
  52. protected $paperSize;
  53. /**
  54. * Orientation
  55. *
  56. * @var string
  57. */
  58. protected $orientation;
  59. /**
  60. * Paper Sizes xRef List
  61. *
  62. * @var array
  63. */
  64. protected static $paperSizes = array(
  65. 9 => 'A4', // (210 mm by 297 mm)
  66. );
  67. /**
  68. * Create new instance
  69. *
  70. * @param PhpWord $phpWord PhpWord object
  71. *
  72. * @throws \PhpOffice\PhpWord\Exception\Exception
  73. */
  74. public function __construct(PhpWord $phpWord)
  75. {
  76. parent::__construct($phpWord);
  77. if ($this->includeFile != null) {
  78. $includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile;
  79. if (file_exists($includeFile)) {
  80. /** @noinspection PhpIncludeInspection Dynamic includes */
  81. require_once $includeFile;
  82. } else {
  83. // @codeCoverageIgnoreStart
  84. // Can't find any test case. Uncomment when found.
  85. throw new Exception('Unable to load PDF Rendering library');
  86. // @codeCoverageIgnoreEnd
  87. }
  88. }
  89. }
  90. /**
  91. * Get Font
  92. *
  93. * @return string
  94. */
  95. public function getFont()
  96. {
  97. return $this->font;
  98. }
  99. /**
  100. * Set font. Examples:
  101. * 'arialunicid0-chinese-simplified'
  102. * 'arialunicid0-chinese-traditional'
  103. * 'arialunicid0-korean'
  104. * 'arialunicid0-japanese'
  105. *
  106. * @param string $fontName
  107. * @return self
  108. */
  109. public function setFont($fontName)
  110. {
  111. $this->font = $fontName;
  112. return $this;
  113. }
  114. /**
  115. * Get Paper Size
  116. *
  117. * @return int
  118. */
  119. public function getPaperSize()
  120. {
  121. return $this->paperSize;
  122. }
  123. /**
  124. * Set Paper Size
  125. *
  126. * @param int $value Paper size = PAPERSIZE_A4
  127. * @return self
  128. */
  129. public function setPaperSize($value = 9)
  130. {
  131. $this->paperSize = $value;
  132. return $this;
  133. }
  134. /**
  135. * Get Orientation
  136. *
  137. * @return string
  138. */
  139. public function getOrientation()
  140. {
  141. return $this->orientation;
  142. }
  143. /**
  144. * Set Orientation
  145. *
  146. * @param string $value Page orientation ORIENTATION_DEFAULT
  147. * @return self
  148. */
  149. public function setOrientation($value = 'default')
  150. {
  151. $this->orientation = $value;
  152. return $this;
  153. }
  154. /**
  155. * Save PhpWord to PDF file, pre-save
  156. *
  157. * @param string $filename Name of the file to save as
  158. *
  159. * @throws \PhpOffice\PhpWord\Exception\Exception
  160. * @return resource
  161. */
  162. protected function prepareForSave($filename = null)
  163. {
  164. $fileHandle = fopen($filename, 'w');
  165. // @codeCoverageIgnoreStart
  166. // Can't find any test case. Uncomment when found.
  167. if ($fileHandle === false) {
  168. throw new Exception("Could not open file $filename for writing.");
  169. }
  170. // @codeCoverageIgnoreEnd
  171. $this->isPdf = true;
  172. return $fileHandle;
  173. }
  174. /**
  175. * Save PhpWord to PDF file, post-save
  176. *
  177. * @param resource $fileHandle
  178. *
  179. * @throws Exception
  180. */
  181. protected function restoreStateAfterSave($fileHandle)
  182. {
  183. fclose($fileHandle);
  184. }
  185. }