PHPWord.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. function utf8encode_dummy($data) {
  34. return $data;
  35. }
  36. /**
  37. * PHPWord
  38. *
  39. * @category PHPWord
  40. * @package PHPWord
  41. * @copyright Copyright (c) 2011 PHPWord
  42. */
  43. class PHPWord {
  44. /**
  45. * Document properties
  46. *
  47. * @var PHPWord_DocumentProperties
  48. */
  49. private $_properties;
  50. /**
  51. * Default Font Name
  52. *
  53. * @var string
  54. */
  55. private $_defaultFontName;
  56. /**
  57. * Default Font Size
  58. *
  59. * @var int
  60. */
  61. private $_defaultFontSize;
  62. /**
  63. * Collection of section elements
  64. *
  65. * @var array
  66. */
  67. private $_sectionCollection = array();
  68. /**
  69. * Create a new PHPWord Document
  70. */
  71. public function __construct() {
  72. $this->_properties = new PHPWord_DocumentProperties();
  73. $this->_defaultFontName = 'Arial';
  74. $this->_defaultFontSize = 20;
  75. }
  76. /**
  77. * Get properties
  78. * @return PHPWord_DocumentProperties
  79. */
  80. public function getProperties() {
  81. return $this->_properties;
  82. }
  83. /**
  84. * Set properties
  85. *
  86. * @param PHPWord_DocumentProperties $value
  87. * @return PHPWord
  88. */
  89. public function setProperties(PHPWord_DocumentProperties $value) {
  90. $this->_properties = $value;
  91. return $this;
  92. }
  93. /**
  94. * Create a new Section
  95. *
  96. * @param PHPWord_Section_Settings $settings
  97. * @return PHPWord_Section
  98. */
  99. public function createSection($settings = null) {
  100. $sectionCount = $this->_countSections() + 1;
  101. $section = new PHPWord_Section($sectionCount, $settings);
  102. $this->_sectionCollection[] = $section;
  103. return $section;
  104. }
  105. /**
  106. * Get default Font name
  107. * @return string
  108. */
  109. public function getDefaultFontName() {
  110. return $this->_defaultFontName;
  111. }
  112. /**
  113. * Set default Font name
  114. * @param string $pValue
  115. */
  116. public function setDefaultFontName($pValue) {
  117. $this->_defaultFontName = $pValue;
  118. }
  119. /**
  120. * Get default Font size
  121. * @return string
  122. */
  123. public function getDefaultFontSize() {
  124. return $this->_defaultFontSize;
  125. }
  126. /**
  127. * Set default Font size
  128. * @param int $pValue
  129. */
  130. public function setDefaultFontSize($pValue) {
  131. $pValue = $pValue * 2;
  132. $this->_defaultFontSize = $pValue;
  133. }
  134. /**
  135. * Adds a paragraph style definition to styles.xml
  136. *
  137. * @param $styleName string
  138. * @param $styles array
  139. */
  140. public function addParagraphStyle($styleName, $styles) {
  141. PHPWord_Style::addParagraphStyle($styleName, $styles);
  142. }
  143. /**
  144. * Adds a font style definition to styles.xml
  145. *
  146. * @param $styleName string
  147. * @param $styles array
  148. */
  149. public function addFontStyle($styleName, $styleFont, $styleParagraph = null) {
  150. PHPWord_Style::addFontStyle($styleName, $styleFont, $styleParagraph);
  151. }
  152. /**
  153. * Adds a table style definition to styles.xml
  154. *
  155. * @param $styleName string
  156. * @param $styles array
  157. */
  158. public function addTableStyle($styleName, $styleTable, $styleFirstRow = null) {
  159. PHPWord_Style::addTableStyle($styleName, $styleTable, $styleFirstRow);
  160. }
  161. /**
  162. * Adds a heading style definition to styles.xml
  163. *
  164. * @param $titleCount int
  165. * @param $styles array
  166. */
  167. public function addTitleStyle($titleCount, $styleFont, $styleParagraph = null) {
  168. PHPWord_Style::addTitleStyle($titleCount, $styleFont, $styleParagraph);
  169. }
  170. /**
  171. * Adds a hyperlink style to styles.xml
  172. *
  173. * @param $styleName string
  174. * @param $styles array
  175. */
  176. public function addLinkStyle($styleName, $styles) {
  177. PHPWord_Style::addLinkStyle($styleName, $styles);
  178. }
  179. /**
  180. * Get sections
  181. * @return PHPWord_Section[]
  182. */
  183. public function getSections() {
  184. return $this->_sectionCollection;
  185. }
  186. /**
  187. * Get section count
  188. * @return int
  189. */
  190. private function _countSections() {
  191. return count($this->_sectionCollection);
  192. }
  193. /**
  194. * Load a Template File
  195. *
  196. * @param string $strFilename
  197. * @return PHPWord_Template
  198. */
  199. public function loadTemplate($strFilename) {
  200. if(file_exists($strFilename)) {
  201. $template = new PHPWord_Template($strFilename);
  202. return $template;
  203. } else {
  204. trigger_error('Template file '.$strFilename.' not found.', E_ERROR);
  205. }
  206. }
  207. }
  208. ?>