Overview

Packages

  • PHP
  • PHPWord

Classes

  • PHPWord
  • PHPWord_Autoloader
  • PHPWord_DocumentProperties
  • PHPWord_HashTable
  • PHPWord_IOFactory
  • PHPWord_Media
  • PHPWord_Section
  • PHPWord_Section_Footer
  • PHPWord_Section_Footer_PreserveText
  • PHPWord_Section_Header
  • PHPWord_Section_Image
  • PHPWord_Section_Link
  • PHPWord_Section_ListItem
  • PHPWord_Section_MemoryImage
  • PHPWord_Section_Object
  • PHPWord_Section_PageBreak
  • PHPWord_Section_Settings
  • PHPWord_Section_Table
  • PHPWord_Section_Table_Cell
  • PHPWord_Section_Table_Row
  • PHPWord_Section_Text
  • PHPWord_Section_TextBreak
  • PHPWord_Section_TextRun
  • PHPWord_Section_Title
  • PHPWord_Shared_Drawing
  • PHPWord_Shared_File
  • PHPWord_Shared_Font
  • PHPWord_Shared_String
  • PHPWord_Shared_XMLWriter
  • PHPWord_Shared_ZipStreamWrapper
  • PHPWord_Style
  • PHPWord_Style_Cell
  • PHPWord_Style_Font
  • PHPWord_Style_Image
  • PHPWord_Style_ListItem
  • PHPWord_Style_Paragraph
  • PHPWord_Style_Row
  • PHPWord_Style_Tab
  • PHPWord_Style_Table
  • PHPWord_Style_TableFull
  • PHPWord_Style_Tabs
  • PHPWord_Style_TOC
  • PHPWord_Template
  • PHPWord_TOC
  • PHPWord_Writer_ODText
  • PHPWord_Writer_ODText_Content
  • PHPWord_Writer_ODText_Manifest
  • PHPWord_Writer_ODText_Meta
  • PHPWord_Writer_ODText_Mimetype
  • PHPWord_Writer_ODText_Styles
  • PHPWord_Writer_ODText_WriterPart
  • PHPWord_Writer_RTF
  • PHPWord_Writer_Word2007
  • PHPWord_Writer_Word2007_Base
  • PHPWord_Writer_Word2007_ContentTypes
  • PHPWord_Writer_Word2007_DocProps
  • PHPWord_Writer_Word2007_Document
  • PHPWord_Writer_Word2007_DocumentRels
  • PHPWord_Writer_Word2007_Footer
  • PHPWord_Writer_Word2007_Header
  • PHPWord_Writer_Word2007_Rels
  • PHPWord_Writer_Word2007_Styles
  • PHPWord_Writer_Word2007_WriterPart

Interfaces

  • PHPWord_Writer_IWriter

Exceptions

  • PHPWord_Exception
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * PHPWord
  4:  *
  5:  * Copyright (c) 2013 PHPWord
  6:  *
  7:  * This library is free software; you can redistribute it and/or
  8:  * modify it under the terms of the GNU Lesser General Public
  9:  * License as published by the Free Software Foundation; either
 10:  * version 2.1 of the License, or (at your option) any later version.
 11:  *
 12:  * This library is distributed in the hope that it will be useful,
 13:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15:  * Lesser General Public License for more details.
 16:  *
 17:  * You should have received a copy of the GNU Lesser General Public
 18:  * License along with this library; if not, write to the Free Software
 19:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 20:  *
 21:  * @category   PHPWord
 22:  * @package    PHPWord
 23:  * @copyright  Copyright (c) 2013 PHPWord
 24:  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 25:  * @version    0.7.0
 26:  */
 27: 
 28: /**
 29:  * Class PHPWord_Writer_RTF
 30:  */
 31: class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
 32: {
 33:     /**
 34:      * Private PHPWord
 35:      *
 36:      * @var PHPWord
 37:      */
 38:     private $_document;
 39: 
 40:     /**
 41:      * Private unique PHPWord_Worksheet_BaseDrawing HashTable
 42:      *
 43:      * @var PHPWord_HashTable
 44:      */
 45:     private $_drawingHashTable;
 46: 
 47:     private $_colorTable;
 48:     private $_fontTable;
 49:     private $_lastParagraphStyle;
 50: 
 51:     /**
 52:      * Create a new PHPWord_Writer_ODText
 53:      *
 54:      * @param    PHPWord $pPHPWord
 55:      */
 56:     public function __construct(PHPWord $pPHPWord = null)
 57:     {
 58:         // Assign PHPWord
 59:         $this->setPHPWord($pPHPWord);
 60: 
 61:         // Set HashTable variables
 62:         $this->_drawingHashTable = new PHPWord_HashTable();
 63:     }
 64: 
 65:     /**
 66:      * Save PHPWord to file
 67:      *
 68:      * @param    string $pFileName
 69:      * @throws    Exception
 70:      */
 71:     public function save($pFilename = null)
 72:     {
 73:         if (!is_null($this->_document)) {
 74:             // If $pFilename is php://output or php://stdout, make it a temporary file...
 75:             $originalFilename = $pFilename;
 76:             if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
 77:                 $pFilename = @tempnam('./', 'phppttmp');
 78:                 if ($pFilename == '') {
 79:                     $pFilename = $originalFilename;
 80:                 }
 81:             }
 82: 
 83:             $hFile = fopen($pFilename, 'w') or die("can't open file");
 84:             fwrite($hFile, $this->_getData());
 85:             fclose($hFile);
 86: 
 87:             // If a temporary file was used, copy it to the correct file stream
 88:             if ($originalFilename != $pFilename) {
 89:                 if (copy($pFilename, $originalFilename) === false) {
 90:                     throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
 91:                 }
 92:                 @unlink($pFilename);
 93:             }
 94: 
 95:         } else {
 96:             throw new Exception("PHPWord object unassigned.");
 97:         }
 98:     }
 99: 
