├── PHPWord.php ├── PHPWord ├── Autoloader.php ├── DocumentProperties.php ├── IOFactory.php ├── Media.php ├── Section.php ├── Section │ ├── Footer.php │ ├── Footer │ │ └── PreserveText.php │ ├── Header.php │ ├── Image.php │ ├── Link.php │ ├── ListItem.php │ ├── MemoryImage.php │ ├── Object.php │ ├── PageBreak.php │ ├── Settings.php │ ├── Table.php │ ├── Table │ │ └── Cell.php │ ├── Text.php │ ├── TextBreak.php │ ├── TextRun.php │ └── Title.php ├── Shared │ ├── Drawing.php │ ├── File.php │ ├── Font.php │ ├── String.php │ ├── XMLWriter.php │ └── ZipStreamWrapper.php ├── Style.php ├── Style │ ├── Cell.php │ ├── Font.php │ ├── Image.php │ ├── ListItem.php │ ├── Paragraph.php │ ├── TOC.php │ ├── Table.php │ └── TableFull.php ├── TOC.php ├── Template.php ├── Writer │ ├── IWriter.php │ ├── Word2007.php │ └── Word2007 │ │ ├── Base.php │ │ ├── ContentTypes.php │ │ ├── DocProps.php │ │ ├── Document.php │ │ ├── DocumentRels.php │ │ ├── Footer.php │ │ ├── Header.php │ │ ├── Rels.php │ │ ├── Styles.php │ │ └── WriterPart.php └── _staticDocParts │ ├── _doc.png │ ├── _ppt.png │ ├── _xls.png │ ├── fontTable.xml │ ├── numbering.xml │ ├── settings.xml │ ├── theme1.xml │ └── webSettings.xml ├── README.md └── start.php /PHPWord.php: -------------------------------------------------------------------------------- 1 | _properties = new PHPWord_DocumentProperties(); 79 | $this->_defaultFontName = 'Arial'; 80 | $this->_defaultFontSize = 20; 81 | } 82 | 83 | /** 84 | * Get properties 85 | * @return PHPWord_DocumentProperties 86 | */ 87 | public function getProperties() { 88 | return $this->_properties; 89 | } 90 | 91 | /** 92 | * Set properties 93 | * 94 | * @param PHPWord_DocumentProperties $value 95 | * @return PHPWord 96 | */ 97 | public function setProperties(PHPWord_DocumentProperties $value) { 98 | $this->_properties = $value; 99 | return $this; 100 | } 101 | 102 | /** 103 | * Create a new Section 104 | * 105 | * @param PHPWord_Section_Settings $settings 106 | * @return PHPWord_Section 107 | */ 108 | public function createSection($settings = null) { 109 | $sectionCount = $this->_countSections() + 1; 110 | 111 | $section = new PHPWord_Section($sectionCount, $settings); 112 | $this->_sectionCollection[] = $section; 113 | return $section; 114 | } 115 | 116 | /** 117 | * Get default Font name 118 | * @return string 119 | */ 120 | public function getDefaultFontName() { 121 | return $this->_defaultFontName; 122 | } 123 | 124 | /** 125 | * Set default Font name 126 | * @param string $pValue 127 | */ 128 | public function setDefaultFontName($pValue) { 129 | $this->_defaultFontName = $pValue; 130 | } 131 | 132 | /** 133 | * Get default Font size 134 | * @return string 135 | */ 136 | public function getDefaultFontSize() { 137 | return $this->_defaultFontSize; 138 | } 139 | 140 | /** 141 | * Set default Font size 142 | * @param int $pValue 143 | */ 144 | public function setDefaultFontSize($pValue) { 145 | $pValue = $pValue * 2; 146 | $this->_defaultFontSize = $pValue; 147 | } 148 | 149 | /** 150 | * Adds a paragraph style definition to styles.xml 151 | * 152 | * @param $styleName string 153 | * @param $styles array 154 | */ 155 | public function addParagraphStyle($styleName, $styles) { 156 | PHPWord_Style::addParagraphStyle($styleName, $styles); 157 | } 158 | 159 | /** 160 | * Adds a font style definition to styles.xml 161 | * 162 | * @param $styleName string 163 | * @param $styles array 164 | */ 165 | public function addFontStyle($styleName, $styleFont, $styleParagraph = null) { 166 | PHPWord_Style::addFontStyle($styleName, $styleFont, $styleParagraph); 167 | } 168 | 169 | /** 170 | * Adds a table style definition to styles.xml 171 | * 172 | * @param $styleName string 173 | * @param $styles array 174 | */ 175 | public function addTableStyle($styleName, $styleTable, $styleFirstRow = null) { 176 | PHPWord_Style::addTableStyle($styleName, $styleTable, $styleFirstRow); 177 | } 178 | 179 | /** 180 | * Adds a heading style definition to styles.xml 181 | * 182 | * @param $titleCount int 183 | * @param $styles array 184 | */ 185 | public function addTitleStyle($titleCount, $styleFont, $styleParagraph = null) { 186 | PHPWord_Style::addTitleStyle($titleCount, $styleFont, $styleParagraph); 187 | } 188 | 189 | /** 190 | * Adds a hyperlink style to styles.xml 191 | * 192 | * @param $styleName string 193 | * @param $styles array 194 | */ 195 | public function addLinkStyle($styleName, $styles) { 196 | PHPWord_Style::addLinkStyle($styleName, $styles); 197 | } 198 | 199 | /** 200 | * Get sections 201 | * @return PHPWord_Section[] 202 | */ 203 | public function getSections() { 204 | return $this->_sectionCollection; 205 | } 206 | 207 | /** 208 | * Get section count 209 | * @return int 210 | */ 211 | private function _countSections() { 212 | return count($this->_sectionCollection); 213 | } 214 | 215 | /** 216 | * Load a Template File 217 | * 218 | * @param string $strFilename 219 | * @return PHPWord_Template 220 | */ 221 | public function loadTemplate($strFilename) { 222 | if(file_exists($strFilename)) { 223 | $template = new PHPWord_Template($strFilename); 224 | return $template; 225 | } else { 226 | trigger_error('Template file '.$strFilename.' not found.', E_ERROR); 227 | } 228 | } 229 | } 230 | ?> -------------------------------------------------------------------------------- /PHPWord/Autoloader.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /PHPWord/DocumentProperties.php: -------------------------------------------------------------------------------- 1 | _creator = ''; 113 | $this->_lastModifiedBy = $this->_creator; 114 | $this->_created = time(); 115 | $this->_modified = time(); 116 | $this->_title = ''; 117 | $this->_subject = ''; 118 | $this->_description = ''; 119 | $this->_keywords = ''; 120 | $this->_category = ''; 121 | $this->_company = ''; 122 | } 123 | 124 | /** 125 | * Get Creator 126 | * 127 | * @return string 128 | */ 129 | public function getCreator() { 130 | return $this->_creator; 131 | } 132 | 133 | /** 134 | * Set Creator 135 | * 136 | * @param string $pValue 137 | * @return PHPWord_DocumentProperties 138 | */ 139 | public function setCreator($pValue = '') { 140 | $this->_creator = $pValue; 141 | return $this; 142 | } 143 | 144 | /** 145 | * Get Last Modified By 146 | * 147 | * @return string 148 | */ 149 | public function getLastModifiedBy() { 150 | return $this->_lastModifiedBy; 151 | } 152 | 153 | /** 154 | * Set Last Modified By 155 | * 156 | * @param string $pValue 157 | * @return PHPWord_DocumentProperties 158 | */ 159 | public function setLastModifiedBy($pValue = '') { 160 | $this->_lastModifiedBy = $pValue; 161 | return $this; 162 | } 163 | 164 | /** 165 | * Get Created 166 | * 167 | * @return datetime 168 | */ 169 | public function getCreated() { 170 | return $this->_created; 171 | } 172 | 173 | /** 174 | * Set Created 175 | * 176 | * @param datetime $pValue 177 | * @return PHPWord_DocumentProperties 178 | */ 179 | public function setCreated($pValue = null) { 180 | if (is_null($pValue)) { 181 | $pValue = time(); 182 | } 183 | $this->_created = $pValue; 184 | return $this; 185 | } 186 | 187 | /** 188 | * Get Modified 189 | * 190 | * @return datetime 191 | */ 192 | public function getModified() { 193 | return $this->_modified; 194 | } 195 | 196 | /** 197 | * Set Modified 198 | * 199 | * @param datetime $pValue 200 | * @return PHPWord_DocumentProperties 201 | */ 202 | public function setModified($pValue = null) { 203 | if (is_null($pValue)) { 204 | $pValue = time(); 205 | } 206 | $this->_modified = $pValue; 207 | return $this; 208 | } 209 | 210 | /** 211 | * Get Title 212 | * 213 | * @return string 214 | */ 215 | public function getTitle() { 216 | return $this->_title; 217 | } 218 | 219 | /** 220 | * Set Title 221 | * 222 | * @param string $pValue 223 | * @return PHPWord_DocumentProperties 224 | */ 225 | public function setTitle($pValue = '') { 226 | $this->_title = $pValue; 227 | return $this; 228 | } 229 | 230 | /** 231 | * Get Description 232 | * 233 | * @return string 234 | */ 235 | public function getDescription() { 236 | return $this->_description; 237 | } 238 | 239 | /** 240 | * Set Description 241 | * 242 | * @param string $pValue 243 | * @return PHPWord_DocumentProperties 244 | */ 245 | public function setDescription($pValue = '') { 246 | $this->_description = $pValue; 247 | return $this; 248 | } 249 | 250 | /** 251 | * Get Subject 252 | * 253 | * @return string 254 | */ 255 | public function getSubject() { 256 | return $this->_subject; 257 | } 258 | 259 | /** 260 | * Set Subject 261 | * 262 | * @param string $pValue 263 | * @return PHPWord_DocumentProperties 264 | */ 265 | public function setSubject($pValue = '') { 266 | $this->_subject = $pValue; 267 | return $this; 268 | } 269 | 270 | /** 271 | * Get Keywords 272 | * 273 | * @return string 274 | */ 275 | public function getKeywords() { 276 | return $this->_keywords; 277 | } 278 | 279 | /** 280 | * Set Keywords 281 | * 282 | * @param string $pValue 283 | * @return PHPWord_DocumentProperties 284 | */ 285 | public function setKeywords($pValue = '') { 286 | $this->_keywords = $pValue; 287 | return $this; 288 | } 289 | 290 | /** 291 | * Get Category 292 | * 293 | * @return string 294 | */ 295 | public function getCategory() { 296 | return $this->_category; 297 | } 298 | 299 | /** 300 | * Set Category 301 | * 302 | * @param string $pValue 303 | * @return PHPWord_DocumentProperties 304 | */ 305 | public function setCategory($pValue = '') { 306 | $this->_category = $pValue; 307 | return $this; 308 | } 309 | 310 | /** 311 | * Get Company 312 | * 313 | * @return string 314 | */ 315 | public function getCompany() { 316 | return $this->_company; 317 | } 318 | 319 | /** 320 | * Set Company 321 | * 322 | * @param string $pValue 323 | * @return PHPWord_DocumentProperties 324 | */ 325 | public function setCompany($pValue = '') { 326 | $this->_company = $pValue; 327 | return $this; 328 | } 329 | } 330 | ?> 331 | -------------------------------------------------------------------------------- /PHPWord/IOFactory.php: -------------------------------------------------------------------------------- 1 | 'IWriter', 'path' => 'PHPWord/Writer/{0}.php', 'class' => 'PHPWord_Writer_{0}') 45 | ); 46 | 47 | /** 48 | * Autoresolve classes 49 | * 50 | * @var array 51 | */ 52 | private static $_autoResolveClasses = array( 53 | 'Serialized' 54 | ); 55 | 56 | /** 57 | * Private constructor for PHPWord_IOFactory 58 | */ 59 | private function __construct() { } 60 | 61 | /** 62 | * Get search locations 63 | * 64 | * @return array 65 | */ 66 | public static function getSearchLocations() { 67 | return self::$_searchLocations; 68 | } 69 | 70 | /** 71 | * Set search locations 72 | * 73 | * @param array $value 74 | * @throws Exception 75 | */ 76 | public static function setSearchLocations($value) { 77 | if (is_array($value)) { 78 | self::$_searchLocations = $value; 79 | } else { 80 | throw new Exception('Invalid parameter passed.'); 81 | } 82 | } 83 | 84 | /** 85 | * Add search location 86 | * 87 | * @param string $type Example: IWriter 88 | * @param string $location Example: PHPWord/Writer/{0}.php 89 | * @param string $classname Example: PHPWord_Writer_{0} 90 | */ 91 | public static function addSearchLocation($type = '', $location = '', $classname = '') { 92 | self::$_searchLocations[] = array( 'type' => $type, 'path' => $location, 'class' => $classname ); 93 | } 94 | 95 | /** 96 | * Create PHPWord_Writer_IWriter 97 | * 98 | * @param PHPWord $PHPWord 99 | * @param string $writerType Example: Word2007 100 | * @return PHPWord_Writer_IWriter 101 | */ 102 | public static function createWriter(PHPWord $PHPWord, $writerType = '') { 103 | $searchType = 'IWriter'; 104 | 105 | foreach (self::$_searchLocations as $searchLocation) { 106 | if ($searchLocation['type'] == $searchType) { 107 | $className = str_replace('{0}', $writerType, $searchLocation['class']); 108 | $classFile = str_replace('{0}', $writerType, $searchLocation['path']); 109 | 110 | $instance = new $className($PHPWord); 111 | if(!is_null($instance)) { 112 | return $instance; 113 | } 114 | } 115 | } 116 | 117 | throw new Exception("No $searchType found for type $writerType"); 118 | } 119 | } 120 | ?> -------------------------------------------------------------------------------- /PHPWord/Media.php: -------------------------------------------------------------------------------- 1 | array(), 44 | 'embeddings'=>array(), 45 | 'links'=>array()); 46 | 47 | /** 48 | * Header Media Elements 49 | * 50 | * @var array 51 | */ 52 | private static $_headerMedia = array(); 53 | 54 | /** 55 | * Footer Media Elements 56 | * 57 | * @var array 58 | */ 59 | private static $_footerMedia = array(); 60 | 61 | /** 62 | * ObjectID Counter 63 | * 64 | * @var int 65 | */ 66 | private static $_objectId = 1325353440; 67 | 68 | 69 | /** 70 | * Add new Section Media Element 71 | * 72 | * @param string $src 73 | * @param string $type 74 | * 75 | * @return mixed 76 | */ 77 | public static function addSectionMediaElement($src, $type, PHPWord_Section_MemoryImage $memoryImage = null) { 78 | $mediaId = md5($src); 79 | $key = ($type == 'image') ? 'images' : 'embeddings'; 80 | 81 | if(!array_key_exists($mediaId, self::$_sectionMedia[$key])) { 82 | $cImg = self::countSectionMediaElements('images'); 83 | $cObj = self::countSectionMediaElements('embeddings'); 84 | $rID = self::countSectionMediaElements() + 7; 85 | 86 | $media = array(); 87 | 88 | if($type == 'image') { 89 | $cImg++; 90 | $inf = pathinfo($src); 91 | $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php' && $type == 'image') ? true : false; 92 | 93 | if($isMemImage) { 94 | $ext = $memoryImage->getImageExtension(); 95 | $media['isMemImage'] = true; 96 | $media['createfunction'] = $memoryImage->getImageCreateFunction(); 97 | $media['imagefunction'] = $memoryImage->getImageFunction(); 98 | } else { 99 | $ext = $inf['extension']; 100 | if($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg 101 | $ext = 'jpg'; 102 | } 103 | } 104 | 105 | $folder = 'media'; 106 | $file = $type.$cImg.'.'.strtolower($ext); 107 | } elseif($type == 'oleObject') { 108 | $cObj++; 109 | $folder = 'embedding'; 110 | $file = $type.$cObj.'.bin'; 111 | } 112 | 113 | $media['source'] = $src; 114 | $media['target'] = "$folder/section_$file"; 115 | $media['type'] = $type; 116 | $media['rID'] = $rID; 117 | 118 | self::$_sectionMedia[$key][$mediaId] = $media; 119 | 120 | if($type == 'oleObject') { 121 | return array($rID, ++self::$_objectId); 122 | } else { 123 | return $rID; 124 | } 125 | } else { 126 | if($type == 'oleObject') { 127 | $rID = self::$_sectionMedia[$key][$mediaId]['rID']; 128 | return array($rID, ++self::$_objectId); 129 | } else { 130 | return self::$_sectionMedia[$key][$mediaId]['rID']; 131 | } 132 | } 133 | } 134 | 135 | /** 136 | * Add new Section Link Element 137 | * 138 | * @param string $linkSrc 139 | * @param string $linkName 140 | * 141 | * @return mixed 142 | */ 143 | public static function addSectionLinkElement($linkSrc) { 144 | $rID = self::countSectionMediaElements() + 7; 145 | 146 | $link = array(); 147 | $link['target'] = $linkSrc; 148 | $link['rID'] = $rID; 149 | $link['type'] = 'hyperlink'; 150 | 151 | self::$_sectionMedia['links'][] = $link; 152 | 153 | return $rID; 154 | } 155 | 156 | /** 157 | * Get Section Media Elements 158 | * 159 | * @param string $key 160 | * @return array 161 | */ 162 | public static function getSectionMediaElements($key = null) { 163 | if(!is_null($key)) { 164 | return self::$_sectionMedia[$key]; 165 | } else { 166 | $arrImages = self::$_sectionMedia['images']; 167 | $arrObjects = self::$_sectionMedia['embeddings']; 168 | $arrLinks = self::$_sectionMedia['links']; 169 | return array_merge($arrImages, $arrObjects, $arrLinks); 170 | } 171 | } 172 | 173 | /** 174 | * Get Section Media Elements Count 175 | * 176 | * @param string $key 177 | * @return int 178 | */ 179 | public static function countSectionMediaElements($key = null) { 180 | if(!is_null($key)) { 181 | return count(self::$_sectionMedia[$key]); 182 | } else { 183 | $cImages = count(self::$_sectionMedia['images']); 184 | $cObjects = count(self::$_sectionMedia['embeddings']); 185 | $cLinks = count(self::$_sectionMedia['links']); 186 | return ($cImages + $cObjects + $cLinks); 187 | } 188 | } 189 | 190 | /** 191 | * Add new Header Media Element 192 | * 193 | * @param int $headerCount 194 | * @param string $src 195 | * @return int 196 | */ 197 | public static function addHeaderMediaElement($headerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null) { 198 | $mediaId = md5($src); 199 | $key = 'header'.$headerCount; 200 | 201 | if(!array_key_exists($key, self::$_headerMedia)) { 202 | self::$_headerMedia[$key] = array(); 203 | } 204 | 205 | if(!array_key_exists($mediaId, self::$_headerMedia[$key])) { 206 | $cImg = self::countHeaderMediaElements($key); 207 | $rID = $cImg + 1; 208 | 209 | $cImg++; 210 | $inf = pathinfo($src); 211 | $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false; 212 | 213 | $media = array(); 214 | if($isMemImage) { 215 | $ext = $memoryImage->getImageExtension(); 216 | $media['isMemImage'] = true; 217 | $media['createfunction'] = $memoryImage->getImageCreateFunction(); 218 | $media['imagefunction'] = $memoryImage->getImageFunction(); 219 | } else { 220 | $ext = $inf['extension']; 221 | if($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg 222 | $ext = 'jpg'; 223 | } 224 | } 225 | $file = 'image'.$cImg.'.'.strtolower($ext); 226 | 227 | $media['source'] = $src; 228 | $media['target'] = 'media/'.$key.'_'.$file; 229 | $media['type'] = 'image'; 230 | $media['rID'] = $rID; 231 | 232 | self::$_headerMedia[$key][$mediaId] = $media; 233 | 234 | return $rID; 235 | } else { 236 | return self::$_headerMedia[$key][$mediaId]['rID']; 237 | } 238 | } 239 | 240 | /** 241 | * Get Header Media Elements Count 242 | * 243 | * @param string $key 244 | * @return int 245 | */ 246 | public static function countHeaderMediaElements($key) { 247 | return count(self::$_headerMedia[$key]); 248 | } 249 | 250 | /** 251 | * Get Header Media Elements 252 | * 253 | * @return int 254 | */ 255 | public static function getHeaderMediaElements() { 256 | return self::$_headerMedia; 257 | } 258 | 259 | /** 260 | * Add new Footer Media Element 261 | * 262 | * @param int $footerCount 263 | * @param string $src 264 | * @return int 265 | */ 266 | public static function addFooterMediaElement($footerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null) { 267 | $mediaId = md5($src); 268 | $key = 'footer'.$footerCount; 269 | 270 | if(!array_key_exists($key, self::$_footerMedia)) { 271 | self::$_footerMedia[$key] = array(); 272 | } 273 | 274 | if(!array_key_exists($mediaId, self::$_footerMedia[$key])) { 275 | $cImg = self::countFooterMediaElements($key); 276 | $rID = $cImg + 1; 277 | 278 | $cImg++; 279 | $inf = pathinfo($src); 280 | $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false; 281 | 282 | $media = array(); 283 | if($isMemImage) { 284 | $ext = $memoryImage->getImageExtension(); 285 | $media['isMemImage'] = true; 286 | $media['createfunction'] = $memoryImage->getImageCreateFunction(); 287 | $media['imagefunction'] = $memoryImage->getImageFunction(); 288 | } else { 289 | $ext = $inf['extension']; 290 | if($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg 291 | $ext = 'jpg'; 292 | } 293 | } 294 | $file = 'image'.$cImg.'.'.strtolower($ext); 295 | 296 | $media['source'] = $src; 297 | $media['target'] = 'media/'.$key.'_'.$file; 298 | $media['type'] = 'image'; 299 | $media['rID'] = $rID; 300 | 301 | self::$_footerMedia[$key][$mediaId] = $media; 302 | 303 | return $rID; 304 | } else { 305 | return self::$_footerMedia[$key][$mediaId]['rID']; 306 | } 307 | } 308 | 309 | /** 310 | * Get Footer Media Elements Count 311 | * 312 | * @param string $key 313 | * @return int 314 | */ 315 | public static function countFooterMediaElements($key) { 316 | return count(self::$_footerMedia[$key]); 317 | } 318 | 319 | /** 320 | * Get Footer Media Elements 321 | * 322 | * @return int 323 | */ 324 | public static function getFooterMediaElements() { 325 | return self::$_footerMedia; 326 | } 327 | } 328 | ?> 329 | -------------------------------------------------------------------------------- /PHPWord/Section.php: -------------------------------------------------------------------------------- 1 | _sectionCount = $sectionCount; 82 | $this->_settings = new PHPWord_Section_Settings(); 83 | 84 | if(!is_null($settings) && is_array($settings)) { 85 | foreach($settings as $key => $value) { 86 | if(substr($key, 0, 1) != '_') { 87 | $key = '_'.$key; 88 | } 89 | $this->_settings->setSettingValue($key, $value); 90 | } 91 | } 92 | } 93 | 94 | /** 95 | * Get Section Settings 96 | * 97 | * @return PHPWord_Section_Settings 98 | */ 99 | public function getSettings() { 100 | return $this->_settings; 101 | } 102 | 103 | /** 104 | * Add a Text Element 105 | * 106 | * @param string $text 107 | * @param mixed $styleFont 108 | * @param mixed $styleParagraph 109 | * @return PHPWord_Section_Text 110 | */ 111 | public function addText($text, $styleFont = null, $styleParagraph = null) { 112 | $givenText = utf8_encode($text); 113 | $text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph); 114 | $this->_elementCollection[] = $text; 115 | return $text; 116 | } 117 | 118 | /** 119 | * Add a Link Element 120 | * 121 | * @param string $linkSrc 122 | * @param string $linkName 123 | * @param mixed $styleFont 124 | * @param mixed $styleParagraph 125 | * @return PHPWord_Section_Link 126 | */ 127 | public function addLink($linkSrc, $linkName = null, $styleFont = null, $styleParagraph = null) { 128 | $linkSrc = utf8_encode($linkSrc); 129 | if(!is_null($linkName)) { 130 | $linkName = utf8_encode($linkName); 131 | } 132 | 133 | $link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont, $styleParagraph); 134 | $rID = PHPWord_Media::addSectionLinkElement($linkSrc); 135 | $link->setRelationId($rID); 136 | 137 | $this->_elementCollection[] = $link; 138 | return $link; 139 | } 140 | 141 | /** 142 | * Add a TextBreak Element 143 | * 144 | * @param int $count 145 | */ 146 | public function addTextBreak($count = 1) { 147 | for($i=1; $i<=$count; $i++) { 148 | $this->_elementCollection[] = new PHPWord_Section_TextBreak(); 149 | } 150 | } 151 | 152 | /** 153 | * Add a PageBreak Element 154 | */ 155 | public function addPageBreak() { 156 | $this->_elementCollection[] = new PHPWord_Section_PageBreak(); 157 | } 158 | 159 | /** 160 | * Add a Table Element 161 | * 162 | * @param mixed $style 163 | * @return PHPWord_Section_Table 164 | */ 165 | public function addTable($style = null) { 166 | $table = new PHPWord_Section_Table('section', $this->_sectionCount, $style); 167 | $this->_elementCollection[] = $table; 168 | return $table; 169 | } 170 | 171 | /** 172 | * Add a ListItem Element 173 | * 174 | * @param string $text 175 | * @param int $depth 176 | * @param mixed $styleFont 177 | * @param mixed $styleList 178 | * @param mixed $styleParagraph 179 | * @return PHPWord_Section_ListItem 180 | */ 181 | public function addListItem($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null) { 182 | $text = utf8_encode($text); 183 | $listItem = new PHPWord_Section_ListItem($text, $depth, $styleFont, $styleList, $styleParagraph); 184 | $this->_elementCollection[] = $listItem; 185 | return $listItem; 186 | } 187 | 188 | /** 189 | * Add a OLE-Object Element 190 | * 191 | * @param string $src 192 | * @param mixed $style 193 | * @return PHPWord_Section_Object 194 | */ 195 | public function addObject($src, $style = null) { 196 | $object = new PHPWord_Section_Object($src, $style); 197 | 198 | if(!is_null($object->getSource())) { 199 | $inf = pathinfo($src); 200 | $ext = $inf['extension']; 201 | if(strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') { 202 | $ext = substr($ext, 0, -1); 203 | } 204 | 205 | $iconSrc = PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/'; 206 | if(!file_exists($iconSrc.'_'.$ext.'.png')) { 207 | $iconSrc = $iconSrc.'_default.png'; 208 | } else { 209 | $iconSrc .= '_'.$ext.'.png'; 210 | } 211 | 212 | $rIDimg = PHPWord_Media::addSectionMediaElement($iconSrc, 'image'); 213 | $data = PHPWord_Media::addSectionMediaElement($src, 'oleObject'); 214 | $rID = $data[0]; 215 | $objectId = $data[1]; 216 | 217 | $object->setRelationId($rID); 218 | $object->setObjectId($objectId); 219 | $object->setImageRelationId($rIDimg); 220 | 221 | $this->_elementCollection[] = $object; 222 | return $object; 223 | } else { 224 | trigger_error('Source does not exist or unsupported object type.'); 225 | } 226 | } 227 | 228 | /** 229 | * Add a Image Element 230 | * 231 | * @param string $src 232 | * @param mixed $style 233 | * @return PHPWord_Section_Image 234 | */ 235 | public function addImage($src, $style = null) { 236 | $image = new PHPWord_Section_Image($src, $style); 237 | 238 | if(!is_null($image->getSource())) { 239 | $rID = PHPWord_Media::addSectionMediaElement($src, 'image'); 240 | $image->setRelationId($rID); 241 | 242 | $this->_elementCollection[] = $image; 243 | return $image; 244 | } else { 245 | trigger_error('Source does not exist or unsupported image type.'); 246 | } 247 | } 248 | 249 | /** 250 | * Add a by PHP created Image Element 251 | * 252 | * @param string $link 253 | * @param mixed $style 254 | * @return PHPWord_Section_MemoryImage 255 | */ 256 | public function addMemoryImage($link, $style = null) { 257 | $memoryImage = new PHPWord_Section_MemoryImage($link, $style); 258 | if(!is_null($memoryImage->getSource())) { 259 | $rID = PHPWord_Media::addSectionMediaElement($link, 'image', $memoryImage); 260 | $memoryImage->setRelationId($rID); 261 | 262 | $this->_elementCollection[] = $memoryImage; 263 | return $memoryImage; 264 | } else { 265 | trigger_error('Unsupported image type.'); 266 | } 267 | } 268 | 269 | /** 270 | * Add a Table-of-Contents Element 271 | * 272 | * @param mixed $styleFont 273 | * @param mixed $styleTOC 274 | * @return PHPWord_TOC 275 | */ 276 | public function addTOC($styleFont = null, $styleTOC = null) { 277 | $toc = new PHPWord_TOC($styleFont, $styleTOC); 278 | $this->_elementCollection[] = $toc; 279 | return $toc; 280 | } 281 | 282 | /** 283 | * Add a Title Element 284 | * 285 | * @param string $text 286 | * @param int $depth 287 | * @return PHPWord_Section_Title 288 | */ 289 | public function addTitle($text, $depth = 1) { 290 | $text = utf8_encode($text); 291 | $styles = PHPWord_Style::getStyles(); 292 | if(array_key_exists('Heading_'.$depth, $styles)) { 293 | $style = 'Heading'.$depth; 294 | } else { 295 | $style = null; 296 | } 297 | 298 | $title = new PHPWord_Section_Title($text, $depth, $style); 299 | 300 | $data = PHPWord_TOC::addTitle($text, $depth); 301 | $anchor = $data[0]; 302 | $bookmarkId = $data[1]; 303 | 304 | $title->setAnchor($anchor); 305 | $title->setBookmarkId($bookmarkId); 306 | 307 | $this->_elementCollection[] = $title; 308 | return $title; 309 | } 310 | 311 | /** 312 | * Create a new TextRun 313 | * 314 | * @return PHPWord_Section_TextRun 315 | */ 316 | public function createTextRun($styleParagraph = null) { 317 | $textRun = new PHPWord_Section_TextRun($styleParagraph); 318 | $this->_elementCollection[] = $textRun; 319 | return $textRun; 320 | } 321 | 322 | /** 323 | * Get all Elements 324 | * 325 | * @return array 326 | */ 327 | public function getElements() { 328 | return $this->_elementCollection; 329 | } 330 | 331 | /** 332 | * Create a new Header 333 | * 334 | * @return PHPWord_Section_Header 335 | */ 336 | public function createHeader() { 337 | $header = new PHPWord_Section_Header($this->_sectionCount); 338 | $this->_header = $header; 339 | return $header; 340 | } 341 | 342 | /** 343 | * Get Header 344 | * 345 | * @return PHPWord_Section_Header 346 | */ 347 | public function getHeader() { 348 | return $this->_header; 349 | } 350 | 351 | /** 352 | * Create a new Footer 353 | * 354 | * @return PHPWord_Section_Footer 355 | */ 356 | public function createFooter() { 357 | $footer = new PHPWord_Section_Footer($this->_sectionCount); 358 | $this->_footer = $footer; 359 | return $footer; 360 | } 361 | 362 | /** 363 | * Get Footer 364 | * 365 | * @return PHPWord_Section_Footer 366 | */ 367 | public function getFooter() { 368 | return $this->_footer; 369 | } 370 | } 371 | ?> -------------------------------------------------------------------------------- /PHPWord/Section/Footer.php: -------------------------------------------------------------------------------- 1 | _footerCount = $sectionCount; 64 | } 65 | 66 | /** 67 | * Add a Text Element 68 | * 69 | * @param string $text 70 | * @param mixed $styleFont 71 | * @param mixed $styleParagraph 72 | * @return PHPWord_Section_Text 73 | */ 74 | public function addText($text, $styleFont = null, $styleParagraph = null) { 75 | $givenText = utf8_encode($text); 76 | $text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph); 77 | $this->_elementCollection[] = $text; 78 | return $text; 79 | } 80 | 81 | /** 82 | * Add a TextBreak Element 83 | * 84 | * @param int $count 85 | */ 86 | public function addTextBreak($count = 1) { 87 | for($i=1; $i<=$count; $i++) { 88 | $this->_elementCollection[] = new PHPWord_Section_TextBreak(); 89 | } 90 | } 91 | 92 | /** 93 | * Create a new TextRun 94 | * 95 | * @return PHPWord_Section_TextRun 96 | */ 97 | public function createTextRun($styleParagraph = null) { 98 | $textRun = new PHPWord_Section_TextRun($styleParagraph); 99 | $this->_elementCollection[] = $textRun; 100 | return $textRun; 101 | } 102 | 103 | /** 104 | * Add a Table Element 105 | * 106 | * @param mixed $style 107 | * @return PHPWord_Section_Table 108 | */ 109 | public function addTable($style = null) { 110 | $table = new PHPWord_Section_Table('footer', $this->_footerCount, $style); 111 | $this->_elementCollection[] = $table; 112 | return $table; 113 | } 114 | 115 | /** 116 | * Add a Image Element 117 | * 118 | * @param string $src 119 | * @param mixed $style 120 | * @return PHPWord_Section_Image 121 | */ 122 | public function addImage($src, $style = null) { 123 | $image = new PHPWord_Section_Image($src, $style); 124 | 125 | if(!is_null($image->getSource())) { 126 | $rID = PHPWord_Media::addFooterMediaElement($this->_footerCount, $src); 127 | $image->setRelationId($rID); 128 | 129 | $this->_elementCollection[] = $image; 130 | return $image; 131 | } else { 132 | trigger_error('Src does not exist or invalid image type.', E_ERROR); 133 | } 134 | } 135 | 136 | /** 137 | * Add a by PHP created Image Element 138 | * 139 | * @param string $link 140 | * @param mixed $style 141 | * @return PHPWord_Section_MemoryImage 142 | */ 143 | public function addMemoryImage($link, $style = null) { 144 | $memoryImage = new PHPWord_Section_MemoryImage($link, $style); 145 | if(!is_null($memoryImage->getSource())) { 146 | $rID = PHPWord_Media::addFooterMediaElement($this->_footerCount, $link, $memoryImage); 147 | $memoryImage->setRelationId($rID); 148 | 149 | $this->_elementCollection[] = $memoryImage; 150 | return $memoryImage; 151 | } else { 152 | trigger_error('Unsupported image type.'); 153 | } 154 | } 155 | 156 | /** 157 | * Add a PreserveText Element 158 | * 159 | * @param string $text 160 | * @param mixed $styleFont 161 | * @param mixed $styleParagraph 162 | * @return PHPWord_Section_Footer_PreserveText 163 | */ 164 | public function addPreserveText($text, $styleFont = null, $styleParagraph = null) { 165 | $text = utf8_encode($text); 166 | $ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph); 167 | $this->_elementCollection[] = $ptext; 168 | return $ptext; 169 | } 170 | 171 | /** 172 | * Get Footer Relation ID 173 | */ 174 | public function getRelationId() { 175 | return $this->_rId; 176 | } 177 | 178 | /** 179 | * Set Footer Relation ID 180 | * 181 | * @param int $rId 182 | */ 183 | public function setRelationId($rId) { 184 | $this->_rId = $rId; 185 | } 186 | 187 | /** 188 | * Get all Footer Elements 189 | */ 190 | public function getElements() { 191 | return $this->_elementCollection; 192 | } 193 | 194 | /** 195 | * Get Footer Count 196 | */ 197 | public function getFooterCount() { 198 | return $this->_footerCount; 199 | } 200 | } 201 | ?> -------------------------------------------------------------------------------- /PHPWord/Section/Footer/PreserveText.php: -------------------------------------------------------------------------------- 1 | _styleFont = new PHPWord_Style_Font('text'); 70 | 71 | foreach($styleFont as $key => $value) { 72 | if(substr($key, 0, 1) != '_') { 73 | $key = '_'.$key; 74 | } 75 | $this->_styleFont->setStyleValue($key, $value); 76 | } 77 | } else { 78 | $this->_styleFont = $styleFont; 79 | } 80 | 81 | // Set paragraph style 82 | if(is_array($styleParagraph)) { 83 | $this->_styleParagraph = new PHPWord_Style_Paragraph(); 84 | 85 | foreach($styleParagraph as $key => $value) { 86 | if(substr($key, 0, 1) != '_') { 87 | $key = '_'.$key; 88 | } 89 | $this->_styleParagraph->setStyleValue($key, $value); 90 | } 91 | } else { 92 | $this->_styleParagraph = $styleParagraph; 93 | } 94 | 95 | $pattern = '/({.*?})/'; 96 | $this->_text = preg_split($pattern, $text, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); 97 | 98 | return $this; 99 | } 100 | 101 | /** 102 | * Get Text style 103 | * 104 | * @return PHPWord_Style_Font 105 | */ 106 | public function getFontStyle() { 107 | return $this->_styleFont; 108 | } 109 | 110 | /** 111 | * Get Paragraph style 112 | * 113 | * @return PHPWord_Style_Paragraph 114 | */ 115 | public function getParagraphStyle() { 116 | return $this->_styleParagraph; 117 | } 118 | 119 | /** 120 | * Get Text content 121 | * 122 | * @return string 123 | */ 124 | public function getText() { 125 | return $this->_text; 126 | } 127 | } 128 | ?> 129 | -------------------------------------------------------------------------------- /PHPWord/Section/Header.php: -------------------------------------------------------------------------------- 1 | _headerCount = $sectionCount; 64 | } 65 | 66 | /** 67 | * Add a Text Element 68 | * 69 | * @param string $text 70 | * @param mixed $styleFont 71 | * @param mixed $styleParagraph 72 | * @return PHPWord_Section_Text 73 | */ 74 | public function addText($text, $styleFont = null, $styleParagraph = null) { 75 | $givenText = utf8_encode($text); 76 | $text = new PHPWord_Section_Text($givenText, $styleFont, $styleParagraph); 77 | $this->_elementCollection[] = $text; 78 | return $text; 79 | } 80 | 81 | /** 82 | * Add a TextBreak Element 83 | * 84 | * @param int $count 85 | */ 86 | public function addTextBreak($count = 1) { 87 | for($i=1; $i<=$count; $i++) { 88 | $this->_elementCollection[] = new PHPWord_Section_TextBreak(); 89 | } 90 | } 91 | 92 | /** 93 | * Create a new TextRun 94 | * 95 | * @return PHPWord_Section_TextRun 96 | */ 97 | public function createTextRun($styleParagraph = null) { 98 | $textRun = new PHPWord_Section_TextRun($styleParagraph); 99 | $this->_elementCollection[] = $textRun; 100 | return $textRun; 101 | } 102 | 103 | /** 104 | * Add a Table Element 105 | * 106 | * @param mixed $style 107 | * @return PHPWord_Section_Table 108 | */ 109 | public function addTable($style = null) { 110 | $table = new PHPWord_Section_Table('header', $this->_headerCount, $style); 111 | $this->_elementCollection[] = $table; 112 | return $table; 113 | } 114 | 115 | /** 116 | * Add a Image Element 117 | * 118 | * @param string $src 119 | * @param mixed $style 120 | * @return PHPWord_Section_Image 121 | */ 122 | public function addImage($src, $style = null) { 123 | $image = new PHPWord_Section_Image($src, $style); 124 | 125 | if(!is_null($image->getSource())) { 126 | $rID = PHPWord_Media::addHeaderMediaElement($this->_headerCount, $src); 127 | $image->setRelationId($rID); 128 | 129 | $this->_elementCollection[] = $image; 130 | return $image; 131 | } else { 132 | trigger_error('Src does not exist or invalid image type.', E_ERROR); 133 | } 134 | } 135 | 136 | /** 137 | * Add a by PHP created Image Element 138 | * 139 | * @param string $link 140 | * @param mixed $style 141 | * @return PHPWord_Section_MemoryImage 142 | */ 143 | public function addMemoryImage($link, $style = null) { 144 | $memoryImage = new PHPWord_Section_MemoryImage($link, $style); 145 | if(!is_null($memoryImage->getSource())) { 146 | $rID = PHPWord_Media::addHeaderMediaElement($this->_headerCount, $link, $memoryImage); 147 | $memoryImage->setRelationId($rID); 148 | 149 | $this->_elementCollection[] = $memoryImage; 150 | return $memoryImage; 151 | } else { 152 | trigger_error('Unsupported image type.'); 153 | } 154 | } 155 | 156 | /** 157 | * Add a PreserveText Element 158 | * 159 | * @param string $text 160 | * @param mixed $styleFont 161 | * @param mixed $styleParagraph 162 | * @return PHPWord_Section_Footer_PreserveText 163 | */ 164 | public function addPreserveText($text, $styleFont = null, $styleParagraph = null) { 165 | $text = utf8_encode($text); 166 | $ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph); 167 | $this->_elementCollection[] = $ptext; 168 | return $ptext; 169 | } 170 | 171 | /** 172 | * Add a Watermark Element 173 | * 174 | * @param string $src 175 | * @param mixed $style 176 | * @return PHPWord_Section_Image 177 | */ 178 | public function addWatermark($src, $style = null) { 179 | $image = new PHPWord_Section_Image($src, $style, true); 180 | 181 | if(!is_null($image->getSource())) { 182 | $rID = PHPWord_Media::addHeaderMediaElement($this->_headerCount, $src); 183 | $image->setRelationId($rID); 184 | 185 | $this->_elementCollection[] = $image; 186 | return $image; 187 | } else { 188 | trigger_error('Src does not exist or invalid image type.', E_ERROR); 189 | } 190 | } 191 | 192 | /** 193 | * Get Header Relation ID 194 | */ 195 | public function getRelationId() { 196 | return $this->_rId; 197 | } 198 | 199 | /** 200 | * Set Header Relation ID 201 | * 202 | * @param int $rId 203 | */ 204 | public function setRelationId($rId) { 205 | $this->_rId = $rId; 206 | } 207 | 208 | /** 209 | * Get all Header Elements 210 | */ 211 | public function getElements() { 212 | return $this->_elementCollection; 213 | } 214 | 215 | /** 216 | * Get Header Count 217 | */ 218 | public function getHeaderCount() { 219 | return $this->_headerCount; 220 | } 221 | } 222 | ?> 223 | -------------------------------------------------------------------------------- /PHPWord/Section/Image.php: -------------------------------------------------------------------------------- 1 | _src = $src; 81 | $this->_isWatermark = $isWatermark; 82 | $this->_style = new PHPWord_Style_Image(); 83 | 84 | if(!is_null($style) && is_array($style)) { 85 | foreach($style as $key => $value) { 86 | if(substr($key, 0, 1) != '_') { 87 | $key = '_'.$key; 88 | } 89 | $this->_style->setStyleValue($key, $value); 90 | } 91 | } 92 | 93 | if($this->_style->getWidth() == null && $this->_style->getHeight() == null) { 94 | $imgData = getimagesize($this->_src); 95 | $this->_style->setWidth($imgData[0]); 96 | $this->_style->setHeight($imgData[1]); 97 | } 98 | 99 | return $this; 100 | } else { 101 | return false; 102 | } 103 | } 104 | 105 | /** 106 | * Get Image style 107 | * 108 | * @return PHPWord_Style_Image 109 | */ 110 | public function getStyle() { 111 | return $this->_style; 112 | } 113 | 114 | /** 115 | * Get Image Relation ID 116 | * 117 | * @return int 118 | */ 119 | public function getRelationId() { 120 | return $this->_rId; 121 | } 122 | 123 | /** 124 | * Set Image Relation ID 125 | * 126 | * @param int $rId 127 | */ 128 | public function setRelationId($rId) { 129 | $this->_rId = $rId; 130 | } 131 | 132 | /** 133 | * Get Image Source 134 | * 135 | * @return string 136 | */ 137 | public function getSource() { 138 | return $this->_src; 139 | } 140 | 141 | /** 142 | * Get Image Media ID 143 | * 144 | * @return string 145 | */ 146 | public function getMediaId() { 147 | return md5($this->_src); 148 | } 149 | 150 | /** 151 | * Get IsWatermark 152 | * 153 | * @return int 154 | */ 155 | public function getIsWatermark() { 156 | return $this->_isWatermark; 157 | } 158 | 159 | /** 160 | * Set IsWatermark 161 | * 162 | * @param bool $pValue 163 | */ 164 | public function setIsWatermark($pValue) { 165 | $this->_isWatermark = $pValue; 166 | } 167 | } 168 | ?> 169 | -------------------------------------------------------------------------------- /PHPWord/Section/Link.php: -------------------------------------------------------------------------------- 1 | _linkSrc = $linkSrc; 84 | $this->_linkName = $linkName; 85 | 86 | // Set font style 87 | if(is_array($styleFont)) { 88 | $this->_styleFont = new PHPWord_Style_Font('text'); 89 | 90 | foreach($styleFont as $key => $value) { 91 | if(substr($key, 0, 1) != '_') { 92 | $key = '_'.$key; 93 | } 94 | $this->_styleFont->setStyleValue($key, $value); 95 | } 96 | } else { 97 | $this->_styleFont = $styleFont; 98 | } 99 | 100 | // Set paragraph style 101 | if(is_array($styleParagraph)) { 102 | $this->_styleParagraph = new PHPWord_Style_Paragraph(); 103 | 104 | foreach($styleParagraph as $key => $value) { 105 | if(substr($key, 0, 1) != '_') { 106 | $key = '_'.$key; 107 | } 108 | $this->_styleParagraph->setStyleValue($key, $value); 109 | } 110 | } else { 111 | $this->_styleParagraph = $styleParagraph; 112 | } 113 | 114 | return $this; 115 | } 116 | 117 | /** 118 | * Get Link Relation ID 119 | * 120 | * @return int 121 | */ 122 | public function getRelationId() { 123 | return $this->_rId; 124 | } 125 | 126 | /** 127 | * Set Link Relation ID 128 | * 129 | * @param int $rId 130 | */ 131 | public function setRelationId($rId) { 132 | $this->_rId = $rId; 133 | } 134 | 135 | /** 136 | * Get Link source 137 | * 138 | * @return string 139 | */ 140 | public function getLinkSrc() { 141 | return $this->_linkSrc; 142 | } 143 | 144 | /** 145 | * Get Link name 146 | * 147 | * @return string 148 | */ 149 | public function getLinkName() { 150 | return $this->_linkName; 151 | } 152 | 153 | /** 154 | * Get Text style 155 | * 156 | * @return PHPWord_Style_Font 157 | */ 158 | public function getFontStyle() { 159 | return $this->_styleFont; 160 | } 161 | 162 | /** 163 | * Get Paragraph style 164 | * 165 | * @return PHPWord_Style_Paragraph 166 | */ 167 | public function getParagraphStyle() { 168 | return $this->_styleParagraph; 169 | } 170 | } 171 | ?> -------------------------------------------------------------------------------- /PHPWord/Section/ListItem.php: -------------------------------------------------------------------------------- 1 | _style = new PHPWord_Style_ListItem(); 70 | $this->_textObject = new PHPWord_Section_Text($text, $styleFont, $styleParagraph); 71 | $this->_depth = $depth; 72 | 73 | if(!is_null($styleList) && is_array($styleList)) { 74 | foreach($styleList as $key => $value) { 75 | if(substr($key, 0, 1) != '_') { 76 | $key = '_'.$key; 77 | } 78 | $this->_style->setStyleValue($key, $value); 79 | } 80 | } 81 | } 82 | 83 | /** 84 | * Get ListItem style 85 | */ 86 | public function getStyle() { 87 | return $this->_style; 88 | } 89 | 90 | /** 91 | * Get ListItem TextRun 92 | */ 93 | public function getTextObject() { 94 | return $this->_textObject; 95 | } 96 | 97 | /** 98 | * Get ListItem depth 99 | */ 100 | public function getDepth() { 101 | return $this->_depth; 102 | } 103 | } 104 | ?> -------------------------------------------------------------------------------- /PHPWord/Section/MemoryImage.php: -------------------------------------------------------------------------------- 1 | _imageType = $imgData['mime']; 97 | 98 | $_supportedImageTypes = array('image/jpeg', 'image/gif', 'image/png'); 99 | 100 | if(in_array($this->_imageType, $_supportedImageTypes)) { 101 | $this->_src = $src; 102 | $this->_style = new PHPWord_Style_Image(); 103 | 104 | if(!is_null($style) && is_array($style)) { 105 | foreach($style as $key => $value) { 106 | if(substr($key, 0, 1) != '_') { 107 | $key = '_'.$key; 108 | } 109 | $this->_style->setStyleValue($key, $value); 110 | } 111 | } 112 | 113 | if($this->_style->getWidth() == null && $this->_style->getHeight() == null) { 114 | $this->_style->setWidth($imgData[0]); 115 | $this->_style->setHeight($imgData[1]); 116 | } 117 | 118 | $this->_setFunctions(); 119 | 120 | return $this; 121 | } else { 122 | return false; 123 | } 124 | } 125 | 126 | /** 127 | * Set Functions 128 | */ 129 | private function _setFunctions() { 130 | switch($this->_imageType) { 131 | case 'image/png': 132 | $this->_imageCreateFunc = 'imagecreatefrompng'; 133 | $this->_imageFunc = 'imagepng'; 134 | $this->_imageExtension = 'png'; 135 | break; 136 | case 'image/gif': 137 | $this->_imageCreateFunc = 'imagecreatefromgif'; 138 | $this->_imageFunc = 'imagegif'; 139 | $this->_imageExtension = 'gif'; 140 | break; 141 | case 'image/jpeg': case 'image/jpg': 142 | $this->_imageCreateFunc = 'imagecreatefromjpeg'; 143 | $this->_imageFunc = 'imagejpeg'; 144 | $this->_imageExtension = 'jpg'; 145 | break; 146 | } 147 | } 148 | 149 | 150 | /** 151 | * Get Image style 152 | * 153 | * @return PHPWord_Style_Image 154 | */ 155 | public function getStyle() { 156 | return $this->_style; 157 | } 158 | 159 | /** 160 | * Get Image Relation ID 161 | * 162 | * @return int 163 | */ 164 | public function getRelationId() { 165 | return $this->_rId; 166 | } 167 | 168 | /** 169 | * Set Image Relation ID 170 | * 171 | * @param int $rId 172 | */ 173 | public function setRelationId($rId) { 174 | $this->_rId = $rId; 175 | } 176 | 177 | /** 178 | * Get Image Source 179 | * 180 | * @return string 181 | */ 182 | public function getSource() { 183 | return $this->_src; 184 | } 185 | 186 | /** 187 | * Get Image Media ID 188 | * 189 | * @return string 190 | */ 191 | public function getMediaId() { 192 | return md5($this->_src); 193 | } 194 | 195 | /** 196 | * Get Image Type 197 | * 198 | * @return string 199 | */ 200 | public function getImageType() { 201 | return $this->_imageType; 202 | } 203 | 204 | /** 205 | * Get Image Create Function 206 | * 207 | * @return string 208 | */ 209 | public function getImageCreateFunction() { 210 | return $this->_imageCreateFunc; 211 | } 212 | 213 | /** 214 | * Get Image Function 215 | * 216 | * @return string 217 | */ 218 | public function getImageFunction() { 219 | return $this->_imageFunc; 220 | } 221 | 222 | /** 223 | * Get Image Extension 224 | * 225 | * @return string 226 | */ 227 | public function getImageExtension() { 228 | return $this->_imageExtension; 229 | } 230 | } 231 | ?> -------------------------------------------------------------------------------- /PHPWord/Section/Object.php: -------------------------------------------------------------------------------- 1 | _src = $src; 86 | $this->_style = new PHPWord_Style_Image(); 87 | 88 | if(!is_null($style) && is_array($style)) { 89 | foreach($style as $key => $value) { 90 | if(substr($key, 0, 1) != '_') { 91 | $key = '_'.$key; 92 | } 93 | $this->_style->setStyleValue($key, $value); 94 | } 95 | } 96 | 97 | return $this; 98 | } else { 99 | return false; 100 | } 101 | } 102 | 103 | /** 104 | * Get Image style 105 | * 106 | * @return PHPWord_Style_Image 107 | */ 108 | public function getStyle() { 109 | return $this->_style; 110 | } 111 | 112 | /** 113 | * Get Source 114 | * 115 | * @return string 116 | */ 117 | public function getSource() { 118 | return $this->_src; 119 | } 120 | 121 | /** 122 | * Get Object Relation ID 123 | * 124 | * @return int 125 | */ 126 | public function getRelationId() { 127 | return $this->_rId; 128 | } 129 | 130 | /** 131 | * Set Object Relation ID 132 | * 133 | * @param int $rId 134 | */ 135 | public function setRelationId($rId) { 136 | $this->_rId = $rId; 137 | } 138 | 139 | /** 140 | * Get Image Relation ID 141 | * 142 | * @return int 143 | */ 144 | public function getImageRelationId() { 145 | return $this->_rIdImg; 146 | } 147 | 148 | /** 149 | * Set Image Relation ID 150 | * 151 | * @param int $rId 152 | */ 153 | public function setImageRelationId($rId) { 154 | $this->_rIdImg = $rId; 155 | } 156 | 157 | /** 158 | * Get Object ID 159 | * 160 | * @return int 161 | */ 162 | public function getObjectId() { 163 | return $this->_objId; 164 | } 165 | 166 | /** 167 | * Set Object ID 168 | * 169 | * @param int $objId 170 | */ 171 | public function setObjectId($objId) { 172 | $this->_objId = $objId; 173 | } 174 | } 175 | ?> 176 | -------------------------------------------------------------------------------- /PHPWord/Section/PageBreak.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /PHPWord/Section/Table.php: -------------------------------------------------------------------------------- 1 | _insideOf = $insideOf; 83 | $this->_pCount = $pCount; 84 | 85 | if(!is_null($style)) { 86 | if(is_array($style)) { 87 | $this->_style = new PHPWord_Style_Table(); 88 | 89 | foreach($style as $key => $value) { 90 | if(substr($key, 0, 1) != '_') { 91 | $key = '_'.$key; 92 | } 93 | $this->_style->setStyleValue($key, $value); 94 | } 95 | } else { 96 | $this->_style = $style; 97 | } 98 | } 99 | } 100 | 101 | /** 102 | * Add a row 103 | * 104 | * @param int $height 105 | */ 106 | public function addRow($height = null) { 107 | $this->_rows[] = array(); 108 | $this->_rowHeights[] = $height; 109 | } 110 | 111 | /** 112 | * Add a cell 113 | * 114 | * @param int $width 115 | * @param mixed $style 116 | * @return PHPWord_Section_Table_Cell 117 | */ 118 | public function addCell($width, $style = null) { 119 | $cell = new PHPWord_Section_Table_Cell($this->_insideOf, $this->_pCount, $width, $style); 120 | $i = count($this->_rows) - 1; 121 | $this->_rows[$i][] = $cell; 122 | return $cell; 123 | } 124 | 125 | /** 126 | * Get all rows 127 | * 128 | * @return array 129 | */ 130 | public function getRows() { 131 | return $this->_rows; 132 | } 133 | 134 | /** 135 | * Get all row heights 136 | * 137 | * @return array 138 | */ 139 | public function getRowHeights() { 140 | return $this->_rowHeights; 141 | } 142 | 143 | /** 144 | * Get table style 145 | * 146 | * @return PHPWord_Style_Table 147 | */ 148 | public function getStyle() { 149 | return $this->_style; 150 | } 151 | } 152 | ?> -------------------------------------------------------------------------------- /PHPWord/Section/Table/Cell.php: -------------------------------------------------------------------------------- 1 | _insideOf = $insideOf; 84 | $this->_pCount = $pCount; 85 | $this->_width = $width; 86 | 87 | if(!is_null($style)) { 88 | if(is_array($style)) { 89 | $this->_style = new PHPWord_Style_Cell(); 90 | 91 | foreach($style as $key => $value) { 92 | if(substr($key, 0, 1) != '_') { 93 | $key = '_'.$key; 94 | } 95 | $this->_style->setStyleValue($key, $value); 96 | } 97 | } else { 98 | $this->_style = $style; 99 | } 100 | } 101 | } 102 | 103 | /** 104 | * Add a Text Element 105 | * 106 | * @param string $text 107 | * @param mixed $style 108 | * @return PHPWord_Section_Text 109 | */ 110 | public function addText($text, $styleFont = null, $styleParagraph = null) { 111 | $text = utf8_encode($text); 112 | $text = new PHPWord_Section_Text($text, $styleFont, $styleParagraph); 113 | $this->_elementCollection[] = $text; 114 | return $text; 115 | } 116 | 117 | /** 118 | * Add a Link Element 119 | * 120 | * @param string $linkSrc 121 | * @param string $linkName 122 | * @param mixed $style 123 | * @return PHPWord_Section_Link 124 | */ 125 | public function addLink($linkSrc, $linkName = null, $style = null) { 126 | if($this->_insideOf == 'section') { 127 | $linkSrc = utf8_encode($linkSrc); 128 | if(!is_null($linkName)) { 129 | $linkName = utf8_encode($linkName); 130 | } 131 | 132 | $link = new PHPWord_Section_Link($linkSrc, $linkName, $style); 133 | $rID = PHPWord_Media::addSectionLinkElement($linkSrc); 134 | $link->setRelationId($rID); 135 | 136 | $this->_elementCollection[] = $link; 137 | return $link; 138 | } else { 139 | trigger_error('Unsupported Link header / footer reference'); 140 | return false; 141 | } 142 | } 143 | 144 | /** 145 | * Add a TextBreak Element 146 | * 147 | * @param int $count 148 | */ 149 | public function addTextBreak() { 150 | $this->_elementCollection[] = new PHPWord_Section_TextBreak(); 151 | } 152 | 153 | /** 154 | * Add a ListItem Element 155 | * 156 | * @param string $text 157 | * @param int $depth 158 | * @param mixed $styleText 159 | * @param mixed $styleList 160 | * @return PHPWord_Section_ListItem 161 | */ 162 | public function addListItem($text, $depth = 0, $styleText = null, $styleList = null) { 163 | $text = utf8_encode($text); 164 | $listItem = new PHPWord_Section_ListItem($text, $depth, $styleText, $styleList); 165 | $this->_elementCollection[] = $listItem; 166 | return $listItem; 167 | } 168 | 169 | /** 170 | * Add a Image Element 171 | * 172 | * @param string $src 173 | * @param mixed $style 174 | * @return PHPWord_Section_Image 175 | */ 176 | public function addImage($src, $style = null) { 177 | $image = new PHPWord_Section_Image($src, $style); 178 | 179 | if(!is_null($image->getSource())) { 180 | if($this->_insideOf == 'section') { 181 | $rID = PHPWord_Media::addSectionMediaElement($src, 'image'); 182 | } elseif($this->_insideOf == 'header') { 183 | $rID = PHPWord_Media::addHeaderMediaElement($this->_pCount, $src); 184 | } elseif($this->_insideOf == 'footer') { 185 | $rID = PHPWord_Media::addFooterMediaElement($this->_pCount, $src); 186 | } 187 | $image->setRelationId($rID); 188 | 189 | $this->_elementCollection[] = $image; 190 | return $image; 191 | } else { 192 | trigger_error('Source does not exist or unsupported image type.'); 193 | } 194 | } 195 | 196 | /** 197 | * Add a by PHP created Image Element 198 | * 199 | * @param string $link 200 | * @param mixed $style 201 | * @return PHPWord_Section_MemoryImage 202 | */ 203 | public function addMemoryImage($link, $style = null) { 204 | $memoryImage = new PHPWord_Section_MemoryImage($link, $style); 205 | if(!is_null($memoryImage->getSource())) { 206 | if($this->_insideOf == 'section') { 207 | $rID = PHPWord_Media::addSectionMediaElement($link, 'image', $memoryImage); 208 | } elseif($this->_insideOf == 'header') { 209 | $rID = PHPWord_Media::addHeaderMediaElement($this->_pCount, $link, $memoryImage); 210 | } elseif($this->_insideOf == 'footer') { 211 | $rID = PHPWord_Media::addFooterMediaElement($this->_pCount, $link, $memoryImage); 212 | } 213 | $memoryImage->setRelationId($rID); 214 | 215 | $this->_elementCollection[] = $memoryImage; 216 | return $memoryImage; 217 | } else { 218 | trigger_error('Unsupported image type.'); 219 | } 220 | } 221 | 222 | /** 223 | * Add a OLE-Object Element 224 | * 225 | * @param string $src 226 | * @param mixed $style 227 | * @return PHPWord_Section_Object 228 | */ 229 | public function addObject($src, $style = null) { 230 | $object = new PHPWord_Section_Object($src, $style); 231 | 232 | if(!is_null($object->getSource())) { 233 | $inf = pathinfo($src); 234 | $ext = $inf['extension']; 235 | if(strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') { 236 | $ext = substr($ext, 0, -1); 237 | } 238 | 239 | $iconSrc = PHPWORD_BASE_PATH . 'PHPWord/_staticDocParts/'; 240 | if(!file_exists($iconSrc.'_'.$ext.'.png')) { 241 | $iconSrc = $iconSrc.'_default.png'; 242 | } else { 243 | $iconSrc .= '_'.$ext.'.png'; 244 | } 245 | 246 | $rIDimg = PHPWord_Media::addSectionMediaElement($iconSrc, 'image'); 247 | $data = PHPWord_Media::addSectionMediaElement($src, 'oleObject'); 248 | $rID = $data[0]; 249 | $objectId = $data[1]; 250 | 251 | $object->setRelationId($rID); 252 | $object->setObjectId($objectId); 253 | $object->setImageRelationId($rIDimg); 254 | 255 | $this->_elementCollection[] = $object; 256 | return $object; 257 | } else { 258 | trigger_error('Source does not exist or unsupported object type.'); 259 | } 260 | } 261 | 262 | /** 263 | * Add a PreserveText Element 264 | * 265 | * @param string $text 266 | * @param mixed $styleFont 267 | * @param mixed $styleParagraph 268 | * @return PHPWord_Section_Footer_PreserveText 269 | */ 270 | public function addPreserveText($text, $styleFont = null, $styleParagraph = null) { 271 | if($this->_insideOf == 'footer' || $this->_insideOf == 'header') { 272 | $text = utf8_encode($text); 273 | $ptext = new PHPWord_Section_Footer_PreserveText($text, $styleFont, $styleParagraph); 274 | $this->_elementCollection[] = $ptext; 275 | return $ptext; 276 | } else { 277 | trigger_error('addPreserveText only supported in footer/header.'); 278 | } 279 | } 280 | 281 | /** 282 | * Create a new TextRun 283 | * 284 | * @return PHPWord_Section_TextRun 285 | */ 286 | public function createTextRun($styleParagraph = null) { 287 | $textRun = new PHPWord_Section_TextRun($styleParagraph); 288 | $this->_elementCollection[] = $textRun; 289 | return $textRun; 290 | } 291 | 292 | /** 293 | * Get all Elements 294 | * 295 | * @return array 296 | */ 297 | public function getElements() { 298 | return $this->_elementCollection; 299 | } 300 | 301 | /** 302 | * Get Cell Style 303 | * 304 | * @return PHPWord_Style_Cell 305 | */ 306 | public function getStyle() { 307 | return $this->_style; 308 | } 309 | 310 | /** 311 | * Get Cell width 312 | * 313 | * @return int 314 | */ 315 | public function getWidth() { 316 | return $this->_width; 317 | } 318 | } 319 | ?> 320 | -------------------------------------------------------------------------------- /PHPWord/Section/Text.php: -------------------------------------------------------------------------------- 1 | _styleFont = new PHPWord_Style_Font('text'); 70 | 71 | foreach($styleFont as $key => $value) { 72 | if(substr($key, 0, 1) != '_') { 73 | $key = '_'.$key; 74 | } 75 | $this->_styleFont->setStyleValue($key, $value); 76 | } 77 | } else { 78 | $this->_styleFont = $styleFont; 79 | } 80 | 81 | // Set paragraph style 82 | if(is_array($styleParagraph)) { 83 | $this->_styleParagraph = new PHPWord_Style_Paragraph(); 84 | 85 | foreach($styleParagraph as $key => $value) { 86 | if(substr($key, 0, 1) != '_') { 87 | $key = '_'.$key; 88 | } 89 | $this->_styleParagraph->setStyleValue($key, $value); 90 | } 91 | } else { 92 | $this->_styleParagraph = $styleParagraph; 93 | } 94 | 95 | $this->_text = $text; 96 | 97 | return $this; 98 | } 99 | 100 | /** 101 | * Get Text style 102 | * 103 | * @return PHPWord_Style_Font 104 | */ 105 | public function getFontStyle() { 106 | return $this->_styleFont; 107 | } 108 | 109 | /** 110 | * Get Paragraph style 111 | * 112 | * @return PHPWord_Style_Paragraph 113 | */ 114 | public function getParagraphStyle() { 115 | return $this->_styleParagraph; 116 | } 117 | 118 | /** 119 | * Get Text content 120 | * 121 | * @return string 122 | */ 123 | public function getText() { 124 | return $this->_text; 125 | } 126 | } 127 | ?> -------------------------------------------------------------------------------- /PHPWord/Section/TextBreak.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /PHPWord/Section/TextRun.php: -------------------------------------------------------------------------------- 1 | _elementCollection = array(); 58 | 59 | // Set paragraph style 60 | if(is_array($styleParagraph)) { 61 | $this->_styleParagraph = new PHPWord_Style_Paragraph(); 62 | 63 | foreach($styleParagraph as $key => $value) { 64 | if(substr($key, 0, 1) != '_') { 65 | $key = '_'.$key; 66 | } 67 | $this->_styleParagraph->setStyleValue($key, $value); 68 | } 69 | } else { 70 | $this->_styleParagraph = $styleParagraph; 71 | } 72 | } 73 | 74 | 75 | /** 76 | * Add a Text Element 77 | * 78 | * @var string $text 79 | * @var mixed $styleFont 80 | * @return PHPWord_Section_Text 81 | */ 82 | public function addText($text = null, $styleFont = null) { 83 | $givenText = utf8_encode($text); 84 | $text = new PHPWord_Section_Text($givenText, $styleFont); 85 | $this->_elementCollection[] = $text; 86 | return $text; 87 | } 88 | 89 | /** 90 | * Add a Link Element 91 | * 92 | * @param string $linkSrc 93 | * @param string $linkName 94 | * @param mixed $styleFont 95 | * @return PHPWord_Section_Link 96 | */ 97 | public function addLink($linkSrc, $linkName = null, $styleFont = null) { 98 | $linkSrc = utf8_encode($linkSrc); 99 | if(!is_null($linkName)) { 100 | $linkName = utf8_encode($linkName); 101 | } 102 | 103 | $link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont); 104 | $rID = PHPWord_Media::addSectionLinkElement($linkSrc); 105 | $link->setRelationId($rID); 106 | 107 | $this->_elementCollection[] = $link; 108 | return $link; 109 | } 110 | 111 | /** 112 | * Get TextRun content 113 | * 114 | * @return string 115 | */ 116 | public function getElements() { 117 | return $this->_elementCollection; 118 | } 119 | 120 | /** 121 | * Get Paragraph style 122 | * 123 | * @return PHPWord_Style_Paragraph 124 | */ 125 | public function getParagraphStyle() { 126 | return $this->_styleParagraph; 127 | } 128 | } 129 | ?> -------------------------------------------------------------------------------- /PHPWord/Section/Title.php: -------------------------------------------------------------------------------- 1 | _style = $style; 83 | } 84 | 85 | $this->_text = $text; 86 | $this->_depth = $depth; 87 | 88 | return $this; 89 | } 90 | 91 | /** 92 | * Set Anchor 93 | * 94 | * @var int $anchor 95 | */ 96 | public function setAnchor($anchor) { 97 | $this->_anchor = $anchor; 98 | } 99 | 100 | /** 101 | * Get Anchor 102 | * 103 | * @return int 104 | */ 105 | public function getAnchor() { 106 | return $this->_anchor; 107 | } 108 | 109 | /** 110 | * Set Bookmark ID 111 | * 112 | * @var int $bookmarkId 113 | */ 114 | public function setBookmarkId($bookmarkId) { 115 | $this->_bookmarkId = $bookmarkId; 116 | } 117 | 118 | /** 119 | * Get Anchor 120 | * 121 | * @return int 122 | */ 123 | public function getBookmarkId() { 124 | return $this->_bookmarkId; 125 | } 126 | 127 | /** 128 | * Get Title Text content 129 | * 130 | * @return string 131 | */ 132 | public function getText() { 133 | return $this->_text; 134 | } 135 | 136 | /** 137 | * Get Title style 138 | * 139 | * @return string 140 | */ 141 | public function getStyle() { 142 | return $this->_style; 143 | } 144 | } 145 | ?> -------------------------------------------------------------------------------- /PHPWord/Shared/Drawing.php: -------------------------------------------------------------------------------- 1 | open($zipFile) === true) { 48 | $returnValue = ($zip->getFromName($archiveFile) !== false); 49 | $zip->close(); 50 | return $returnValue; 51 | } else { 52 | return false; 53 | } 54 | } else { 55 | // Regular file_exists 56 | return file_exists($pFilename); 57 | } 58 | } 59 | 60 | /** 61 | * Returns canonicalized absolute pathname, also for ZIP archives 62 | * 63 | * @param string $pFilename 64 | * @return string 65 | */ 66 | public static function realpath($pFilename) { 67 | // Returnvalue 68 | $returnValue = ''; 69 | 70 | // Try using realpath() 71 | $returnValue = realpath($pFilename); 72 | 73 | // Found something? 74 | if ($returnValue == '' || is_null($returnValue)) { 75 | $pathArray = split('/' , $pFilename); 76 | while(in_array('..', $pathArray) && $pathArray[0] != '..') { 77 | for ($i = 0; $i < count($pathArray); ++$i) { 78 | if ($pathArray[$i] == '..' && $i > 0) { 79 | unset($pathArray[$i]); 80 | unset($pathArray[$i - 1]); 81 | break; 82 | } 83 | } 84 | } 85 | $returnValue = implode('/', $pathArray); 86 | } 87 | 88 | // Return 89 | return $returnValue; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /PHPWord/Shared/Font.php: -------------------------------------------------------------------------------- 1 | ) 108 | * element or in the shared string element. 109 | * 110 | * @param string $value Value to unescape 111 | * @return string 112 | */ 113 | public static function ControlCharacterOOXML2PHP($value = '') { 114 | if(empty(self::$_controlCharacters)) { 115 | self::_buildControlCharacters(); 116 | } 117 | 118 | return str_replace( array_keys(self::$_controlCharacters), array_values(self::$_controlCharacters), $value ); 119 | } 120 | 121 | /** 122 | * Convert from PHP control character to OpenXML escaped control character 123 | * 124 | * Excel 2007 team: 125 | * ---------------- 126 | * That's correct, control characters are stored directly in the shared-strings table. 127 | * We do encode characters that cannot be represented in XML using the following escape sequence: 128 | * _xHHHH_ where H represents a hexadecimal character in the character's value... 129 | * So you could end up with something like _x0008_ in a string (either in a cell value () 130 | * element or in the shared string element. 131 | * 132 | * @param string $value Value to escape 133 | * @return string 134 | */ 135 | public static function ControlCharacterPHP2OOXML($value = '') { 136 | if(empty(self::$_controlCharacters)) { 137 | self::_buildControlCharacters(); 138 | } 139 | 140 | return str_replace( array_values(self::$_controlCharacters), array_keys(self::$_controlCharacters), $value ); 141 | } 142 | 143 | /** 144 | * Check if a string contains UTF8 data 145 | * 146 | * @param string $value 147 | * @return boolean 148 | */ 149 | public static function IsUTF8($value = '') { 150 | return utf8_encode(utf8_decode($value)) === $value; 151 | } 152 | 153 | /** 154 | * Formats a numeric value as a string for output in various output writers 155 | * 156 | * @param mixed $value 157 | * @return string 158 | */ 159 | public static function FormatNumber($value) { 160 | return number_format($value, 2, '.', ''); 161 | } 162 | 163 | /** 164 | * Converts a UTF-8 string into BIFF8 Unicode string data (8-bit string length) 165 | * Writes the string using uncompressed notation, no rich text, no Asian phonetics 166 | * If mbstring extension is not available, ASCII is assumed, and compressed notation is used 167 | * although this will give wrong results for non-ASCII strings 168 | * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3 169 | * 170 | * @param string $value UTF-8 encoded string 171 | * @return string 172 | */ 173 | public static function UTF8toBIFF8UnicodeShort($value) 174 | { 175 | // character count 176 | $ln = self::CountCharacters($value, 'UTF-8'); 177 | 178 | // option flags 179 | $opt = (self::getIsMbstringEnabled() || self::getIsIconvEnabled()) ? 180 | 0x0001 : 0x0000; 181 | 182 | // characters 183 | $chars = self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8'); 184 | 185 | $data = pack('CC', $ln, $opt) . $chars; 186 | return $data; 187 | } 188 | 189 | /** 190 | * Converts a UTF-8 string into BIFF8 Unicode string data (16-bit string length) 191 | * Writes the string using uncompressed notation, no rich text, no Asian phonetics 192 | * If mbstring extension is not available, ASCII is assumed, and compressed notation is used 193 | * although this will give wrong results for non-ASCII strings 194 | * see OpenOffice.org's Documentation of the Microsoft Excel File Format, sect. 2.5.3 195 | * 196 | * @param string $value UTF-8 encoded string 197 | * @return string 198 | */ 199 | public static function UTF8toBIFF8UnicodeLong($value) 200 | { 201 | // character count 202 | $ln = self::CountCharacters($value, 'UTF-8'); 203 | 204 | // option flags 205 | $opt = (self::getIsMbstringEnabled() || self::getIsIconvEnabled()) ? 206 | 0x0001 : 0x0000; 207 | 208 | // characters 209 | $chars = self::ConvertEncoding($value, 'UTF-16LE', 'UTF-8'); 210 | 211 | $data = pack('vC', $ln, $opt) . $chars; 212 | return $data; 213 | } 214 | 215 | /** 216 | * Convert string from one encoding to another. First try mbstring, then iconv, or no convertion 217 | * 218 | * @param string $value 219 | * @param string $to Encoding to convert to, e.g. 'UTF-8' 220 | * @param string $from Encoding to convert from, e.g. 'UTF-16LE' 221 | * @return string 222 | */ 223 | public static function ConvertEncoding($value, $to, $from) 224 | { 225 | if (self::getIsMbstringEnabled()) { 226 | $value = mb_convert_encoding($value, $to, $from); 227 | return $value; 228 | } 229 | 230 | if (self::getIsIconvEnabled()) { 231 | $value = iconv($from, $to, $value); 232 | return $value; 233 | } 234 | 235 | // else, no conversion 236 | return $value; 237 | } 238 | 239 | /** 240 | * Get character count. First try mbstring, then iconv, finally strlen 241 | * 242 | * @param string $value 243 | * @param string $enc Encoding 244 | * @return int Character count 245 | */ 246 | public static function CountCharacters($value, $enc = 'UTF-8') 247 | { 248 | if (self::getIsMbstringEnabled()) { 249 | $count = mb_strlen($value, $enc); 250 | return $count; 251 | } 252 | 253 | if (self::getIsIconvEnabled()) { 254 | $count = iconv_strlen($value, $enc); 255 | return $count; 256 | } 257 | 258 | // else strlen 259 | $count = strlen($value); 260 | return $count; 261 | } 262 | 263 | } 264 | -------------------------------------------------------------------------------- /PHPWord/Shared/XMLWriter.php: -------------------------------------------------------------------------------- 1 | _xmlWriter = new XMLWriter(); 62 | 63 | // Open temporary storage 64 | if ($pTemporaryStorage == self::STORAGE_MEMORY) { 65 | $this->_xmlWriter->openMemory(); 66 | } else { 67 | // Create temporary filename 68 | $this->_tempFileName = @tempnam($pTemporaryStorageFolder, 'xml'); 69 | 70 | // Open storage 71 | if ($this->_xmlWriter->openUri($this->_tempFileName) === false) { 72 | // Fallback to memory... 73 | $this->_xmlWriter->openMemory(); 74 | } 75 | } 76 | 77 | // Set default values 78 | // proposed to be false in production version 79 | $this->_xmlWriter->setIndent(true); 80 | //$this->_xmlWriter->setIndent(false); 81 | 82 | // Set indent 83 | // proposed to be '' in production version 84 | $this->_xmlWriter->setIndentString(' '); 85 | //$this->_xmlWriter->setIndentString(''); 86 | } 87 | 88 | /** 89 | * Destructor 90 | */ 91 | public function __destruct() { 92 | // Desctruct XMLWriter 93 | unset($this->_xmlWriter); 94 | 95 | // Unlink temporary files 96 | if ($this->_tempFileName != '') { 97 | @unlink($this->_tempFileName); 98 | } 99 | } 100 | 101 | /** 102 | * Get written data 103 | * 104 | * @return $data 105 | */ 106 | public function getData() { 107 | if ($this->_tempFileName == '') { 108 | return $this->_xmlWriter->outputMemory(true); 109 | } else { 110 | $this->_xmlWriter->flush(); 111 | return file_get_contents($this->_tempFileName); 112 | } 113 | } 114 | 115 | /** 116 | * Catch function calls (and pass them to internal XMLWriter) 117 | * 118 | * @param unknown_type $function 119 | * @param unknown_type $args 120 | */ 121 | public function __call($function, $args) { 122 | try { 123 | @call_user_func_array(array($this->_xmlWriter, $function), $args); 124 | } catch (Exception $ex) { 125 | // Do nothing! 126 | } 127 | } 128 | 129 | /** 130 | * Fallback method for writeRaw, introduced in PHP 5.2 131 | * 132 | * @param string $text 133 | * @return string 134 | */ 135 | public function writeRaw($text) 136 | { 137 | if (isset($this->_xmlWriter) && is_object($this->_xmlWriter) && (method_exists($this->_xmlWriter, 'writeRaw'))) { 138 | return $this->_xmlWriter->writeRaw($text); 139 | } 140 | 141 | return $this->text($text); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /PHPWord/Shared/ZipStreamWrapper.php: -------------------------------------------------------------------------------- 1 | _archive = new ZipArchive(); 100 | $this->_archive->open($url['host']); 101 | 102 | $this->_fileNameInArchive = $url['fragment']; 103 | $this->_position = 0; 104 | $this->_data = $this->_archive->getFromName( $this->_fileNameInArchive ); 105 | 106 | return true; 107 | } 108 | 109 | /** 110 | * Stat stream 111 | */ 112 | public function stream_stat() { 113 | return $this->_archive->statName( $this->_fileNameInArchive ); 114 | } 115 | 116 | /** 117 | * Read stream 118 | */ 119 | function stream_read($count) { 120 | $ret = substr($this->_data, $this->_position, $count); 121 | $this->_position += strlen($ret); 122 | return $ret; 123 | } 124 | 125 | /** 126 | * Tell stream 127 | */ 128 | public function stream_tell() { 129 | return $this->_position; 130 | } 131 | 132 | /** 133 | * EOF stream 134 | */ 135 | public function stream_eof() { 136 | return $this->_position >= strlen($this->_data); 137 | } 138 | 139 | /** 140 | * Seek stream 141 | */ 142 | public function stream_seek($offset, $whence) { 143 | switch ($whence) { 144 | case SEEK_SET: 145 | if ($offset < strlen($this->_data) && $offset >= 0) { 146 | $this->_position = $offset; 147 | return true; 148 | } else { 149 | return false; 150 | } 151 | break; 152 | 153 | case SEEK_CUR: 154 | if ($offset >= 0) { 155 | $this->_position += $offset; 156 | return true; 157 | } else { 158 | return false; 159 | } 160 | break; 161 | 162 | case SEEK_END: 163 | if (strlen($this->_data) + $offset >= 0) { 164 | $this->_position = strlen($this->_data) + $offset; 165 | return true; 166 | } else { 167 | return false; 168 | } 169 | break; 170 | 171 | default: 172 | return false; 173 | } 174 | } 175 | } 176 | ?> 177 | -------------------------------------------------------------------------------- /PHPWord/Style.php: -------------------------------------------------------------------------------- 1 | $value) { 56 | if(substr($key, 0, 1) != '_') { 57 | $key = '_'.$key; 58 | } 59 | $style->setStyleValue($key, $value); 60 | } 61 | 62 | self::$_styleElements[$styleName] = $style; 63 | } 64 | } 65 | 66 | /** 67 | * Add a font style 68 | * 69 | * @param string $styleName 70 | * @param array $styleFont 71 | * @param array $styleParagraph 72 | */ 73 | public static function addFontStyle($styleName, $styleFont, $styleParagraph = null) { 74 | if(!array_key_exists($styleName, self::$_styleElements)) { 75 | $font = new PHPWord_Style_Font('text', $styleParagraph); 76 | foreach($styleFont as $key => $value) { 77 | if(substr($key, 0, 1) != '_') { 78 | $key = '_'.$key; 79 | } 80 | $font->setStyleValue($key, $value); 81 | } 82 | self::$_styleElements[$styleName] = $font; 83 | } 84 | } 85 | 86 | /** 87 | * Add a link style 88 | * 89 | * @param string $styleName 90 | * @param array $styles 91 | */ 92 | public static function addLinkStyle($styleName, $styles) { 93 | if(!array_key_exists($styleName, self::$_styleElements)) { 94 | $style = new PHPWord_Style_Font('link'); 95 | foreach($styles as $key => $value) { 96 | if(substr($key, 0, 1) != '_') { 97 | $key = '_'.$key; 98 | } 99 | $style->setStyleValue($key, $value); 100 | } 101 | 102 | self::$_styleElements[$styleName] = $style; 103 | } 104 | } 105 | 106 | /** 107 | * Add a table style 108 | * 109 | * @param string $styleName 110 | * @param array $styles 111 | */ 112 | public static function addTableStyle($styleName, $styleTable, $styleFirstRow = null, $styleLastRow = null) { 113 | if(!array_key_exists($styleName, self::$_styleElements)) { 114 | $style = new PHPWord_Style_TableFull($styleTable, $styleFirstRow, $styleLastRow); 115 | 116 | self::$_styleElements[$styleName] = $style; 117 | } 118 | } 119 | 120 | /** 121 | * Add a title style 122 | * 123 | * @param string $styleName 124 | * @param array $styleFont 125 | * @param array $styleParagraph 126 | */ 127 | public static function addTitleStyle($titleCount, $styleFont, $styleParagraph = null) { 128 | $styleName = 'Heading_'.$titleCount; 129 | if(!array_key_exists($styleName, self::$_styleElements)) { 130 | $font = new PHPWord_Style_Font('title', $styleParagraph); 131 | foreach($styleFont as $key => $value) { 132 | if(substr($key, 0, 1) != '_') { 133 | $key = '_'.$key; 134 | } 135 | $font->setStyleValue($key, $value); 136 | } 137 | 138 | self::$_styleElements[$styleName] = $font; 139 | } 140 | } 141 | 142 | /** 143 | * Get all styles 144 | * 145 | * @return PHPWord_Style_Font[] 146 | */ 147 | public static function getStyles() { 148 | return self::$_styleElements; 149 | } 150 | } 151 | ?> 152 | -------------------------------------------------------------------------------- /PHPWord/Style/Cell.php: -------------------------------------------------------------------------------- 1 | _valign = null; 131 | $this->_textDirection = null; 132 | $this->_bgColor = null; 133 | $this->_borderTopSize = null; 134 | $this->_borderTopColor = null; 135 | $this->_borderLeftSize = null; 136 | $this->_borderLeftColor = null; 137 | $this->_borderRightSize = null; 138 | $this->_borderRightColor = null; 139 | $this->_borderBottomSize = null; 140 | $this->_borderBottomColor = null; 141 | $this->_defaultBorderColor = '000000'; 142 | } 143 | 144 | /** 145 | * Set style value 146 | * 147 | * @var string $key 148 | * @var mixed $value 149 | */ 150 | public function setStyleValue($key, $value) { 151 | if($key == '_borderSize') { 152 | $this->setBorderSize($value); 153 | } elseif($key == '_borderColor') { 154 | $this->setBorderColor($value); 155 | } else { 156 | $this->$key = $value; 157 | } 158 | } 159 | 160 | public function getVAlign() { 161 | return $this->_valign; 162 | } 163 | 164 | public function setVAlign($pValue = null) { 165 | $this->_valign = $pValue; 166 | } 167 | 168 | public function getTextDirection() { 169 | return $this->_textDirection; 170 | } 171 | 172 | public function setTextDirection($pValue = null) { 173 | $this->_textDirection = $pValue; 174 | } 175 | 176 | public function getBgColor() { 177 | return $this->_bgColor; 178 | } 179 | 180 | public function setBgColor($pValue = null) { 181 | $this->_bgColor = $pValue; 182 | } 183 | 184 | public function setHeight($pValue = null) { 185 | $this->_height = $pValue; 186 | } 187 | 188 | public function setBorderSize($pValue = null) { 189 | $this->_borderTopSize = $pValue; 190 | $this->_borderLeftSize = $pValue; 191 | $this->_borderRightSize = $pValue; 192 | $this->_borderBottomSize = $pValue; 193 | } 194 | 195 | public function getBorderSize() { 196 | $t = $this->getBorderTopSize(); 197 | $l = $this->getBorderLeftSize(); 198 | $r = $this->getBorderRightSize(); 199 | $b = $this->getBorderBottomSize(); 200 | 201 | return array($t, $l, $r, $b); 202 | } 203 | 204 | public function setBorderColor($pValue = null) { 205 | $this->_borderTopColor = $pValue; 206 | $this->_borderLeftColor = $pValue; 207 | $this->_borderRightColor = $pValue; 208 | $this->_borderBottomColor = $pValue; 209 | } 210 | 211 | public function getBorderColor() { 212 | $t = $this->getBorderTopColor(); 213 | $l = $this->getBorderLeftColor(); 214 | $r = $this->getBorderRightColor(); 215 | $b = $this->getBorderBottomColor(); 216 | 217 | return array($t, $l, $r, $b); 218 | } 219 | 220 | public function setBorderTopSize($pValue = null) { 221 | $this->_borderTopSize = $pValue; 222 | } 223 | 224 | public function getBorderTopSize() { 225 | return $this->_borderTopSize; 226 | } 227 | 228 | public function setBorderTopColor($pValue = null) { 229 | $this->_borderTopColor = $pValue; 230 | } 231 | 232 | public function getBorderTopColor() { 233 | return $this->_borderTopColor; 234 | } 235 | 236 | 237 | public function setBorderLeftSize($pValue = null) { 238 | $this->_borderLeftSize = $pValue; 239 | } 240 | 241 | public function getBorderLeftSize() { 242 | return $this->_borderLeftSize; 243 | } 244 | 245 | public function setBorderLeftColor($pValue = null) { 246 | $this->_borderLeftColor = $pValue; 247 | } 248 | 249 | public function getBorderLeftColor() { 250 | return $this->_borderLeftColor; 251 | } 252 | 253 | 254 | public function setBorderRightSize($pValue = null) { 255 | $this->_borderRightSize = $pValue; 256 | } 257 | 258 | public function getBorderRightSize() { 259 | return $this->_borderRightSize; 260 | } 261 | 262 | public function setBorderRightColor($pValue = null) { 263 | $this->_borderRightColor = $pValue; 264 | } 265 | 266 | public function getBorderRightColor() { 267 | return $this->_borderRightColor; 268 | } 269 | 270 | 271 | public function setBorderBottomSize($pValue = null) { 272 | $this->_borderBottomSize = $pValue; 273 | } 274 | 275 | public function getBorderBottomSize() { 276 | return $this->_borderBottomSize; 277 | } 278 | 279 | public function setBorderBottomColor($pValue = null) { 280 | $this->_borderBottomColor = $pValue; 281 | } 282 | 283 | public function getBorderBottomColor() { 284 | return $this->_borderBottomColor; 285 | } 286 | 287 | public function getDefaultBorderColor() { 288 | return $this->_defaultBorderColor; 289 | } 290 | } 291 | ?> -------------------------------------------------------------------------------- /PHPWord/Style/Font.php: -------------------------------------------------------------------------------- 1 | _type = $type; 100 | $this->_name = 'Arial'; 101 | $this->_size = 20; 102 | $this->_bold = false; 103 | $this->_italic = false; 104 | $this->_superScript = false; 105 | $this->_subScript = false; 106 | $this->_underline = PHPWord_Style_Font::UNDERLINE_NONE; 107 | $this->_strikethrough = false; 108 | $this->_color = '000000'; 109 | $this->_fgColor = null; 110 | 111 | if(!is_null($styleParagraph)) { 112 | $paragraph = new PHPWord_Style_Paragraph(); 113 | foreach($styleParagraph as $key => $value) { 114 | if(substr($key, 0, 1) != '_') { 115 | $key = '_'.$key; 116 | } 117 | $paragraph->setStyleValue($key, $value); 118 | } 119 | $this->_paragraphStyle = $paragraph; 120 | } else { 121 | $this->_paragraphStyle = null; 122 | } 123 | } 124 | 125 | public function getName() { 126 | return $this->_name; 127 | } 128 | 129 | public function setStyleValue($key, $value) { 130 | if($key == '_size') { 131 | $value *= 2; 132 | } 133 | $this->$key = $value; 134 | } 135 | 136 | public function setName($pValue = 'Arial') { 137 | if($pValue == '') { 138 | $pValue = 'Arial'; 139 | } 140 | $this->_name = $pValue; 141 | return $this; 142 | } 143 | 144 | public function getSize() { 145 | return $this->_size; 146 | } 147 | 148 | public function setSize($pValue = 20) { 149 | if($pValue == '') { 150 | $pValue = 20; 151 | } 152 | $this->_size = ($pValue*2); 153 | return $this; 154 | } 155 | 156 | public function getBold() { 157 | return $this->_bold; 158 | } 159 | 160 | public function setBold($pValue = false) { 161 | if($pValue == '') { 162 | $pValue = false; 163 | } 164 | $this->_bold = $pValue; 165 | return $this; 166 | } 167 | 168 | public function getItalic() { 169 | return $this->_italic; 170 | } 171 | 172 | public function setItalic($pValue = false) { 173 | if($pValue == '') { 174 | $pValue = false; 175 | } 176 | $this->_italic = $pValue; 177 | return $this; 178 | } 179 | 180 | public function getSuperScript() { 181 | return $this->_superScript; 182 | } 183 | 184 | public function setSuperScript($pValue = false) { 185 | if($pValue == '') { 186 | $pValue = false; 187 | } 188 | $this->_superScript = $pValue; 189 | $this->_subScript = !$pValue; 190 | return $this; 191 | } 192 | 193 | public function getSubScript() { 194 | return $this->_subScript; 195 | } 196 | 197 | public function setSubScript($pValue = false) { 198 | if($pValue == '') { 199 | $pValue = false; 200 | } 201 | $this->_subScript = $pValue; 202 | $this->_superScript = !$pValue; 203 | return $this; 204 | } 205 | 206 | public function getUnderline() { 207 | return $this->_underline; 208 | } 209 | 210 | public function setUnderline($pValue = PHPWord_Style_Font::UNDERLINE_NONE) { 211 | if ($pValue == '') { 212 | $pValue = PHPWord_Style_Font::UNDERLINE_NONE; 213 | } 214 | $this->_underline = $pValue; 215 | return $this; 216 | } 217 | 218 | public function getStrikethrough() { 219 | return $this->_strikethrough; 220 | } 221 | 222 | public function setStrikethrough($pValue = false) { 223 | if($pValue == '') { 224 | $pValue = false; 225 | } 226 | $this->_strikethrough = $pValue; 227 | return $this; 228 | } 229 | 230 | public function getColor() { 231 | return $this->_color; 232 | } 233 | 234 | public function setColor($pValue = '000000') { 235 | $this->_color = $pValue; 236 | return $this; 237 | } 238 | 239 | public function getFgColor() { 240 | return $this->_fgColor; 241 | } 242 | 243 | public function setFgColor($pValue = null) { 244 | $this->_fgColor = $pValue; 245 | return $this; 246 | } 247 | 248 | public function getStyleType() { 249 | return $this->_type; 250 | } 251 | 252 | /** 253 | * Get Paragraph style 254 | * 255 | * @return PHPWord_Style_Paragraph 256 | */ 257 | public function getParagraphStyle() { 258 | return $this->_paragraphStyle; 259 | } 260 | } 261 | ?> 262 | -------------------------------------------------------------------------------- /PHPWord/Style/Image.php: -------------------------------------------------------------------------------- 1 | _width = null; 58 | $this->_height = null; 59 | $this->_align = null; 60 | $this->_marginTop = null; 61 | $this->_marginLeft = null; 62 | } 63 | 64 | public function setStyleValue($key, $value) { 65 | $this->$key = $value; 66 | } 67 | 68 | public function getWidth() { 69 | return $this->_width; 70 | } 71 | 72 | public function setWidth($pValue = null) { 73 | $this->_width = $pValue; 74 | } 75 | 76 | public function getHeight() { 77 | return $this->_height; 78 | } 79 | 80 | public function setHeight($pValue = null) { 81 | $this->_height = $pValue; 82 | } 83 | 84 | public function getAlign() { 85 | return $this->_align; 86 | } 87 | 88 | public function setAlign($pValue = null) { 89 | $this->_align = $pValue; 90 | } 91 | 92 | /** 93 | * Get Margin Top 94 | * 95 | * @return int 96 | */ 97 | public function getMarginTop() { 98 | return $this->_marginTop; 99 | } 100 | 101 | /** 102 | * Set Margin Top 103 | * 104 | * @param int $pValue 105 | */ 106 | public function setMarginTop($pValue = null) { 107 | $this->_marginTop = $pValue; 108 | return $this; 109 | } 110 | 111 | /** 112 | * Get Margin Left 113 | * 114 | * @return int 115 | */ 116 | public function getMarginLeft() { 117 | return $this->_marginLeft; 118 | } 119 | 120 | /** 121 | * Set Margin Left 122 | * 123 | * @param int $pValue 124 | */ 125 | public function setMarginLeft($pValue = null) { 126 | $this->_marginLeft = $pValue; 127 | return $this; 128 | } 129 | } 130 | ?> 131 | -------------------------------------------------------------------------------- /PHPWord/Style/ListItem.php: -------------------------------------------------------------------------------- 1 | _listType = PHPWord_Style_ListItem::TYPE_BULLET_FILLED; 54 | } 55 | 56 | /** 57 | * Set style value 58 | * 59 | * @param string $key 60 | * @param string $value 61 | */ 62 | public function setStyleValue($key, $value) { 63 | $this->$key = $value; 64 | } 65 | 66 | /** 67 | * Set List Type 68 | * 69 | * @param int $pValue 70 | */ 71 | public function setListType($pValue = PHPWord_Style_ListItem::TYPE_BULLET_FILLED) { 72 | $this->_listType = $pValue; 73 | } 74 | 75 | /** 76 | * Get List Type 77 | */ 78 | public function getListType() { 79 | return $this->_listType; 80 | } 81 | } 82 | ?> 83 | -------------------------------------------------------------------------------- /PHPWord/Style/Paragraph.php: -------------------------------------------------------------------------------- 1 | _align = null; 72 | $this->_spaceBefore = null; 73 | $this->_spaceAfter = null; 74 | $this->_spacing = null; 75 | } 76 | 77 | /** 78 | * Set Style value 79 | * 80 | * @param string $key 81 | * @param mixed $value 82 | */ 83 | public function setStyleValue($key, $value) { 84 | if($key == '_spacing') { 85 | $value += 240; // because line height of 1 matches 240 twips 86 | } 87 | $this->$key = $value; 88 | } 89 | 90 | /** 91 | * Get Paragraph Alignment 92 | * 93 | * @return string 94 | */ 95 | public function getAlign() { 96 | return $this->_align; 97 | } 98 | 99 | /** 100 | * Set Paragraph Alignment 101 | * 102 | * @param string $pValue 103 | * @return PHPWord_Style_Paragraph 104 | */ 105 | public function setAlign($pValue = null) { 106 | if(strtolower($pValue) == 'justify') { 107 | // justify becames both 108 | $pValue = 'both'; 109 | } 110 | $this->_align = $pValue; 111 | return $this; 112 | } 113 | 114 | /** 115 | * Get Space before Paragraph 116 | * 117 | * @return string 118 | */ 119 | public function getSpaceBefore() { 120 | return $this->_spaceBefore; 121 | } 122 | 123 | /** 124 | * Set Space before Paragraph 125 | * 126 | * @param int $pValue 127 | * @return PHPWord_Style_Paragraph 128 | */ 129 | public function setSpaceBefore($pValue = null) { 130 | $this->_spaceBefore = $pValue; 131 | return $this; 132 | } 133 | 134 | /** 135 | * Get Space after Paragraph 136 | * 137 | * @return string 138 | */ 139 | public function getSpaceAfter() { 140 | return $this->_spaceAfter; 141 | } 142 | 143 | /** 144 | * Set Space after Paragraph 145 | * 146 | * @param int $pValue 147 | * @return PHPWord_Style_Paragraph 148 | */ 149 | public function setSpaceAfter($pValue = null) { 150 | $this->_spaceAfter = $pValue; 151 | return $this; 152 | } 153 | 154 | /** 155 | * Get Spacing between breaks 156 | * 157 | * @return int 158 | */ 159 | public function getSpacing() { 160 | return $this->_spacing; 161 | } 162 | 163 | /** 164 | * Set Spacing between breaks 165 | * 166 | * @param int $pValue 167 | * @return PHPWord_Style_Paragraph 168 | */ 169 | public function setSpacing($pValue = null) { 170 | $this->_spacing = $pValue; 171 | return $this; 172 | } 173 | } 174 | ?> -------------------------------------------------------------------------------- /PHPWord/Style/TOC.php: -------------------------------------------------------------------------------- 1 | _tabPos = 9062; 70 | $this->_tabLeader = PHPWord_Style_TOC::TABLEADER_DOT; 71 | $this->_indent = 200; 72 | } 73 | 74 | /** 75 | * Get Tab Position 76 | * 77 | * @return int 78 | */ 79 | public function getTabPos() { 80 | return $this->_tabPos; 81 | } 82 | 83 | /** 84 | * Set Tab Position 85 | * 86 | * @param int $pValue 87 | */ 88 | public function setTabPos($pValue) { 89 | $this->_tabLeader = $pValue; 90 | } 91 | 92 | /** 93 | * Get Tab Leader 94 | * 95 | * @return string 96 | */ 97 | public function getTabLeader() { 98 | return $this->_tabLeader; 99 | } 100 | 101 | /** 102 | * Set Tab Leader 103 | * 104 | * @param string $pValue 105 | */ 106 | public function setTabLeader($pValue = PHPWord_Style_TOC::TABLEADER_DOT) { 107 | $this->_tabLeader = $pValue; 108 | } 109 | 110 | /** 111 | * Get Indent 112 | * 113 | * @return int 114 | */ 115 | public function getIndent() { 116 | return $this->_indent; 117 | } 118 | 119 | /** 120 | * Set Indent 121 | * 122 | * @param string $pValue 123 | */ 124 | public function setIndent($pValue) { 125 | $this->_indent = $pValue; 126 | } 127 | 128 | /** 129 | * Set style value 130 | * 131 | * @param string $key 132 | * @param string $value 133 | */ 134 | public function setStyleValue($key, $value) { 135 | $this->$key = $value; 136 | } 137 | } 138 | ?> 139 | -------------------------------------------------------------------------------- /PHPWord/Style/Table.php: -------------------------------------------------------------------------------- 1 | _cellMarginTop = null; 38 | $this->_cellMarginLeft = null; 39 | $this->_cellMarginRight = null; 40 | $this->_cellMarginBottom = null; 41 | } 42 | 43 | public function setStyleValue($key, $value) { 44 | $this->$key = $value; 45 | } 46 | 47 | public function setCellMarginTop($pValue = null) { 48 | $this->_cellMarginTop = $pValue; 49 | } 50 | 51 | public function getCellMarginTop() { 52 | return $this->_cellMarginTop; 53 | } 54 | 55 | public function setCellMarginLeft($pValue = null) { 56 | $this->_cellMarginLeft = $pValue; 57 | } 58 | 59 | public function getCellMarginLeft() { 60 | return $this->_cellMarginLeft; 61 | } 62 | 63 | public function setCellMarginRight($pValue = null) { 64 | $this->_cellMarginRight = $pValue; 65 | } 66 | 67 | public function getCellMarginRight() { 68 | return $this->_cellMarginRight; 69 | } 70 | 71 | public function setCellMarginBottom($pValue = null) { 72 | $this->_cellMarginBottom = $pValue; 73 | } 74 | 75 | public function getCellMarginBottom() { 76 | return $this->_cellMarginBottom; 77 | } 78 | 79 | public function getCellMargin() { 80 | return array($this->_cellMarginTop, $this->_cellMarginLeft, $this->_cellMarginRight, $this->_cellMarginBottom); 81 | } 82 | } 83 | ?> -------------------------------------------------------------------------------- /PHPWord/TOC.php: -------------------------------------------------------------------------------- 1 | $value) { 85 | if(substr($key, 0, 1) != '_') { 86 | $key = '_'.$key; 87 | } 88 | self::$_styleTOC->setStyleValue($key, $value); 89 | } 90 | } 91 | 92 | if(!is_null($styleFont)) { 93 | if(is_array($styleFont)) { 94 | self::$_styleFont = new PHPWord_Style_Font(); 95 | 96 | foreach($styleFont as $key => $value) { 97 | if(substr($key, 0, 1) != '_') { 98 | $key = '_'.$key; 99 | } 100 | self::$_styleFont->setStyleValue($key, $value); 101 | } 102 | } else { 103 | self::$_styleFont = $styleFont; 104 | } 105 | } 106 | } 107 | 108 | /** 109 | * Add a Title 110 | * 111 | * @return array 112 | */ 113 | public static function addTitle($text, $depth = 0) { 114 | $anchor = '_Toc'.++self::$_anchor; 115 | $bookmarkId = self::$_bookmarkId++; 116 | 117 | $title = array(); 118 | $title['text'] = $text; 119 | $title['depth'] = $depth; 120 | $title['anchor'] = $anchor; 121 | $title['bookmarkId'] = $bookmarkId; 122 | 123 | self::$_titles[] = $title; 124 | 125 | return array($anchor, $bookmarkId); 126 | } 127 | 128 | /** 129 | * Get all titles 130 | * 131 | * @return array 132 | */ 133 | public static function getTitles() { 134 | return self::$_titles; 135 | } 136 | 137 | /** 138 | * Get TOC Style 139 | * 140 | * @return PHPWord_Style_TOC 141 | */ 142 | public static function getStyleTOC() { 143 | return self::$_styleTOC; 144 | } 145 | 146 | /** 147 | * Get Font Style 148 | * 149 | * @return PHPWord_Style_Font 150 | */ 151 | public static function getStyleFont() { 152 | return self::$_styleFont; 153 | } 154 | } 155 | ?> -------------------------------------------------------------------------------- /PHPWord/Template.php: -------------------------------------------------------------------------------- 1 | _tempFileName = $path.DIRECTORY_SEPARATOR.time().'.docx'; 68 | 69 | copy($strFilename, $this->_tempFileName); // Copy the source File to the temp File 70 | 71 | $this->_objZip = new ZipArchive(); 72 | $this->_objZip->open($this->_tempFileName); 73 | 74 | $this->_documentXML = $this->_objZip->getFromName('word/document.xml'); 75 | } 76 | 77 | /** 78 | * Set a Template value 79 | * 80 | * @param mixed $search 81 | * @param mixed $replace 82 | */ 83 | public function setValue($search, $replace) { 84 | if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') { 85 | $search = '${'.$search.'}'; 86 | } 87 | 88 | if(!is_array($replace)) { 89 | $replace = utf8_encode($replace); 90 | } 91 | 92 | $this->_documentXML = str_replace($search, $replace, $this->_documentXML); 93 | } 94 | 95 | /** 96 | * Save Template 97 | * 98 | * @param string $strFilename 99 | */ 100 | public function save($strFilename) { 101 | if(file_exists($strFilename)) { 102 | unlink($strFilename); 103 | } 104 | 105 | $this->_objZip->addFromString('word/document.xml', $this->_documentXML); 106 | 107 | // Close zip file 108 | if($this->_objZip->close() === false) { 109 | throw new Exception('Could not close zip file.'); 110 | } 111 | 112 | rename($this->_tempFileName, $strFilename); 113 | } 114 | } 115 | ?> -------------------------------------------------------------------------------- /PHPWord/Writer/IWriter.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /PHPWord/Writer/Word2007/ContentTypes.php: -------------------------------------------------------------------------------- 1 | getParentWriter()->getUseDiskCaching()) { 35 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); 36 | } else { 37 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY); 38 | } 39 | 40 | // XML header 41 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); 42 | 43 | // Types 44 | $objWriter->startElement('Types'); 45 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types'); 46 | 47 | // Rels 48 | $this->_writeDefaultContentType( 49 | $objWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml' 50 | ); 51 | 52 | // XML 53 | $this->_writeDefaultContentType( 54 | $objWriter, 'xml', 'application/xml' 55 | ); 56 | 57 | // Add media content-types 58 | foreach($_imageTypes as $key => $value) { 59 | $this->_writeDefaultContentType($objWriter, $key, $value); 60 | } 61 | 62 | // Add embedding content-types 63 | if(count($_objectTypes) > 0) { 64 | $this->_writeDefaultContentType($objWriter, 'bin', 'application/vnd.openxmlformats-officedocument.oleObject'); 65 | } 66 | 67 | // DocProps 68 | $this->_writeOverrideContentType( 69 | $objWriter, '/docProps/app.xml', 'application/vnd.openxmlformats-officedocument.extended-properties+xml' 70 | ); 71 | 72 | $this->_writeOverrideContentType( 73 | $objWriter, '/docProps/core.xml', 'application/vnd.openxmlformats-package.core-properties+xml' 74 | ); 75 | 76 | // Document 77 | $this->_writeOverrideContentType( 78 | $objWriter, '/word/document.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml' 79 | ); 80 | 81 | // Styles 82 | $this->_writeOverrideContentType( 83 | $objWriter, '/word/styles.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml' 84 | ); 85 | 86 | // Numbering 87 | $this->_writeOverrideContentType( 88 | $objWriter, '/word/numbering.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml' 89 | ); 90 | 91 | // Settings 92 | $this->_writeOverrideContentType( 93 | $objWriter, '/word/settings.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml' 94 | ); 95 | 96 | // Theme1 97 | $this->_writeOverrideContentType( 98 | $objWriter, '/word/theme/theme1.xml', 'application/vnd.openxmlformats-officedocument.theme+xml' 99 | ); 100 | 101 | // WebSettings 102 | $this->_writeOverrideContentType( 103 | $objWriter, '/word/webSettings.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml' 104 | ); 105 | 106 | // Font Table 107 | $this->_writeOverrideContentType( 108 | $objWriter, '/word/fontTable.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml' 109 | ); 110 | 111 | for($i=1; $i<=$_cHdrs; $i++) { 112 | $this->_writeOverrideContentType( 113 | $objWriter, '/word/header'.$i.'.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml' 114 | ); 115 | } 116 | 117 | for($i=1; $i<=$_cFtrs; $i++) { 118 | $this->_writeOverrideContentType( 119 | $objWriter, '/word/footer'.$i.'.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml' 120 | ); 121 | } 122 | 123 | 124 | $objWriter->endElement(); 125 | 126 | // Return 127 | return $objWriter->getData(); 128 | } 129 | 130 | /** 131 | * Get image mime type 132 | * 133 | * @param string $pFile Filename 134 | * @return string Mime Type 135 | * @throws Exception 136 | */ 137 | private function _getImageMimeType($pFile = '') { 138 | if(PHPWord_Shared_File::file_exists($pFile)) { 139 | $image = getimagesize($pFile); 140 | return image_type_to_mime_type($image[2]); 141 | } else { 142 | throw new Exception("File $pFile does not exist"); 143 | } 144 | } 145 | 146 | /** 147 | * Write Default content type 148 | * 149 | * @param PHPWord_Shared_XMLWriter $objWriter XML Writer 150 | * @param string $pPartname Part name 151 | * @param string $pContentType Content type 152 | * @throws Exception 153 | */ 154 | private function _writeDefaultContentType(PHPWord_Shared_XMLWriter $objWriter = null, $pPartname = '', $pContentType = '') { 155 | if($pPartname != '' && $pContentType != '') { 156 | // Write content type 157 | $objWriter->startElement('Default'); 158 | $objWriter->writeAttribute('Extension', $pPartname); 159 | $objWriter->writeAttribute('ContentType', $pContentType); 160 | $objWriter->endElement(); 161 | } else { 162 | throw new Exception("Invalid parameters passed."); 163 | } 164 | } 165 | 166 | /** 167 | * Write Override content type 168 | * 169 | * @param PHPPowerPoint_Shared_XMLWriter $objWriter XML Writer 170 | * @param string $pPartname Part name 171 | * @param string $pContentType Content type 172 | * @throws Exception 173 | */ 174 | private function _writeOverrideContentType(PHPWord_Shared_XMLWriter $objWriter = null, $pPartname = '', $pContentType = '') { 175 | if($pPartname != '' && $pContentType != '') { 176 | // Write content type 177 | $objWriter->startElement('Override'); 178 | $objWriter->writeAttribute('PartName', $pPartname); 179 | $objWriter->writeAttribute('ContentType', $pContentType); 180 | $objWriter->endElement(); 181 | } else { 182 | throw new Exception("Invalid parameters passed."); 183 | } 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /PHPWord/Writer/Word2007/DocProps.php: -------------------------------------------------------------------------------- 1 | getParentWriter()->getUseDiskCaching()) { 35 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); 36 | } else { 37 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY); 38 | } 39 | 40 | // XML header 41 | $objWriter->startDocument('1.0','UTF-8','yes'); 42 | 43 | // Properties 44 | $objWriter->startElement('Properties'); 45 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties'); 46 | $objWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes'); 47 | 48 | // Application 49 | $objWriter->writeElement('Application', 'Microsoft Office Word'); 50 | 51 | // ScaleCrop 52 | $objWriter->writeElement('ScaleCrop', 'false'); 53 | 54 | // HeadingPairs 55 | $objWriter->startElement('HeadingPairs'); 56 | 57 | // Vector 58 | $objWriter->startElement('vt:vector'); 59 | $objWriter->writeAttribute('size', '4'); 60 | $objWriter->writeAttribute('baseType', 'variant'); 61 | 62 | // Variant 63 | $objWriter->startElement('vt:variant'); 64 | $objWriter->writeElement('vt:lpstr', 'Theme'); 65 | $objWriter->endElement(); 66 | 67 | // Variant 68 | $objWriter->startElement('vt:variant'); 69 | $objWriter->writeElement('vt:i4', '1'); 70 | $objWriter->endElement(); 71 | 72 | // Variant 73 | $objWriter->startElement('vt:variant'); 74 | $objWriter->writeElement('vt:lpstr', 'Slide Titles'); 75 | $objWriter->endElement(); 76 | 77 | // Variant 78 | $objWriter->startElement('vt:variant'); 79 | $objWriter->writeElement('vt:i4', '1'); 80 | $objWriter->endElement(); 81 | 82 | $objWriter->endElement(); 83 | 84 | $objWriter->endElement(); 85 | 86 | // TitlesOfParts 87 | $objWriter->startElement('TitlesOfParts'); 88 | 89 | // Vector 90 | $objWriter->startElement('vt:vector'); 91 | $objWriter->writeAttribute('size', '1'); 92 | $objWriter->writeAttribute('baseType', 'lpstr'); 93 | 94 | $objWriter->writeElement('vt:lpstr', 'Office Theme'); 95 | 96 | $objWriter->endElement(); 97 | 98 | $objWriter->endElement(); 99 | 100 | // Company 101 | $objWriter->writeElement('Company', $pPHPWord->getProperties()->getCompany()); 102 | 103 | // LinksUpToDate 104 | $objWriter->writeElement('LinksUpToDate', 'false'); 105 | 106 | // SharedDoc 107 | $objWriter->writeElement('SharedDoc', 'false'); 108 | 109 | // HyperlinksChanged 110 | $objWriter->writeElement('HyperlinksChanged', 'false'); 111 | 112 | // AppVersion 113 | $objWriter->writeElement('AppVersion', '12.0000'); 114 | 115 | $objWriter->endElement(); 116 | 117 | // Return 118 | return $objWriter->getData(); 119 | } 120 | 121 | 122 | public function writeDocPropsCore(PHPWord $pPHPWord = null) { 123 | // Create XML writer 124 | $objWriter = null; 125 | if ($this->getParentWriter()->getUseDiskCaching()) { 126 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); 127 | } else { 128 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY); 129 | } 130 | 131 | // XML header 132 | $objWriter->startDocument('1.0','UTF-8','yes'); 133 | 134 | // cp:coreProperties 135 | $objWriter->startElement('cp:coreProperties'); 136 | $objWriter->writeAttribute('xmlns:cp', 'http://schemas.openxmlformats.org/package/2006/metadata/core-properties'); 137 | $objWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); 138 | $objWriter->writeAttribute('xmlns:dcterms', 'http://purl.org/dc/terms/'); 139 | $objWriter->writeAttribute('xmlns:dcmitype', 'http://purl.org/dc/dcmitype/'); 140 | $objWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); 141 | 142 | // dc:creator 143 | $objWriter->writeElement('dc:creator', $pPHPWord->getProperties()->getCreator()); 144 | 145 | // cp:lastModifiedBy 146 | $objWriter->writeElement('cp:lastModifiedBy', $pPHPWord->getProperties()->getLastModifiedBy()); 147 | 148 | // dcterms:created 149 | $objWriter->startElement('dcterms:created'); 150 | $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF'); 151 | $objWriter->writeRaw(date(DATE_W3C, $pPHPWord->getProperties()->getCreated())); 152 | $objWriter->endElement(); 153 | 154 | // dcterms:modified 155 | $objWriter->startElement('dcterms:modified'); 156 | $objWriter->writeAttribute('xsi:type', 'dcterms:W3CDTF'); 157 | $objWriter->writeRaw(date(DATE_W3C, $pPHPWord->getProperties()->getModified())); 158 | $objWriter->endElement(); 159 | 160 | // dc:title 161 | $objWriter->writeElement('dc:title', $pPHPWord->getProperties()->getTitle()); 162 | 163 | // dc:description 164 | $objWriter->writeElement('dc:description', $pPHPWord->getProperties()->getDescription()); 165 | 166 | // dc:subject 167 | $objWriter->writeElement('dc:subject', $pPHPWord->getProperties()->getSubject()); 168 | 169 | // cp:keywords 170 | $objWriter->writeElement('cp:keywords', $pPHPWord->getProperties()->getKeywords()); 171 | 172 | // cp:category 173 | $objWriter->writeElement('cp:category', $pPHPWord->getProperties()->getCategory()); 174 | 175 | $objWriter->endElement(); 176 | 177 | // Return 178 | return $objWriter->getData(); 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /PHPWord/Writer/Word2007/DocumentRels.php: -------------------------------------------------------------------------------- 1 | getParentWriter()->getUseDiskCaching()) { 35 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); 36 | } else { 37 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY); 38 | } 39 | 40 | // XML header 41 | $objWriter->startDocument('1.0','UTF-8','yes'); 42 | 43 | // Relationships 44 | $objWriter->startElement('Relationships'); 45 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); 46 | 47 | // Relationship word/document.xml 48 | $this->_writeRelationship( 49 | $objWriter, 50 | 1, 51 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles', 52 | 'styles.xml' 53 | ); 54 | 55 | // Relationship word/numbering.xml 56 | $this->_writeRelationship( 57 | $objWriter, 58 | 2, 59 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering', 60 | 'numbering.xml' 61 | ); 62 | 63 | // Relationship word/settings.xml 64 | $this->_writeRelationship( 65 | $objWriter, 66 | 3, 67 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings', 68 | 'settings.xml' 69 | ); 70 | 71 | // Relationship word/settings.xml 72 | $this->_writeRelationship( 73 | $objWriter, 74 | 4, 75 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', 76 | 'theme/theme1.xml' 77 | ); 78 | 79 | // Relationship word/settings.xml 80 | $this->_writeRelationship( 81 | $objWriter, 82 | 5, 83 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings', 84 | 'webSettings.xml' 85 | ); 86 | 87 | // Relationship word/settings.xml 88 | $this->_writeRelationship( 89 | $objWriter, 90 | 6, 91 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable', 92 | 'fontTable.xml' 93 | ); 94 | 95 | // Relationships to Images / Embeddings / Headers / Footers 96 | foreach($_relsCollection as $relation) { 97 | $relationType = $relation['type']; 98 | $relationName = $relation['target']; 99 | $relationId = $relation['rID']; 100 | $targetMode = ($relationType == 'hyperlink') ? 'External' : ''; 101 | 102 | $this->_writeRelationship( 103 | $objWriter, 104 | $relationId, 105 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/'.$relationType, 106 | $relationName, 107 | $targetMode 108 | ); 109 | } 110 | 111 | 112 | $objWriter->endElement(); 113 | 114 | // Return 115 | return $objWriter->getData(); 116 | } 117 | 118 | public function writeHeaderFooterRels($_relsCollection) { 119 | // Create XML writer 120 | $objWriter = null; 121 | if ($this->getParentWriter()->getUseDiskCaching()) { 122 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); 123 | } else { 124 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY); 125 | } 126 | 127 | // XML header 128 | $objWriter->startDocument('1.0','UTF-8','yes'); 129 | 130 | // Relationships 131 | $objWriter->startElement('Relationships'); 132 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); 133 | 134 | // Relationships to Images / Embeddings / Headers / Footers 135 | foreach($_relsCollection as $relation) { 136 | $relationType = $relation['type']; 137 | $relationName = $relation['target']; 138 | $relationId = $relation['rID']; 139 | 140 | $this->_writeRelationship( 141 | $objWriter, 142 | $relationId, 143 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/'.$relationType, 144 | $relationName 145 | ); 146 | } 147 | 148 | 149 | $objWriter->endElement(); 150 | 151 | // Return 152 | return $objWriter->getData(); 153 | } 154 | 155 | private function _writeRelationship(PHPWord_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '') { 156 | if($pType != '' && $pTarget != '') { 157 | if(strpos($pId, 'rId') === false) { 158 | $pId = 'rId' . $pId; 159 | } 160 | 161 | // Write relationship 162 | $objWriter->startElement('Relationship'); 163 | $objWriter->writeAttribute('Id', $pId); 164 | $objWriter->writeAttribute('Type', $pType); 165 | $objWriter->writeAttribute('Target', $pTarget); 166 | 167 | if($pTargetMode != '') { 168 | $objWriter->writeAttribute('TargetMode', $pTargetMode); 169 | } 170 | 171 | $objWriter->endElement(); 172 | } else { 173 | throw new Exception("Invalid parameters passed."); 174 | } 175 | } 176 | } 177 | ?> -------------------------------------------------------------------------------- /PHPWord/Writer/Word2007/Footer.php: -------------------------------------------------------------------------------- 1 | getParentWriter()->getUseDiskCaching()) { 35 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); 36 | } else { 37 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY); 38 | } 39 | 40 | // XML header 41 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); 42 | 43 | $objWriter->startElement('w:ftr'); 44 | $objWriter->writeAttribute('xmlns:ve','http://schemas.openxmlformats.org/markup-compatibility/2006'); 45 | $objWriter->writeAttribute('xmlns:o','urn:schemas-microsoft-com:office:office'); 46 | $objWriter->writeAttribute('xmlns:r','http://schemas.openxmlformats.org/officeDocument/2006/relationships'); 47 | $objWriter->writeAttribute('xmlns:m','http://schemas.openxmlformats.org/officeDocument/2006/math'); 48 | $objWriter->writeAttribute('xmlns:v','urn:schemas-microsoft-com:vml'); 49 | $objWriter->writeAttribute('xmlns:wp','http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'); 50 | $objWriter->writeAttribute('xmlns:w10','urn:schemas-microsoft-com:office:word'); 51 | $objWriter->writeAttribute('xmlns:w','http://schemas.openxmlformats.org/wordprocessingml/2006/main'); 52 | $objWriter->writeAttribute('xmlns:wne','http://schemas.microsoft.com/office/word/2006/wordml'); 53 | 54 | $_elements = $footer->getElements(); 55 | 56 | foreach($_elements as $element) { 57 | if($element instanceof PHPWord_Section_Text) { 58 | $this->_writeText($objWriter, $element); 59 | } elseif($element instanceof PHPWord_Section_TextRun) { 60 | $this->_writeTextRun($objWriter, $element); 61 | } elseif($element instanceof PHPWord_Section_TextBreak) { 62 | $this->_writeTextBreak($objWriter); 63 | } elseif($element instanceof PHPWord_Section_Table) { 64 | $this->_writeTable($objWriter, $element); 65 | } elseif($element instanceof PHPWord_Section_Image || 66 | $element instanceof PHPWord_Section_MemoryImage) { 67 | $this->_writeImage($objWriter, $element); 68 | } elseif($element instanceof PHPWord_Section_Footer_PreserveText) { 69 | $this->_writePreserveText($objWriter, $element); 70 | } 71 | } 72 | 73 | $objWriter->endElement(); 74 | 75 | // Return 76 | return $objWriter->getData(); 77 | } 78 | } 79 | ?> 80 | -------------------------------------------------------------------------------- /PHPWord/Writer/Word2007/Header.php: -------------------------------------------------------------------------------- 1 | getParentWriter()->getUseDiskCaching()) { 34 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); 35 | } else { 36 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY); 37 | } 38 | 39 | // XML header 40 | $objWriter->startDocument('1.0', 'UTF-8', 'yes'); 41 | 42 | $objWriter->startElement('w:hdr'); 43 | $objWriter->writeAttribute('xmlns:ve','http://schemas.openxmlformats.org/markup-compatibility/2006'); 44 | $objWriter->writeAttribute('xmlns:o','urn:schemas-microsoft-com:office:office'); 45 | $objWriter->writeAttribute('xmlns:r','http://schemas.openxmlformats.org/officeDocument/2006/relationships'); 46 | $objWriter->writeAttribute('xmlns:m','http://schemas.openxmlformats.org/officeDocument/2006/math'); 47 | $objWriter->writeAttribute('xmlns:v','urn:schemas-microsoft-com:vml'); 48 | $objWriter->writeAttribute('xmlns:wp','http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'); 49 | $objWriter->writeAttribute('xmlns:w10','urn:schemas-microsoft-com:office:word'); 50 | $objWriter->writeAttribute('xmlns:w','http://schemas.openxmlformats.org/wordprocessingml/2006/main'); 51 | $objWriter->writeAttribute('xmlns:wne','http://schemas.microsoft.com/office/word/2006/wordml'); 52 | 53 | 54 | $_elements = $header->getElements(); 55 | 56 | foreach($_elements as $element) { 57 | if($element instanceof PHPWord_Section_Text) { 58 | $this->_writeText($objWriter, $element); 59 | } elseif($element instanceof PHPWord_Section_TextRun) { 60 | $this->_writeTextRun($objWriter, $element); 61 | } elseif($element instanceof PHPWord_Section_TextBreak) { 62 | $this->_writeTextBreak($objWriter); 63 | } elseif($element instanceof PHPWord_Section_Table) { 64 | $this->_writeTable($objWriter, $element); 65 | } elseif($element instanceof PHPWord_Section_Image || 66 | $element instanceof PHPWord_Section_MemoryImage) { 67 | if(!$element->getIsWatermark()) { 68 | $this->_writeImage($objWriter, $element); 69 | } else { 70 | $this->_writeWatermark($objWriter, $element); 71 | } 72 | } elseif($element instanceof PHPWord_Section_Footer_PreserveText) { 73 | $this->_writePreserveText($objWriter, $element); 74 | } 75 | } 76 | 77 | $objWriter->endElement(); 78 | 79 | // Return 80 | return $objWriter->getData(); 81 | } 82 | } 83 | ?> 84 | -------------------------------------------------------------------------------- /PHPWord/Writer/Word2007/Rels.php: -------------------------------------------------------------------------------- 1 | getParentWriter()->getUseDiskCaching()) { 35 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); 36 | } else { 37 | $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY); 38 | } 39 | 40 | // XML header 41 | $objWriter->startDocument('1.0','UTF-8','yes'); 42 | 43 | // Relationships 44 | $objWriter->startElement('Relationships'); 45 | $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); 46 | 47 | $relationId = 1; 48 | 49 | // Relationship word/document.xml 50 | $this->_writeRelationship( 51 | $objWriter, 52 | $relationId, 53 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument', 54 | 'word/document.xml' 55 | ); 56 | 57 | // Relationship docProps/core.xml 58 | $this->_writeRelationship( 59 | $objWriter, 60 | ++$relationId, 61 | 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties', 62 | 'docProps/core.xml' 63 | ); 64 | 65 | // Relationship docProps/app.xml 66 | $this->_writeRelationship( 67 | $objWriter, 68 | ++$relationId, 69 | 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties', 70 | 'docProps/app.xml' 71 | ); 72 | 73 | $objWriter->endElement(); 74 | 75 | // Return 76 | return $objWriter->getData(); 77 | } 78 | 79 | /** 80 | * Write Override content type 81 | * 82 | * @param PHPWord_Shared_XMLWriter $objWriter XML Writer 83 | * @param int $pId Relationship ID. rId will be prepended! 84 | * @param string $pType Relationship type 85 | * @param string $pTarget Relationship target 86 | * @param string $pTargetMode Relationship target mode 87 | * @throws Exception 88 | */ 89 | private function _writeRelationship(PHPWord_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '') { 90 | if($pType != '' && $pTarget != '') { 91 | if(strpos($pId, 'rId') === false) { 92 | $pId = 'rId' . $pId; 93 | } 94 | 95 | // Write relationship 96 | $objWriter->startElement('Relationship'); 97 | $objWriter->writeAttribute('Id', $pId); 98 | $objWriter->writeAttribute('Type', $pType); 99 | $objWriter->writeAttribute('Target', $pTarget); 100 | 101 | if($pTargetMode != '') { 102 | $objWriter->writeAttribute('TargetMode', $pTargetMode); 103 | } 104 | 105 | $objWriter->endElement(); 106 | } else { 107 | throw new Exception("Invalid parameters passed."); 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /PHPWord/Writer/Word2007/WriterPart.php: -------------------------------------------------------------------------------- 1 | _parentWriter = $pWriter; 34 | } 35 | 36 | public function getParentWriter() { 37 | if (!is_null($this->_parentWriter)) { 38 | return $this->_parentWriter; 39 | } else { 40 | throw new Exception("No parent PHPWord_Writer_IWriter assigned."); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /PHPWord/_staticDocParts/_doc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guiguidoc/laravel-phpword/e1bf6d8e34bc6b525dc0dac62b3ac1f3d8831583/PHPWord/_staticDocParts/_doc.png -------------------------------------------------------------------------------- /PHPWord/_staticDocParts/_ppt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guiguidoc/laravel-phpword/e1bf6d8e34bc6b525dc0dac62b3ac1f3d8831583/PHPWord/_staticDocParts/_ppt.png -------------------------------------------------------------------------------- /PHPWord/_staticDocParts/_xls.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/guiguidoc/laravel-phpword/e1bf6d8e34bc6b525dc0dac62b3ac1f3d8831583/PHPWord/_staticDocParts/_xls.png -------------------------------------------------------------------------------- /PHPWord/_staticDocParts/fontTable.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /PHPWord/_staticDocParts/settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /PHPWord/_staticDocParts/theme1.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /PHPWord/_staticDocParts/webSettings.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel PHPWord Bundle 2 | 3 | --- 4 | 5 | PHPWord is a library written in PHP that create word documents. 6 | No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be opened by all major office software. 7 | 8 | --- 9 | 10 | ## Installation 11 | 12 | Run this command on the CLI: 13 | 14 | php artisan bundle:install laravel-phpword 15 | 16 | ### Registering the Bundle 17 | 18 | Place the following code in ``application/bundles.php``: 19 | 20 | 21 | 'laravel-phpword' => array( 22 | 'auto' => true 23 | ) 24 | 25 | 26 | ### Usage #### 27 | 28 | $objPHPWord = new PHPWord(); 29 | $section = $objPHPWord->createSection(); 30 | $section->addText('Test PhpWord'); 31 | $writer = PHPWord_IOFactory::createWriter($objPHPWord, 'Word2007');; 32 | $writer->save("testword.docx"); 33 | return Response::download('testword.docx', 'testword.docx'); 34 | 35 | 36 | More info about PHPWord , visit : http://phpword.codeplex.com/ 37 | -------------------------------------------------------------------------------- /start.php: -------------------------------------------------------------------------------- 1 | __DIR__.DS.'PHPWord'.EXT, 30 | )); 31 | --------------------------------------------------------------------------------