setPhpWord($phpWord); // Set writer parts $this->writerParts['content'] = new Content(); $this->writerParts['manifest'] = new Manifest(); $this->writerParts['meta'] = new Meta(); $this->writerParts['mimetype'] = new Mimetype(); $this->writerParts['styles'] = new Styles(); foreach ($this->writerParts as $writer) { $writer->setParentWriter($this); } // Set HashTable variables $this->drawingHashTable = new HashTable(); } /** * Save PhpWord to file * * @param string $pFilename * @throws \PhpOffice\PhpWord\Exceptions\Exception */ public function save($pFilename = null) { if (!is_null($this->phpWord)) { $pFilename = $this->getTempFile($pFilename); // Create new ZIP file and open it for writing $objZip = new \ZipArchive(); // Try opening the ZIP file if ($objZip->open($pFilename, \ZipArchive::OVERWRITE) !== true) { if ($objZip->open($pFilename, \ZipArchive::CREATE) !== true) { throw new Exception("Could not open " . $pFilename . " for writing."); } } // Add mimetype to ZIP file //@todo Not in \ZipArchive::CM_STORE mode $objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->writeMimetype($this->phpWord)); // Add content.xml to ZIP file $objZip->addFromString('content.xml', $this->getWriterPart('content')->writeContent($this->phpWord)); // Add meta.xml to ZIP file $objZip->addFromString('meta.xml', $this->getWriterPart('meta')->writeMeta($this->phpWord)); // Add styles.xml to ZIP file $objZip->addFromString('styles.xml', $this->getWriterPart('styles')->writeStyles($this->phpWord)); // Add META-INF/manifest.xml $objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest($this->phpWord)); // Add media. Has not used yet. Legacy from PHPExcel. // @codeCoverageIgnoreStart for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) { if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_Drawing) { $imageContents = null; $imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath(); if (strpos($imagePath, 'zip://') !== false) { $imagePath = substr($imagePath, 6); $imagePathSplitted = explode('#', $imagePath); $imageZip = new \ZipArchive(); $imageZip->open($imagePathSplitted[0]); $imageContents = $imageZip->getFromName($imagePathSplitted[1]); $imageZip->close(); unset($imageZip); } else { $imageContents = file_get_contents($imagePath); } $objZip->addFromString('Pictures/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); } elseif ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_MemoryDrawing) { ob_start(); call_user_func( $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(), $this->getDrawingHashTable()->getByIndex($i)->getImageResource() ); $imageContents = ob_get_contents(); ob_end_clean(); $objZip->addFromString('Pictures/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); } } // @codeCoverageIgnoreEnd // Close file if ($objZip->close() === false) { throw new Exception("Could not close zip file $pFilename."); } $this->cleanupTempFile(); } else { throw new Exception("PhpWord object unassigned."); } } /** * Get PHPWord_Worksheet_BaseDrawing HashTable * * @return \PhpOffice\PhpWord\HashTable */ public function getDrawingHashTable() { return $this->drawingHashTable; } }