/** * Test set/get document properties */ public function testSetGetDocumentProperties() { $phpWord = new PhpWord(); $creator = 'PhpWord'; $properties = $phpWord->getDocumentProperties(); $properties->setCreator($creator); $phpWord->setDocumentProperties($properties); $this->assertEquals($creator, $phpWord->getDocumentProperties()->getCreator()); }
/** * Read meta.xml * * @param \PhpOffice\PhpWord\PhpWord $phpWord * @todo Process property type */ public function read(PhpWord &$phpWord) { $xmlReader = new XMLReader(); $xmlReader->getDomFromZip($this->docFile, $this->xmlFile); $docProps = $phpWord->getDocumentProperties(); $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); } } }
/** * 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->getDocumentProperties(); $docProps->setTitle('HTML Test'); $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('Test 1', 'Font', 'Paragraph'); $section->addTextBreak(); $section->addText('Test 2', array('name' => 'Tahoma', 'bold' => true, 'italic' => true, 'subscript' => true)); $section->addLink('http://test.com'); $section->addTitle('Test', 1); $section->addPageBreak(); $section->addListItem('Test'); $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('Test 3'); $textrun->addTextBreak(); $textrun = $section->addTextRun('Paragraph'); $textrun->addLink('http://test.com'); $textrun->addImage($localImage); $textrun->addFootnote()->addText('Footnote'); $textrun->addEndnote()->addText('Endnote'); $section = $phpWord->addSection(); $table = $section->addTable(); $cell = $table->addRow()->addCell(); $cell->addText('Test 1', array('superscript' => true, 'underline' => 'dash', 'strikethrough' => true)); $cell->addTextRun(); $cell->addLink('http://test.com'); $cell->addTextBreak(); $cell->addListItem('Test'); $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); }
/** * Read custom document properties * * @param \PhpOffice\PhpWord\PhpWord $phpWord */ public function read(PhpWord &$phpWord) { $xmlReader = new XMLReader(); $xmlReader->getDomFromZip($this->docFile, $this->xmlFile); $docProps = $phpWord->getDocumentProperties(); $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 = DocumentProperties::convertProperty($attributeValue, $attributeType); $attributeType = DocumentProperties::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->getDocumentProperties(); $docProps->setCustomProperty('Company', 'PHPWord'); $phpWord->setDefaultFontName('Verdana'); $phpWord->addFontStyle('Font', array('size' => 11)); $phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); $phpWord->addTableStyle('tblStyle', array('width' => 100)); $section = $phpWord->addSection(array('colsNum' => 2)); $section->addText($expected); $section->addText('Test font style', 'Font'); $section->addText('Test paragraph style', null, 'Paragraph'); $section->addLink('http://test.com', 'Test link'); $section->addTitle('Test title', 1); $section->addTextBreak(); $section->addPageBreak(); $section->addListItem('Test list item'); $section->addImage($imageSrc, array('width' => 50)); $section->addObject($objectSrc); $section->addTOC(); $textrun = $section->addTextRun(); $textrun->addText('Test text run'); $table = $section->addTable(array('width' => 50)); $cell = $table->addRow()->addCell(); $cell = $table->addRow()->addCell(); $cell->addText('Test'); $cell->addLink('http://test.com', 'Test link'); $cell->addTextBreak(); $cell->addListItem('Test list item'); $cell->addImage($imageSrc); $cell->addObject($objectSrc); $textrun = $cell->addTextRun(); $textrun->addText('Test text run'); $footer = $section->addFooter(); $footer->addPreserveText('{PAGE}'); $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 */ public function read(PhpWord &$phpWord) { $xmlReader = new XMLReader(); $xmlReader->getDomFromZip($this->docFile, $this->xmlFile); $docProps = $phpWord->getDocumentProperties(); $nodes = $xmlReader->getElements('*'); if ($nodes->length > 0) { foreach ($nodes as $node) { if (!array_key_exists($node->nodeName, $this->mapping)) { continue; } $method = $this->mapping[$node->nodeName]; $value = $node->nodeValue == '' ? null : $node->nodeValue; if (array_key_exists($node->nodeName, $this->callbacks)) { $value = $this->callbacks[$node->nodeName]($value); } if (method_exists($docProps, $method)) { $docProps->{$method}($value); } } } }