No Description

PhpWord.php 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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;
  18. use PhpOffice\PhpWord\Element\Section;
  19. use PhpOffice\PhpWord\Exception\Exception;
  20. /**
  21. * PHPWord main class
  22. *
  23. * @method Collection\Titles getTitles()
  24. * @method Collection\Footnotes getFootnotes()
  25. * @method Collection\Endnotes getEndnotes()
  26. * @method Collection\Charts getCharts()
  27. * @method int addBookmark(Element\Bookmark $bookmark)
  28. * @method int addTitle(Element\Title $title)
  29. * @method int addFootnote(Element\Footnote $footnote)
  30. * @method int addEndnote(Element\Endnote $endnote)
  31. * @method int addChart(Element\Chart $chart)
  32. *
  33. * @method Style\Paragraph addParagraphStyle(string $styleName, array $styles)
  34. * @method Style\Font addFontStyle(string $styleName, mixed $fontStyle, mixed $paragraphStyle = null)
  35. * @method Style\Font addLinkStyle(string $styleName, mixed $styles)
  36. * @method Style\Font addTitleStyle(int $depth, mixed $fontStyle, mixed $paragraphStyle = null)
  37. * @method Style\Table addTableStyle(string $styleName, mixed $styleTable, mixed $styleFirstRow = null)
  38. * @method Style\Numbering addNumberingStyle(string $styleName, mixed $styles)
  39. */
  40. class PhpWord
  41. {
  42. /**
  43. * Default font settings
  44. *
  45. * @const string|int
  46. * @deprecated 0.11.0 Use Settings constants
  47. */
  48. const DEFAULT_FONT_NAME = Settings::DEFAULT_FONT_NAME;
  49. const DEFAULT_FONT_SIZE = Settings::DEFAULT_FONT_SIZE;
  50. const DEFAULT_FONT_COLOR = Settings::DEFAULT_FONT_COLOR;
  51. const DEFAULT_FONT_CONTENT_TYPE = Settings::DEFAULT_FONT_CONTENT_TYPE;
  52. /**
  53. * Collection of sections
  54. *
  55. * @var \PhpOffice\PhpWord\Element\Section[]
  56. */
  57. private $sections = array();
  58. /**
  59. * Collections
  60. *
  61. * @var array
  62. */
  63. private $collections = array();
  64. /**
  65. * Metadata
  66. *
  67. * @var array
  68. * @since 0.12.0
  69. */
  70. private $metadata = array();
  71. /**
  72. * Create new instance
  73. *
  74. * Collections are created dynamically
  75. */
  76. public function __construct()
  77. {
  78. // Collection
  79. $collections = array('Bookmarks', 'Titles', 'Footnotes', 'Endnotes', 'Charts');
  80. foreach ($collections as $collection) {
  81. $class = 'PhpOffice\\PhpWord\\Collection\\' . $collection;
  82. $this->collections[$collection] = new $class();
  83. }
  84. // Metadata
  85. $metadata = array('DocInfo', 'Protection', 'Compatibility');
  86. foreach ($metadata as $meta) {
  87. $class = 'PhpOffice\\PhpWord\\Metadata\\' . $meta;
  88. $this->metadata[$meta] = new $class();
  89. }
  90. }
  91. /**
  92. * Dynamic function call to reduce static dependency
  93. *
  94. * @param mixed $function
  95. * @param mixed $args
  96. * @throws \BadMethodCallException
  97. * @return mixed
  98. * @since 0.12.0
  99. */
  100. public function __call($function, $args)
  101. {
  102. $function = strtolower($function);
  103. $getCollection = array();
  104. $addCollection = array();
  105. $addStyle = array();
  106. $collections = array('Bookmark', 'Title', 'Footnote', 'Endnote', 'Chart');
  107. foreach ($collections as $collection) {
  108. $getCollection[] = strtolower("get{$collection}s");
  109. $addCollection[] = strtolower("add{$collection}");
  110. }
  111. $styles = array('Paragraph', 'Font', 'Table', 'Numbering', 'Link', 'Title');
  112. foreach ($styles as $style) {
  113. $addStyle[] = strtolower("add{$style}Style");
  114. }
  115. // Run get collection method
  116. if (in_array($function, $getCollection)) {
  117. $key = ucfirst(str_replace('get', '', $function));
  118. return $this->collections[$key];
  119. }
  120. // Run add collection item method
  121. if (in_array($function, $addCollection)) {
  122. $key = ucfirst(str_replace('add', '', $function) . 's');
  123. /** @var \PhpOffice\PhpWord\Collection\AbstractCollection $collectionObject */
  124. $collectionObject = $this->collections[$key];
  125. return $collectionObject->addItem(isset($args[0]) ? $args[0] : null);
  126. }
  127. // Run add style method
  128. if (in_array($function, $addStyle)) {
  129. return forward_static_call_array(array('PhpOffice\\PhpWord\\Style', $function), $args);
  130. }
  131. // Exception
  132. throw new \BadMethodCallException("Method $function is not defined.");
  133. }
  134. /**
  135. * Get document properties object
  136. *
  137. * @return \PhpOffice\PhpWord\Metadata\DocInfo
  138. */
  139. public function getDocInfo()
  140. {
  141. return $this->metadata['DocInfo'];
  142. }
  143. /**
  144. * Get protection
  145. *
  146. * @return \PhpOffice\PhpWord\Metadata\Protection
  147. * @since 0.12.0
  148. */
  149. public function getProtection()
  150. {
  151. return $this->metadata['Protection'];
  152. }
  153. /**
  154. * Get compatibility
  155. *
  156. * @return \PhpOffice\PhpWord\Metadata\Compatibility
  157. * @since 0.12.0
  158. */
  159. public function getCompatibility()
  160. {
  161. return $this->metadata['Compatibility'];
  162. }
  163. /**
  164. * Get all sections
  165. *
  166. * @return \PhpOffice\PhpWord\Element\Section[]
  167. */
  168. public function getSections()
  169. {
  170. return $this->sections;
  171. }
  172. /**
  173. * Create new section
  174. *
  175. * @param array $style
  176. * @return \PhpOffice\PhpWord\Element\Section
  177. */
  178. public function addSection($style = null)
  179. {
  180. $section = new Section(count($this->sections) + 1, $style);
  181. $section->setPhpWord($this);
  182. $this->sections[] = $section;
  183. return $section;
  184. }
  185. /**
  186. * Get default font name
  187. *
  188. * @return string
  189. */
  190. public function getDefaultFontName()
  191. {
  192. return Settings::getDefaultFontName();
  193. }
  194. /**
  195. * Set default font name.
  196. *
  197. * @param string $fontName
  198. * @return void
  199. */
  200. public function setDefaultFontName($fontName)
  201. {
  202. Settings::setDefaultFontName($fontName);
  203. }
  204. /**
  205. * Get default font size
  206. *
  207. * @return integer
  208. */
  209. public function getDefaultFontSize()
  210. {
  211. return Settings::getDefaultFontSize();
  212. }
  213. /**
  214. * Set default font size.
  215. *
  216. * @param int $fontSize
  217. * @return void
  218. */
  219. public function setDefaultFontSize($fontSize)
  220. {
  221. Settings::setDefaultFontSize($fontSize);
  222. }
  223. /**
  224. * Set default paragraph style definition to styles.xml
  225. *
  226. * @param array $styles Paragraph style definition
  227. * @return \PhpOffice\PhpWord\Style\Paragraph
  228. */
  229. public function setDefaultParagraphStyle($styles)
  230. {
  231. return Style::setDefaultParagraphStyle($styles);
  232. }
  233. /**
  234. * Load template by filename
  235. *
  236. * @deprecated 0.12.0 Use `new TemplateProcessor($documentTemplate)` instead.
  237. *
  238. * @param string $filename Fully qualified filename.
  239. * @return TemplateProcessor
  240. * @throws \PhpOffice\PhpWord\Exception\Exception
  241. */
  242. public function loadTemplate($filename)
  243. {
  244. if (file_exists($filename)) {
  245. return new TemplateProcessor($filename);
  246. } else {
  247. throw new Exception("Template file {$filename} not found.");
  248. }
  249. }
  250. /**
  251. * Save to file or download
  252. *
  253. * All exceptions should already been handled by the writers
  254. *
  255. * @param string $filename
  256. * @param string $format
  257. * @param bool $download
  258. * @return bool
  259. */
  260. public function save($filename, $format = 'Word2007', $download = false)
  261. {
  262. $mime = array(
  263. 'Word2007' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  264. 'ODText' => 'application/vnd.oasis.opendocument.text',
  265. 'RTF' => 'application/rtf',
  266. 'HTML' => 'text/html',
  267. 'PDF' => 'application/pdf',
  268. );
  269. $writer = IOFactory::createWriter($this, $format);
  270. if ($download === true) {
  271. header("Content-Description: File Transfer");
  272. header('Content-Disposition: attachment; filename="' . $filename . '"');
  273. header('Content-Type: ' . $mime[$format]);
  274. header('Content-Transfer-Encoding: binary');
  275. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  276. header('Expires: 0');
  277. $filename = 'php://output'; // Change filename to force download
  278. }
  279. $writer->save($filename);
  280. return true;
  281. }
  282. /**
  283. * Create new section
  284. *
  285. * @param array $settings
  286. * @return \PhpOffice\PhpWord\Element\Section
  287. * @deprecated 0.10.0
  288. * @codeCoverageIgnore
  289. */
  290. public function createSection($settings = null)
  291. {
  292. return $this->addSection($settings);
  293. }
  294. /**
  295. * Get document properties object
  296. *
  297. * @return \PhpOffice\PhpWord\Metadata\DocInfo
  298. * @deprecated 0.12.0
  299. * @codeCoverageIgnore
  300. */
  301. public function getDocumentProperties()
  302. {
  303. return $this->getDocInfo();
  304. }
  305. /**
  306. * Set document properties object
  307. *
  308. * @param \PhpOffice\PhpWord\Metadata\DocInfo $documentProperties
  309. * @return self
  310. * @deprecated 0.12.0
  311. * @codeCoverageIgnore
  312. */
  313. public function setDocumentProperties($documentProperties)
  314. {
  315. $this->metadata['Document'] = $documentProperties;
  316. return $this;
  317. }
  318. }