1: <?php
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: class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter
32: {
33: 34: 35: 36: 37:
38: private $_document;
39:
40: 41: 42: 43: 44:
45: private $_drawingHashTable;
46:
47: private $_colorTable;
48: private $_fontTable;
49: private $_lastParagraphStyle;
50:
51: 52: 53: 54: 55:
56: public function __construct(PHPWord $pPHPWord = null)
57: {
58:
59: $this->setPHPWord($pPHPWord);
60:
61:
62: $this->_drawingHashTable = new PHPWord_HashTable();
63: }
64:
65: 66: 67: 68: 69: 70:
71: public function save($pFilename = null)
72: {
73: if (!is_null($this->_document)) {
74:
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:
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: 102: 103: 104: 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: 117: 118: 119: 120: 121:
122: public function setPHPWord(PHPWord $pPHPWord = null)
123: {
124: $this->_document = $pPHPWord;
125: return $this;
126: }
127:
128: 129: 130: 131: 132:
133: public function getDrawingHashTable()
134: {
135: return $this->_drawingHashTable;
136: }
137:
138: private function _getData()
139: {
140:
141: $this->_fontTable = $this->_getDataFont();
142: $this->_colorTable = $this->_getDataColor();
143:
144: $sRTFContent = '{\rtf1';
145:
146: $sRTFContent .= '\ansi\ansicpg1252';
147:
148: $sRTFContent .= '\deff0';
149:
150: $sRTFContent .= '\deftab720';
151:
152: $sRTFContent .= '{\fonttbl';
153: foreach ($this->_fontTable as $idx => $font) {
154: $sRTFContent .= '{\f' . $idx . '\fnil\fcharset0 ' . $font . ';}';
155: }
156: $sRTFContent .= '}' . PHP_EOL;
157:
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:
165: $sRTFContent .= '{\*\generator PHPWord;}';
166:
167: $sRTFContent .= '\viewkind4';
168:
169: $sRTFContent .= '\uc1';
170:
171: $sRTFContent .= '\pard';
172:
173: $sRTFContent .= '\nowidctlpar';
174:
175: $sRTFContent .= '\lang1036';
176:
177: $sRTFContent .= '\kerning1';
178:
179: $sRTFContent .= '\fs20';
180:
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:
195: $arrFonts[] = PHPWord::DEFAULT_FONT_NAME;
196:
197:
198:
199: $styles = PHPWord_Style::getStyles();
200: $numPStyles = 0;
201: if (count($styles) > 0) {
202: foreach ($styles as $styleName => $style) {
203:
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:
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:
245:
246:
247: $styles = PHPWord_Style::getStyles();
248: $numPStyles = 0;
249: if (count($styles) > 0) {
250: foreach ($styles as $styleName => $style) {
251:
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:
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: } 312: 313: 314: 315: 316: 317:
318: elseif ($element instanceof PHPWord_Section_TextBreak) {
319: $sRTFBody .= $this->_getDataContent_writeTextBreak();
320: } 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 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: }