100:     /**
101:      * Get PHPWord object
102:      *
103:      * @return PHPWord
104:      * @throws Exception
105:      */
106:     public function getPHPWord()
107:     {
108:         if (!is_null($this->_document)) {
109:             return $this->_document;
110:         } else {
111:             throw new Exception("No PHPWord assigned.");
112:         }
113:     }
114: 
115:     /**
116:      * Get PHPWord object
117:      *
118:      * @param    PHPWord $pPHPWord PHPWord object
119:      * @throws    Exception
120:      * @return PHPWord_Writer_PowerPoint2007
121:      */
122:     public function setPHPWord(PHPWord $pPHPWord = null)
123:     {
124:         $this->_document = $pPHPWord;
125:         return $this;
126:     }
127: 
128:     /**
129:      * Get PHPWord_Worksheet_BaseDrawing HashTable
130:      *
131:      * @return PHPWord_HashTable
132:      */
133:     public function getDrawingHashTable()
134:     {
135:         return $this->_drawingHashTable;
136:     }
137: 
138:     private function _getData()
139:     {
140:         // PHPWord object : $this->_document
141:         $this->_fontTable = $this->_getDataFont();
142:         $this->_colorTable = $this->_getDataColor();
143: 
144:         $sRTFContent = '{\rtf1';
145:         // Set the default character set
146:         $sRTFContent .= '\ansi\ansicpg1252';
147:         // Set the default font (the first one)
148:         $sRTFContent .= '\deff0';
149:         // Set the default tab size (720 twips)
150:         $sRTFContent .= '\deftab720';
151:         // Set the font tbl group
152:         $sRTFContent .= '{\fonttbl';
153:         foreach ($this->_fontTable as $idx => $font) {
154:             $sRTFContent .= '{\f' . $idx . '\fnil\fcharset0 ' . $font . ';}';
155:         }
156:         $sRTFContent .= '}' . PHP_EOL;
157:         // Set the color tbl group
158:         $sRTFContent .= '{\colortbl ';
159:         foreach ($this->_colorTable as $idx => $color) {
160:             $arrColor = PHPWord_Shared_Drawing::htmlToRGB($color);
161:             $sRTFContent .= ';\red' . $arrColor[0] . '\green' . $arrColor[1] . '\blue' . $arrColor[2] . '';
162:         }
163:         $sRTFContent .= ';}' . PHP_EOL;
164:         // Set the generator
165:         $sRTFContent .= '{\*\generator PHPWord;}';
166:         // Set the view mode of the document
167:         $sRTFContent .= '\viewkind4';
168:         // Set the numberof bytes that follows a unicode character
169:         $sRTFContent .= '\uc1';
170:         // Resets to default paragraph properties.
171:         $sRTFContent .= '\pard';
172:         // No widow/orphan control
173:         $sRTFContent .= '\nowidctlpar';
174:         // Applies a language to a text run (1036 : French (France))
175:         $sRTFContent .= '\lang1036';
176:         // Point size (in half-points) above which to kern character pairs
177:         $sRTFContent .= '\kerning1';
178:         // Set the font size in half-points
179:         $sRTFContent .= '\fs20';
180:         // Body
181:         $sRTFContent .= $this->_getDataContent();
182: 
183: 
184:         $sRTFContent .= '}';
185: 
186:         return $sRTFContent;
187:     }
188: 
189:     private function _getDataFont()
190:     {
191:         $pPHPWord = $this->_document;
192: 
193:         $arrFonts = array();
194:         // Default font : PHPWord::DEFAULT_FONT_NAME
195:         $arrFonts[] = PHPWord::DEFAULT_FONT_NAME;
196:         // PHPWord object : $this->_document
197: 
198:         // Browse styles
199:         $styles = PHPWord_Style::getStyles();
200:         $numPStyles = 0;
201:         if (count($styles) > 0) {
202:             foreach ($styles as $styleName => $style) {
203:                 // PHPWord_Style_Font
204:                 if ($style instanceof PHPWord_Style_Font) {
205:                     if (in_array($style->getName(), $arrFonts) == FALSE) {
206:                         $arrFonts[] = $style->getName();
207:                     }
208:                 }
209:             }
210:         }
211: 
212:         // Search all fonts used
213:         $_sections = $pPHPWord->getSections();
214:         $countSections = count($_sections);
215:         if ($countSections > 0) {
216:             $pSection = 0;
217: 
218:             foreach ($_sections as $section) {
219:                 $pSection++;
220:                 $_elements = $section->getElements();
221: 
222:                 foreach ($_elements as $element) {
223:                     if ($element instanceof PHPWord_Section_Text) {
224:                         $fStyle = $element->getFontStyle();
225: 
226:                         if ($fStyle instanceof PHPWord_Style_Font) {
227:                             if (in_array($fStyle->getName(), $arrFonts) == FALSE) {
228:                                 $arrFonts[] = $fStyle->getName();
229:                             }
230:                         }
231:                     }
232:                 }
233:             }
234:         }
235: 
236:         return $arrFonts;
237:     }
238: 
239:     private function _getDataColor()
240:     {
241:         $pPHPWord = $this->_document;
242: 
243:         $arrColors = array();
244:         // PHPWord object : $this->_document
245: 
246:         // Browse styles
247:         $styles = PHPWord_Style::getStyles();
248:         $numPStyles = 0;
249:         if (count($styles) > 0) {
250:             foreach ($styles as $styleName => $style) {
251:                 // PHPWord_Style_Font
252:                 if ($style instanceof PHPWord_Style_Font) {
253:                     $color = $style->getColor();
254:                     $fgcolor = $style->getFgColor();
255:                     if (in_array($color, $arrColors) == FALSE && $color != '000000' && !empty($color)) {
256:                         $arrColors[] = $color;
257:                     }
258:                     if (in_array($fgcolor, $arrColors) == FALSE && $fgcolor != '000000' && !empty($fgcolor)) {
259:                         $arrColors[] = $fgcolor;
260:                     }
261:                 }
262:             }
263:         }
264: 
265:         // Search all fonts used
266:         $_sections = $pPHPWord->getSections();
267:         $countSections = count($_sections);
268:         if ($countSections > 0) {
269:             $pSection = 0;
270: 
271:             foreach ($_sections as $section) {
272:                 $pSection++;
273:                 $_elements = $section->getElements();
274: 
275:                 foreach ($_elements as $element) {
276:                     if ($element instanceof PHPWord_Section_Text) {
277:                         $fStyle = $element->getFontStyle();
278: 
279:                         if ($fStyle instanceof PHPWord_Style_Font) {
280:                             if (in_array($fStyle->getColor(), $arrColors) == FALSE) {
281:                                 $arrColors[] = $fStyle->getColor();
282:                             }
283:                             if (in_array($fStyle->getFgColor(), $arrColors) == FALSE) {
284:                                 $arrColors[] = $fStyle->getFgColor();
285:                             }
286:                         }
287:                     }
288:                 }
289:             }
290:         }
291: 
292:         return $arrColors;
293:     }
294: 
295:     private function _getDataContent()
296:     {
297:         $pPHPWord = $this->_document;
298:         $sRTFBody = '';
299: 
300:         $_sections = $pPHPWord->getSections();
301:         $countSections = count($_sections);
302:         $pSection = 0;
303: 
304:         if ($countSections > 0) {
305:             foreach ($_sections as $section) {
306:                 $pSection++;
307:                 $_elements = $section->getElements();
308:                 foreach ($_elements as $element) {
309:                     if ($element instanceof PHPWord_Section_Text) {
310:                         $sRTFBody .= $this->_getDataContent_writeText($element);
311:                     } /* elseif($element instanceof PHPWord_Section_TextRun) {
312:                     $this->_writeTextRun($objWriter, $element);
313:                     } elseif($element instanceof PHPWord_Section_Link) {
314:                     $this->_writeLink($objWriter, $element);
315:                     } elseif($element instanceof PHPWord_Section_Title) {
316:                     $this->_writeTitle($objWriter, $element);
317:                     }*/
318:                     elseif ($element instanceof PHPWord_Section_TextBreak) {
319:                         $sRTFBody .= $this->_getDataContent_writeTextBreak();
320:                     } /* elseif($element instanceof PHPWord_Section_PageBreak) {
321:                     $this->_writePageBreak($objWriter);
322:                     } elseif($element instanceof PHPWord_Section_Table) {
323:                     $this->_writeTable($objWriter, $element);
324:                     } elseif($element instanceof PHPWord_Section_ListItem) {
325:                     $this->_writeListItem($objWriter, $element);
326:                     } elseif($element instanceof PHPWord_Section_Image ||
327:                     $element instanceof PHPWord_Section_MemoryImage) {
328:                     $this->_writeImage($objWriter, $element);
329:                     } elseif($element instanceof PHPWord_Section_Object) {
330:                     $this->_writeObject($objWriter, $element);
331:                     } elseif($element instanceof PHPWord_TOC) {
332:                     $this->_writeTOC($objWriter);
333:                     }*/
334:                     else {
335:                         print_r($element);
336:                         echo '<br />';
337:                     }
338:                 }
339:             }
340:         }
341:         return $sRTFBody;
342:     }
343: 
344:     private function _getDataContent_writeText(PHPWord_Section_Text $text)
345:     {
346:         $sRTFText = '';
347: 
348:         $styleFont = $text->getFontStyle();
349:         $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false;
350:         if (!$SfIsObject) {
351:             $styleFont = PHPWord_Style::getStyle($styleFont);
352:         }
353: 
354:         $styleParagraph = $text->getParagraphStyle();
355:         $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false;
356:         if (!$SpIsObject) {
357:             $styleParagraph = PHPWord_Style::getStyle($styleParagraph);
358:         }
359: 
360:         if ($styleParagraph) {
361:             if ($this->_lastParagraphStyle != $text->getParagraphStyle()) {
362:                 $sRTFText .= '\pard\nowidctlpar';
363:                 if ($styleParagraph->getSpaceAfter() != null) {
364:                     $sRTFText .= '\sa' . $styleParagraph->getSpaceAfter();
365:                 }
366:                 if ($styleParagraph->getAlign() != null) {
367:                     if ($styleParagraph->getAlign() == 'center') {
368:                         $sRTFText .= '\qc';
369:                     }
370:                 }
371:                 $this->_lastParagraphStyle = $text->getParagraphStyle();
372:             } else {
373:                 $this->_lastParagraphStyle = '';
374:             }
375:         } else {
376:             $this->_lastParagraphStyle = '';
377:         }
378: 
379:         if ($styleFont) {
380:             if ($styleFont->getColor() != null) {
381:                 $idxColor = array_search($styleFont->getColor(), $this->_colorTable);
382:                 if ($idxColor !== FALSE) {
383:                     $sRTFText .= '\cf' . ($idxColor + 1);
384:                 }
385:             } else {
386:                 $sRTFText .= '\cf0';
387:             }
388:             if ($styleFont->getName() != null) {
389:                 $idxFont = array_search($styleFont->getName(), $this->_fontTable);
390:                 if ($idxFont !== FALSE) {
391:                     $sRTFText .= '\f' . $idxFont;
392:                 }
393:             } else {
394:                 $sRTFText .= '\f0';
395:             }
396:             if ($styleFont->getBold()) {
397:                 $sRTFText .= '\b';
398:             }
399:             if ($styleFont->getBold()) {
400:                 $sRTFText .= '\i';
401:             }
402:             if ($styleFont->getSize()) {
403:                 $sRTFText .= '\fs' . $styleFont->getSize();
404:             }
405:         }
406:         if ($this->_lastParagraphStyle != '' || $styleFont) {
407:             $sRTFText .= ' ';
408:         }
409:         $sRTFText .= $text->getText();
410: 
411:         if ($styleFont) {
412:             $sRTFText .= '\cf0';
413:             $sRTFText .= '\f0';
414: 
415:             if ($styleFont->getBold()) {
416:                 $sRTFText .= '\b0';
417:             }
418:             if ($styleFont->getItalic()) {
419:                 $sRTFText .= '\i0';
420:             }
421:             if ($styleFont->getSize()) {
422:                 $sRTFText .= '\fs20';
423:             }
424:         }
425: 
426:         $sRTFText .= '\par' . PHP_EOL;
427:         return $sRTFText;
428:     }
429: 
430:     private function _getDataContent_writeTextBreak()
431:     {
432:         $this->_lastParagraphStyle = '';
433: 
434:         return '\par' . PHP_EOL;
435:     }
436: 
437: 
438: }
PHPWord API Docs API documentation generated by ApiGen 2.8.0