AbstractRenderer.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. * @link https://github.com/PHPOffice/PhpWord
  14. * @copyright 2010-2014 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. * @throws \PhpOffice\PhpWord\Exception\Exception
  72. */
  73. public function __construct(PhpWord $phpWord)
  74. {
  75. parent::__construct($phpWord);
  76. $includeFile = Settings::getPdfRendererPath() . '/' . $this->includeFile;
  77. if (file_exists($includeFile)) {
  78. /** @noinspection PhpIncludeInspection Dynamic includes */
  79. require_once $includeFile;
  80. } else {
  81. // @codeCoverageIgnoreStart
  82. // Can't find any test case. Uncomment when found.
  83. throw new Exception('Unable to load PDF Rendering library');
  84. // @codeCoverageIgnoreEnd
  85. }
  86. }
  87. /**
  88. * Get Font
  89. *
  90. * @return string
  91. */
  92. public function getFont()
  93. {
  94. return $this->font;
  95. }
  96. /**
  97. * Set font. Examples:
  98. * 'arialunicid0-chinese-simplified'
  99. * 'arialunicid0-chinese-traditional'
  100. * 'arialunicid0-korean'
  101. * 'arialunicid0-japanese'
  102. *
  103. * @param string $fontName
  104. * @return self
  105. */
  106. public function setFont($fontName)
  107. {
  108. $this->font = $fontName;
  109. return $this;
  110. }
  111. /**
  112. * Get Paper Size
  113. *
  114. * @return int
  115. */
  116. public function getPaperSize()
  117. {
  118. return $this->paperSize;
  119. }
  120. /**
  121. * Set Paper Size
  122. *
  123. * @param int $value Paper size = PAPERSIZE_A4
  124. * @return self
  125. */
  126. public function setPaperSize($value = 9)
  127. {
  128. $this->paperSize = $value;
  129. return $this;
  130. }
  131. /**
  132. * Get Orientation
  133. *
  134. * @return string
  135. */
  136. public function getOrientation()
  137. {
  138. return $this->orientation;
  139. }
  140. /**
  141. * Set Orientation
  142. *
  143. * @param string $value Page orientation ORIENTATION_DEFAULT
  144. * @return self
  145. */
  146. public function setOrientation($value = 'default')
  147. {
  148. $this->orientation = $value;
  149. return $this;
  150. }
  151. /**
  152. * Save PhpWord to PDF file, pre-save
  153. *
  154. * @param string $filename Name of the file to save as
  155. * @return resource
  156. * @throws \PhpOffice\PhpWord\Exception\Exception
  157. */
  158. protected function prepareForSave($filename = null)
  159. {
  160. $fileHandle = fopen($filename, 'w');
  161. // @codeCoverageIgnoreStart
  162. // Can't find any test case. Uncomment when found.
  163. if ($fileHandle === false) {
  164. throw new Exception("Could not open file $filename for writing.");
  165. }
  166. // @codeCoverageIgnoreEnd
  167. $this->isPdf = true;
  168. return $fileHandle;
  169. }
  170. /**
  171. * Save PhpWord to PDF file, post-save
  172. *
  173. * @param resource $fileHandle
  174. * @return void
  175. * @throws Exception
  176. */
  177. protected function restoreStateAfterSave($fileHandle)
  178. {
  179. fclose($fileHandle);
  180. }
  181. }