PhpWord.php 12 KB

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