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_ODText
 30:  */
 31: class PHPWord_Writer_ODText implements PHPWord_Writer_IWriter
 32: {
 33:     /**
 34:      * Private PHPWord
 35:      *
 36:      * @var PHPWord
 37:      */
 38:     private $_document;
 39: 
 40:     /**
 41:      * Private writer parts
 42:      *
 43:      * @var PHPWord_Writer_ODText_WriterPart[]
 44:      */
 45:     private $_writerParts;
 46: 
 47:     /**
 48:      * Private unique PHPWord_Worksheet_BaseDrawing HashTable
 49:      *
 50:      * @var PHPWord_HashTable
 51:      */
 52:     private $_drawingHashTable;
 53: 
 54:     /**
 55:      * Use disk caching where possible?
 56:      *
 57:      * @var boolean
 58:      */
 59:     private $_useDiskCaching = false;
 60: 
 61:     /**
 62:      * Disk caching directory
 63:      *
 64:      * @var string
 65:      */
 66:     private $_diskCachingDirectory;
 67: 
 68:     /**
 69:      * Create a new PHPWord_Writer_ODText
 70:      *
 71:      * @param    PHPWord $pPHPWord
 72:      */
 73:     public function __construct(PHPWord $pPHPWord = null)
 74:     {
 75:         // Assign PHPWord
 76:         $this->setPHPWord($pPHPWord);
 77: 
 78:         // Set up disk caching location
 79:         $this->_diskCachingDirectory = './';
 80: 
 81:         // Initialise writer parts
 82:         $this->_writerParts['content'] = new PHPWord_Writer_ODText_Content();
 83:         $this->_writerParts['manifest'] = new PHPWord_Writer_ODText_Manifest();
 84:         $this->_writerParts['meta'] = new PHPWord_Writer_ODText_Meta();
 85:         $this->_writerParts['mimetype'] = new PHPWord_Writer_ODText_Mimetype();
 86:         $this->_writerParts['styles'] = new PHPWord_Writer_ODText_Styles();
 87: 
 88: 
 89:         // Assign parent IWriter
 90:         foreach ($this->_writerParts as $writer) {
 91:             $writer->setParentWriter($this);
 92:         }
 93: 
 94:         // Set HashTable variables
 95:         $this->_drawingHashTable = new PHPWord_HashTable();
 96:     }
 97: 
 98:     /**
 99:      * Save PHPWord to file
100:      *
101:      * @param    string $pFileName
102:      * @throws    Exception
103:      */
104:     public function save($pFilename = null)
105:     {
106:         if (!is_null($this->_document)) {
107:             // If $pFilename is php://output or php://stdout, make it a temporary file...
108:             $originalFilename = $pFilename;
109:             if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
110:                 $pFilename = @tempnam('./', 'phppttmp');
111:                 if ($pFilename == '') {
112:                     $pFilename = $originalFilename;
113:                 }
114:             }
115: 
116:             // Create drawing dictionary
117: 
118:             // Create new ZIP file and open it for writing
119:             $objZip = new ZipArchive();
120: 
121:             // Try opening the ZIP file
122:             if ($objZip->open($pFilename, ZIPARCHIVE::OVERWRITE) !== true) {
123:                 if ($objZip->open($pFilename, ZIPARCHIVE::CREATE) !== true) {
124:                     throw new Exception("Could not open " . $pFilename . " for writing.");
125:                 }
126:             }
127: 
128:             // Add mimetype to ZIP file
129:             //@todo Not in ZIPARCHIVE::CM_STORE mode
130:             $objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->writeMimetype($this->_document));
131: 
132:             // Add content.xml to ZIP file
133:             $objZip->addFromString('content.xml', $this->getWriterPart('content')->writeContent($this->_document));
134: 
135:             // Add meta.xml to ZIP file
136:             $objZip->addFromString('meta.xml', $this->getWriterPart('meta')->writeMeta($this->_document));
137: 
138:             // Add styles.xml to ZIP file
139:             $objZip->addFromString('styles.xml', $this->getWriterPart('styles')->writeStyles($this->_document));
140: 
141:             // Add META-INF/manifest.xml
142:             $objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest($this->_document));
143: 
144:             // Add media
145:             for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
146:                 if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_Drawing) {
147:                     $imageContents = null;
148:                     $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath();
149: 
150:                     if (strpos($imagePath, 'zip://') !== false) {
151:                         $imagePath = substr($imagePath, 6);
152:                         $imagePathSplitted = explode('#', $imagePath);
153: 
154:                         $imageZip = new ZipArchive();
155:                         $imageZip->open($imagePathSplitted[0]);
156:                         $imageContents = $imageZip->getFromName($imagePathSplitted[1]);
157:                         $imageZip->close();
158:                         unset($imageZip);
159:                     } else {
160:                         $imageContents = file_get_contents($imagePath);
161:                     }
162: 
163:                     $objZip->addFromString('Pictures/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
164:                 } else if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_MemoryDrawing) {
165:                     ob_start();
166:                     call_user_func(
167:                         $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(),
168:                         $this->getDrawingHashTable()->getByIndex($i)->getImageResource()
169:                     );
170:                     $imageContents = ob_get_contents();
171:                     ob_end_clean();
172: 
173:                     $objZip->addFromString('Pictures/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
174:                 }
175:             }
176: 
177:             // Close file
178:             if ($objZip->close() === false) {
179:                 throw new Exception("Could not close zip file $pFilename.");
180:             }
181: 
182:             // If a temporary file was used, copy it to the correct file stream
183:             if ($originalFilename != $pFilename) {
184:                 if (copy($pFilename, $originalFilename) === false) {
185:                     throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
186:                 }
187:                 @unlink($pFilename);
188:             }
189: 
190:         } else {
191:             throw new Exception("PHPWord object unassigned.");
192:         }
193:     }
194: 
195:     /**
196:      * Get PHPWord object
197:      *
198:      * @return PHPWord
199:      * @throws Exception
200:      */
201:     public function getPHPWord()
202:     {
203:         if (!is_null($this->_document)) {
204:             return $this->_document;
205:         } else {
206:             throw new Exception("No PHPWord assigned.");
207:         }
208:     }
209: 
210:     /**
211:      * Get PHPWord object
212:      *
213:      * @param    PHPWord $pPHPWord PHPWord object
214:      * @throws    Exception
215:      * @return PHPWord_Writer_PowerPoint2007
216:      */
217:     public function setPHPWord(PHPWord $pPHPWord = null)
218:     {
219:         $this->_document = $pPHPWord;
220:         return $this;
221:     }
222: 
223:     /**
224:      * Get PHPWord_Worksheet_BaseDrawing HashTable
225:      *
226:      * @return PHPWord_HashTable
227:      */
228:     public function getDrawingHashTable()
229:     {
230:         return $this->_drawingHashTable;
231:     }
232: 
233:     /**
234:      * Get writer part
235:      *
236:      * @param    string $pPartName Writer part name
237:      * @return    PHPWord_Writer_ODText_WriterPart
238:      */
239:     function getWriterPart($pPartName = '')
240:     {
241:         if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) {
242:             return $this->_writerParts[strtolower($pPartName)];
243:         } else {
244:             return null;
245:         }
246:     }
247: 
248:     /**
249:      * Get use disk caching where possible?
250:      *
251:      * @return boolean
252:      */
253:     public function getUseDiskCaching()
254:     {
255:         return $this->_useDiskCaching;
256:     }
257: 
258:     /**
259:      * Set use disk caching where possible?
260:      *
261:      * @param    boolean $pValue
262:      * @param    string $pDirectory Disk caching directory
263:      * @throws    Exception    Exception when directory does not exist
264:      * @return PHPWord_Writer_PowerPoint2007
265:      */
266:     public function setUseDiskCaching($pValue = false, $pDirectory = null)
267:     {
268:         $this->_useDiskCaching = $pValue;
269: 
270:         if (!is_null($pDirectory)) {
271:             if (is_dir($pDirectory)) {
272:                 $this->_diskCachingDirectory = $pDirectory;
273:             } else {
274:                 throw new Exception("Directory does not exist: $pDirectory");
275:             }
276:         }
277: 
278:         return $this;
279:     }
280: 
281:     /**
282:      * Get disk caching directory
283:      *
284:      * @return string
285:      */
286:     public function getDiskCachingDirectory()
287:     {
288:         return $this->_diskCachingDirectory;
289:     }
290: }
PHPWord API Docs API documentation generated by ApiGen 2.8.0