/** * Read meta.xml. * * @param \PhpOffice\PhpWord\PhpWord $phpWord * @return void * @todo Process property type */ public function read(PhpWord $phpWord) { $xmlReader = new XMLReader(); $xmlReader->getDomFromZip($this->docFile, $this->xmlFile); $docProps = $phpWord->getDocInfo(); $metaNode = $xmlReader->getElement('office:meta'); // Standard properties $properties = array('title' => 'dc:title', 'subject' => 'dc:subject', 'description' => 'dc:description', 'keywords' => 'meta:keyword', 'creator' => 'meta:initial-creator', 'lastModifiedBy' => 'dc:creator'); foreach ($properties as $property => $path) { $method = "set{$property}"; $propertyNode = $xmlReader->getElement($path, $metaNode); if ($propertyNode !== null && method_exists($docProps, $method)) { $docProps->{$method}($propertyNode->nodeValue); } } // Custom properties $propertyNodes = $xmlReader->getElements('meta:user-defined', $metaNode); foreach ($propertyNodes as $propertyNode) { $property = $xmlReader->getAttribute('meta:name', $propertyNode); // Set category, company, and manager property if (in_array($property, array('Category', 'Company', 'Manager'))) { $method = "set{$property}"; $docProps->{$method}($propertyNode->nodeValue); // Set other custom properties } else { $docProps->setCustomProperty($property, $propertyNode->nodeValue); } } }
/** * Test object creation */ public function testConstruct() { $phpWord = new PhpWord(); $this->assertEquals(new DocInfo(), $phpWord->getDocInfo()); $this->assertEquals(Settings::DEFAULT_FONT_NAME, $phpWord->getDefaultFontName()); $this->assertEquals(Settings::DEFAULT_FONT_SIZE, $phpWord->getDefaultFontSize()); }
/** * Read custom document properties. * * @param \PhpOffice\PhpWord\PhpWord $phpWord * @return void */ public function read(PhpWord $phpWord) { $xmlReader = new XMLReader(); $xmlReader->getDomFromZip($this->docFile, $this->xmlFile); $docProps = $phpWord->getDocInfo(); $nodes = $xmlReader->getElements('*'); if ($nodes->length > 0) { foreach ($nodes as $node) { $propertyName = $xmlReader->getAttribute('name', $node); $attributeNode = $xmlReader->getElement('*', $node); $attributeType = $attributeNode->nodeName; $attributeValue = $attributeNode->nodeValue; $attributeValue = DocInfo::convertProperty($attributeValue, $attributeType); $attributeType = DocInfo::convertPropertyType($attributeType); $docProps->setCustomProperty($propertyName, $attributeValue, $attributeType); } } }
/** * Test write content */ public function testWriteContent() { $imageSrc = __DIR__ . '/../../../_files/images/PhpWord.png'; $objectSrc = __DIR__ . '/../../../_files/documents/sheet.xls'; $expected = 'Expected'; $phpWord = new PhpWord(); $docProps = $phpWord->getDocInfo(); $docProps->setCustomProperty('Company', 'PHPWord'); $phpWord->setDefaultFontName('Verdana'); $phpWord->addFontStyle('Font', array('size' => 11)); $phpWord->addParagraphStyle('Paragraph', array('alignment' => Jc::CENTER)); $phpWord->addTableStyle('tblStyle', array('width' => 100)); $section = $phpWord->addSection(array('colsNum' => 2)); $section->addText(htmlspecialchars($expected, ENT_COMPAT, 'UTF-8')); $section->addText(htmlspecialchars('Test font style', ENT_COMPAT, 'UTF-8'), 'Font'); $section->addText(htmlspecialchars('Test paragraph style', ENT_COMPAT, 'UTF-8'), null, 'Paragraph'); $section->addLink('https://github.com/PHPOffice/PHPWord', htmlspecialchars('PHPWord on GitHub', ENT_COMPAT, 'UTF-8')); $section->addTitle(htmlspecialchars('Test title', ENT_COMPAT, 'UTF-8'), 1); $section->addTextBreak(); $section->addPageBreak(); $section->addListItem(htmlspecialchars('Test list item', ENT_COMPAT, 'UTF-8')); $section->addImage($imageSrc, array('width' => 50)); $section->addObject($objectSrc); $section->addTOC(); $textrun = $section->addTextRun(); $textrun->addText(htmlspecialchars('Test text run', ENT_COMPAT, 'UTF-8')); $table = $section->addTable(array('width' => 50)); $cell = $table->addRow()->addCell(); $cell = $table->addRow()->addCell(); $cell->addText(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8')); $cell->addLink('https://github.com/PHPOffice/PHPWord', htmlspecialchars('PHPWord on GitHub', ENT_COMPAT, 'UTF-8')); $cell->addTextBreak(); $cell->addListItem(htmlspecialchars('Test list item', ENT_COMPAT, 'UTF-8')); $cell->addImage($imageSrc); $cell->addObject($objectSrc); $textrun = $cell->addTextRun(); $textrun->addText(htmlspecialchars('Test text run', ENT_COMPAT, 'UTF-8')); $footer = $section->addFooter(); $footer->addPreserveText(htmlspecialchars('{PAGE}', ENT_COMPAT, 'UTF-8')); $table = $section->addTable('tblStyle')->addRow()->addCell(); $doc = TestHelperDOCX::getDocument($phpWord, 'ODText'); $element = '/office:document-content/office:body/office:text/text:section/text:p'; $this->assertEquals($expected, $doc->getElement($element, 'content.xml')->nodeValue); }
/** * Read core/extended document properties. * * @param \PhpOffice\PhpWord\PhpWord $phpWord * @return void */ public function read(PhpWord $phpWord) { $xmlReader = new XMLReader(); $xmlReader->getDomFromZip($this->docFile, $this->xmlFile); $docProps = $phpWord->getDocInfo(); $nodes = $xmlReader->getElements('*'); if ($nodes->length > 0) { foreach ($nodes as $node) { if (!isset($this->mapping[$node->nodeName])) { continue; } $method = $this->mapping[$node->nodeName]; $value = $node->nodeValue == '' ? null : $node->nodeValue; if (isset($this->callbacks[$node->nodeName])) { $value = $this->callbacks[$node->nodeName]($value); } if (method_exists($docProps, $method)) { $docProps->{$method}($value); } } } }
/** * Save */ public function testSave() { $localImage = __DIR__ . '/../_files/images/PhpWord.png'; $archiveImage = 'zip://' . __DIR__ . '/../_files/documents/reader.docx#word/media/image1.jpeg'; $gdImage = 'http://php.net/images/logos/php-med-trans-light.gif'; $objectSrc = __DIR__ . '/../_files/documents/sheet.xls'; $file = __DIR__ . '/../_files/temp.html'; $phpWord = new PhpWord(); $docProps = $phpWord->getDocInfo(); $docProps->setTitle(htmlspecialchars('HTML Test', ENT_COMPAT, 'UTF-8')); $phpWord->addTitleStyle(1, array('bold' => true)); $phpWord->addFontStyle('Font', array('name' => 'Verdana', 'size' => 11, 'color' => 'FF0000', 'fgColor' => 'FF0000')); $phpWord->addParagraphStyle('Paragraph', array('align' => 'center', 'spaceAfter' => 20, 'spaceBefore' => 20)); $section = $phpWord->addSection(); $section->addText(htmlspecialchars('Test 1', ENT_COMPAT, 'UTF-8'), 'Font', 'Paragraph'); $section->addTextBreak(); $section->addText(htmlspecialchars('Test 2', ENT_COMPAT, 'UTF-8'), array('name' => 'Tahoma', 'bold' => true, 'italic' => true, 'subscript' => true)); $section->addLink('https://github.com/PHPOffice/PHPWord'); $section->addTitle(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'), 1); $section->addPageBreak(); $section->addListItem(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8')); $section->addImage($localImage); $section->addImage($archiveImage); $section->addImage($gdImage); $section->addObject($objectSrc); $section->addFootnote(); $section->addEndnote(); $section = $phpWord->addSection(); $textrun = $section->addTextRun(array('align' => 'center')); $textrun->addText(htmlspecialchars('Test 3', ENT_COMPAT, 'UTF-8')); $textrun->addTextBreak(); $textrun = $section->addTextRun('Paragraph'); $textrun->addLink('https://github.com/PHPOffice/PHPWord'); $textrun->addImage($localImage); $textrun->addFootnote()->addText(htmlspecialchars('Footnote', ENT_COMPAT, 'UTF-8')); $textrun->addEndnote()->addText(htmlspecialchars('Endnote', ENT_COMPAT, 'UTF-8')); $section = $phpWord->addSection(); $table = $section->addTable(); $cell = $table->addRow()->addCell(); $cell->addText(htmlspecialchars('Test 1', ENT_COMPAT, 'UTF-8'), array('superscript' => true, 'underline' => 'dash', 'strikethrough' => true)); $cell->addTextRun(); $cell->addLink('https://github.com/PHPOffice/PHPWord'); $cell->addTextBreak(); $cell->addListItem(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8')); $cell->addImage($localImage); $cell->addObject($objectSrc); $cell->addFootnote(); $cell->addEndnote(); $cell = $table->addRow()->addCell(); $writer = new HTML($phpWord); $writer->save($file); $this->assertTrue(file_exists($file)); unlink($file); }