PHPWord.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * PHPWord
  4. *
  5. * Copyright (c) 2011 PHPWord
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPWord
  22. * @package PHPWord
  23. * @copyright Copyright (c) 010 PHPWord
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version Beta 0.6.3, 08.07.2011
  26. */
  27. /** PHPWORD_BASE_PATH */
  28. if(!defined('PHPWORD_BASE_PATH')) {
  29. define('PHPWORD_BASE_PATH', dirname(__FILE__) . '/');
  30. require PHPWORD_BASE_PATH . 'PHPWord/Autoloader.php';
  31. PHPWord_Autoloader::Register();
  32. }
  33. /**
  34. * PHPWord
  35. *
  36. * @category PHPWord
  37. * @package PHPWord
  38. * @copyright Copyright (c) 2011 PHPWord
  39. */
  40. class PHPWord {
  41. /**
  42. * Document properties
  43. *
  44. * @var PHPWord_DocumentProperties
  45. */
  46. private $_properties;
  47. /**
  48. * Default Font Name
  49. *
  50. * @var string
  51. */
  52. private $_defaultFontName;
  53. /**
  54. * Default Font Size
  55. *
  56. * @var int
  57. */
  58. private $_defaultFontSize;
  59. /**
  60. * Collection of section elements
  61. *
  62. * @var array
  63. */
  64. private $_sectionCollection = array();
  65. /**
  66. * Create a new PHPWord Document
  67. */
  68. public function __construct() {
  69. $this->_properties = new PHPWord_DocumentProperties();
  70. $this->_defaultFontName = 'Arial';
  71. $this->_defaultFontSize = 20;
  72. }
  73. /**
  74. * Get properties
  75. * @return PHPWord_DocumentProperties
  76. */
  77. public function getProperties() {
  78. return $this->_properties;
  79. }
  80. /**
  81. * Set properties
  82. *
  83. * @param PHPWord_DocumentProperties $value
  84. * @return PHPWord
  85. */
  86. public function setProperties(PHPWord_DocumentProperties $value) {
  87. $this->_properties = $value;
  88. return $this;
  89. }
  90. /**
  91. * Create a new Section
  92. *
  93. * @param PHPWord_Section_Settings $settings
  94. * @return PHPWord_Section
  95. */
  96. public function createSection($settings = null) {
  97. $sectionCount = $this->_countSections() + 1;
  98. $section = new PHPWord_Section($sectionCount, $settings);
  99. $this->_sectionCollection[] = $section;
  100. return $section;
  101. }
  102. /**
  103. * Get default Font name
  104. * @return string
  105. */
  106. public function getDefaultFontName() {
  107. return $this->_defaultFontName;
  108. }
  109. /**
  110. * Set default Font name
  111. * @param string $pValue
  112. */
  113. public function setDefaultFontName($pValue) {
  114. $this->_defaultFontName = $pValue;
  115. }
  116. /**
  117. * Get default Font size
  118. * @return string
  119. */
  120. public function getDefaultFontSize() {
  121. return $this->_defaultFontSize;
  122. }
  123. /**
  124. * Set default Font size
  125. * @param int $pValue
  126. */
  127. public function setDefaultFontSize($pValue) {
  128. $pValue = $pValue * 2;
  129. $this->_defaultFontSize = $pValue;
  130. }
  131. /**
  132. * Adds a paragraph style definition to styles.xml
  133. *
  134. * @param $styleName string
  135. * @param $styles array
  136. */
  137. public function addParagraphStyle($styleName, $styles) {
  138. PHPWord_Style::addParagraphStyle($styleName, $styles);
  139. }
  140. /**
  141. * Adds a font style definition to styles.xml
  142. *
  143. * @param $styleName string
  144. * @param $styles array
  145. */
  146. public function addFontStyle($styleName, $styleFont, $styleParagraph = null) {
  147. PHPWord_Style::addFontStyle($styleName, $styleFont, $styleParagraph);
  148. }
  149. /**
  150. * Adds a table style definition to styles.xml
  151. *
  152. * @param $styleName string
  153. * @param $styles array
  154. */
  155. public function addTableStyle($styleName, $styleTable, $styleFirstRow = null) {
  156. PHPWord_Style::addTableStyle($styleName, $styleTable, $styleFirstRow);
  157. }
  158. /**
  159. * Adds a heading style definition to styles.xml
  160. *
  161. * @param $titleCount int
  162. * @param $styles array
  163. */
  164. public function addTitleStyle($titleCount, $styleFont, $styleParagraph = null) {
  165. PHPWord_Style::addTitleStyle($titleCount, $styleFont, $styleParagraph);
  166. }
  167. /**
  168. * Adds a hyperlink style to styles.xml
  169. *
  170. * @param $styleName string
  171. * @param $styles array
  172. */
  173. public function addLinkStyle($styleName, $styles) {
  174. PHPWord_Style::addLinkStyle($styleName, $styles);
  175. }
  176. /**
  177. * Get sections
  178. * @return PHPWord_Section[]
  179. */
  180. public function getSections() {
  181. return $this->_sectionCollection;
  182. }
  183. /**
  184. * Get section count
  185. * @return int
  186. */
  187. private function _countSections() {
  188. return count($this->_sectionCollection);
  189. }
  190. /**
  191. * Load a Template File
  192. *
  193. * @param string $strFilename
  194. * @return PHPWord_Template
  195. */
  196. public function loadTemplate($strFilename) {
  197. if(file_exists($strFilename)) {
  198. $template = new PHPWord_Template($strFilename);
  199. return $template;
  200. } else {
  201. trigger_error('Template file '.$strFilename.' not found.', E_ERROR);
  202. }
  203. }
  204. }
  205. ?>