/** * Add section link */ public function testAddSectionLinkElement() { $expected = Media::countElements('section') + 1; $actual = Media::addElement('section', 'link', 'http://test.com'); $this->assertEquals($expected, $actual); $this->assertCount(1, Media::getElements('section', 'link')); }
/** * Save document by name * * @param string $filename */ public function save($filename = null) { if (!is_null($this->phpWord)) { $filename = $this->getTempFile($filename); $objZip = $this->getZipArchive($filename); // Content types $this->cTypes['default'] = array('rels' => 'application/vnd.openxmlformats-package.relationships+xml', 'xml' => 'application/xml'); // Add section media files $sectionMedia = Media::getElements('section'); if (!empty($sectionMedia)) { $this->addFilesToPackage($objZip, $sectionMedia); $this->registerContentTypes($sectionMedia); foreach ($sectionMedia as $element) { $this->docRels[] = $element; } } // Add header/footer media files & relations $this->addHeaderFooterMedia($objZip, 'header'); $this->addHeaderFooterMedia($objZip, 'footer'); // Add header/footer contents $rId = Media::countElements('section') + 6; // @see Rels::writeDocRels for 6 first elements $sections = $this->phpWord->getSections(); foreach ($sections as $section) { $this->addHeaderFooterContent($section, $objZip, 'header', $rId); $this->addHeaderFooterContent($section, $objZip, 'footer', $rId); } $this->addNotes($objZip, $rId, 'footnote'); $this->addNotes($objZip, $rId, 'endnote'); // Write parts $objZip->addFromString('[Content_Types].xml', $this->getWriterPart('contenttypes')->writeContentTypes($this->cTypes)); $objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeMainRels()); $objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->phpWord)); $objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($this->phpWord)); $objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('rels')->writeDocRels($this->docRels)); $objZip->addFromString('word/document.xml', $this->getWriterPart('document')->writeDocument($this->phpWord)); $objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->phpWord)); $objZip->addFromString('word/numbering.xml', $this->getWriterPart('numbering')->writeNumbering()); $objZip->addFromString('word/settings.xml', $this->getWriterPart('settings')->writeSettings()); $objZip->addFromString('word/webSettings.xml', $this->getWriterPart('websettings')->writeWebSettings()); $objZip->addFromString('word/fontTable.xml', $this->getWriterPart('fonttable')->write()); $objZip->addFromString('word/theme/theme1.xml', $this->getWriterPart('theme')->write()); // Close file if ($objZip->close() === false) { throw new Exception("Could not close zip file {$filename}."); } $this->cleanupTempFile(); } else { throw new Exception("PhpWord object unassigned."); } }
/** * Add footer media element and reset media */ public function testAddFooterMediaElement() { $local = __DIR__ . "/_files/images/mars.jpg"; $remote = 'http://php.net/images/logos/php-med-trans-light.gif'; Media::addElement('footer1', 'image', $local, new Image($local)); Media::addElement('footer1', 'image', $local, new Image($local)); Media::addElement('footer1', 'image', $remote, new Image($remote)); $this->assertEquals(2, Media::countElements('footer1')); Media::resetElements(); $this->assertEquals(0, Media::countElements('footer1')); }
/** * Save document by name. * * @param string $filename * @return void */ public function save($filename = null) { $filename = $this->getTempFile($filename); $zip = $this->getZipArchive($filename); $phpWord = $this->getPhpWord(); // Content types $this->contentTypes['default'] = array('rels' => 'application/vnd.openxmlformats-package.relationships+xml', 'xml' => 'application/xml'); // Add section media files $sectionMedia = Media::getElements('section'); if (!empty($sectionMedia)) { $this->addFilesToPackage($zip, $sectionMedia); $this->registerContentTypes($sectionMedia); foreach ($sectionMedia as $element) { $this->relationships[] = $element; } } // Add header/footer media files & relations $this->addHeaderFooterMedia($zip, 'header'); $this->addHeaderFooterMedia($zip, 'footer'); // Add header/footer contents $rId = Media::countElements('section') + 6; // @see Rels::writeDocRels for 6 first elements $sections = $phpWord->getSections(); foreach ($sections as $section) { $this->addHeaderFooterContent($section, $zip, 'header', $rId); $this->addHeaderFooterContent($section, $zip, 'footer', $rId); } $this->addNotes($zip, $rId, 'footnote'); $this->addNotes($zip, $rId, 'endnote'); $this->addChart($zip, $rId); // Write parts foreach ($this->parts as $partName => $fileName) { if ($fileName != '') { $zip->addFromString($fileName, $this->getWriterPart($partName)->write()); } } // Close zip archive and cleanup temp file $zip->close(); $this->cleanupTempFile(); }