/** * Add a by PHP created Image Element * * @param string $link * @param mixed $style * @return PHPWord_Section_MemoryImage */ public function addMemoryImage($link, $style = null) { require_once __DIR__ . '/MemoryImage.php'; $memoryImage = new Document_Word_Writer_Section_MemoryImage($link, $style); if (!is_null($memoryImage->getSource())) { $rID = Document_Word_Writer_Media::addFooterMediaElement($this->_footerCount, $link, $memoryImage); $memoryImage->setRelationId($rID); $this->_elementCollection[] = $memoryImage; return $memoryImage; } else { trigger_error('Unsupported image type.'); } }
/** * Add a by PHP created Image Element * * @param string $link * @param mixed $style * @return PHPWord_Section_MemoryImage */ public function addMemoryImage($link, $style = null) { $memoryImage = new Document_Word_Writer_Section_MemoryImage($link, $style); if (!is_null($memoryImage->getSource())) { $rID = Document_Word_Writer_Media::addSectionMediaElement($link, 'image', $memoryImage); $memoryImage->setRelationId($rID); $this->_elementCollection[] = $memoryImage; return $memoryImage; } else { trigger_error('Unsupported image type.'); } }
/** * Add a Link Element * * @param string $linkSrc * @param string $linkName * @param mixed $styleFont * @return PHPWord_Section_Link */ public function addLink($linkSrc, $linkName = null, $styleFont = null) { require_once __DIR__ . '/Link.php'; require_once __DIR__ . '/../Media.php'; // $text = @iconv("UTF-8", "UTF-8//IGNORE", $text); // $linkSrc = utf8_encode($linkSrc); if (!is_null($linkName)) { $linkName = @iconv("UTF-8", "UTF-8//IGNORE", $linkName); // $linkName = utf8_encode($linkName); } $link = new Document_Word_Writer_Section_Link($linkSrc, $linkName, $styleFont); $rID = Document_Word_Writer_Media::addSectionLinkElement($linkSrc); $link->setRelationId($rID); $this->_elementCollection[] = $link; return $link; }
public function save($pFilename = null) { if (!is_null($this->_document)) { // If $pFilename is php://output or php://stdout, make it a temporary file... $originalFilename = $pFilename; if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { $pFilename = @tempnam('./', 'phppttmp'); if ($pFilename == '') { $pFilename = $originalFilename; } } // 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."); } } require_once __DIR__ . '/../Media.php'; $sectionElements = array(); $_secElements = Document_Word_Writer_Media::getSectionMediaElements(); foreach ($_secElements as $element) { // loop through section media elements if ($element['type'] != 'hyperlink') { $this->_addFileToPackage($objZip, $element); } $sectionElements[] = $element; } $_hdrElements = Document_Word_Writer_Media::getHeaderMediaElements(); foreach ($_hdrElements as $_headerFile => $_hdrMedia) { // loop through headers if (count($_hdrMedia) > 0) { $objZip->addFromString('word/_rels/' . $_headerFile . '.xml.rels', $this->getWriterPart('documentrels')->writeHeaderFooterRels($_hdrMedia)); foreach ($_hdrMedia as $element) { // loop through header media elements $this->_addFileToPackage($objZip, $element); } } } $_ftrElements = Document_Word_Writer_Media::getFooterMediaElements(); foreach ($_ftrElements as $_footerFile => $_ftrMedia) { // loop through footers if (count($_ftrMedia) > 0) { $objZip->addFromString('word/_rels/' . $_footerFile . '.xml.rels', $this->getWriterPart('documentrels')->writeHeaderFooterRels($_ftrMedia)); foreach ($_ftrMedia as $element) { // loop through footers media elements $this->_addFileToPackage($objZip, $element); } } } $_cHdrs = 0; $_cFtrs = 0; $rID = Document_Word_Writer_Media::countSectionMediaElements() + 6; $_sections = $this->_document->getSections(); //echo '<PRE>';print_r($_sections);exit; foreach ($_sections as $section) { $_header = $section->getHeader(); if (!is_null($_header)) { $_cHdrs++; $_header->setRelationId(++$rID); $_headerCount = $_header->getHeaderCount(); $_headerFile = 'header' . $_headerCount . '.xml'; $sectionElements[] = array('target' => $_headerFile, 'type' => 'header', 'rID' => $rID); $objZip->addFromString('word/' . $_headerFile, $this->getWriterPart('header')->writeHeader($_header)); } $_footer = $section->getFooter(); if (!is_null($_footer)) { $_cFtrs++; $_footer->setRelationId(++$rID); $_footerCount = $_footer->getFooterCount(); $_footerFile = 'footer' . $_footerCount . '.xml'; $sectionElements[] = array('target' => $_footerFile, 'type' => 'footer', 'rID' => $rID); $objZip->addFromString('word/' . $_footerFile, $this->getWriterPart('footer')->writeFooter($_footer)); } } // build docx file // Write dynamic files $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('contenttypes')->writeContentTypes($this->_imageTypes, $this->_objectTypes, $_cHdrs, $_cFtrs)); $objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeRelationships($this->_document)); $objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->_document)); $objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($this->_document)); $objZip->addFromString('word/document.xml', $this->getWriterPart('document')->writeDocument($this->_document)); $objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('documentrels')->writeDocumentRels($sectionElements)); $objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->_document)); // Write static files $objZip->addFile(__DIR__ . '/../_staticDocParts/numbering.xml', 'word/numbering.xml'); $objZip->addFile(__DIR__ . '/../_staticDocParts/settings.xml', 'word/settings.xml'); $objZip->addFile(__DIR__ . '/../_staticDocParts/theme1.xml', 'word/theme/theme1.xml'); $objZip->addFile(__DIR__ . '/../_staticDocParts/webSettings.xml', 'word/webSettings.xml'); $objZip->addFile(__DIR__ . '/../_staticDocParts/fontTable.xml', 'word/fontTable.xml'); // Close file if ($objZip->close() === false) { throw new Exception("Could not close zip file {$pFilename}."); } // If a temporary file was used, copy it to the correct file stream if ($originalFilename != $pFilename) { if (copy($pFilename, $originalFilename) === false) { throw new Exception("Could not copy temporary zip file {$pFilename} to {$originalFilename}."); } @unlink($pFilename); } } else { throw new Exception("PHPWord object unassigned."); } }
/** * Add a OLE-Object Element * * @param string $src * @param mixed $style * @return PHPWord_Section_Object */ public function addObject($src, $style = null) { $object = new Document_Word_Writer_Section_Object($src, $style); if (!is_null($object->getSource())) { $inf = pathinfo($src); $ext = $inf['extension']; if (strlen($ext) == 4 && strtolower(substr($ext, -1)) == 'x') { $ext = substr($ext, 0, -1); } $iconSrc = Document_Word_Writer_BASE_PATH . 'PHPWord/_staticDocParts/'; if (!file_exists($iconSrc . '_' . $ext . '.png')) { $iconSrc = $iconSrc . '_default.png'; } else { $iconSrc .= '_' . $ext . '.png'; } $rIDimg = Document_Word_Writer_Media::addSectionMediaElement($iconSrc, 'image'); $data = Document_Word_Writer_Media::addSectionMediaElement($src, 'oleObject'); $rID = $data[0]; $objectId = $data[1]; $object->setRelationId($rID); $object->setObjectId($objectId); $object->setImageRelationId($rIDimg); $this->_elementCollection[] = $object; return $object; } else { trigger_error('Source does not exist or unsupported object type.'); } }
/** * Add a Watermark Element * * @param string $src * @param mixed $style * @return PHPWord_Section_Image */ public function addWatermark($src, $style = null) { $image = new Document_Word_Writer_Section_Image($src, $style, true); if (!is_null($image->getSource())) { $rID = Document_Word_Writer_Media::addHeaderMediaElement($this->_headerCount, $src); $image->setRelationId($rID); $this->_elementCollection[] = $image; return $image; } else { trigger_error('Src does not exist or invalid image type.', E_ERROR); } }