예제 #1
9
 /**
  * Read styles.xml.
  *
  * @param \PhpOffice\PhpWord\PhpWord $phpWord
  * @return void
  */
 public function read(PhpWord $phpWord)
 {
     $xmlReader = new XMLReader();
     $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
     $nodes = $xmlReader->getElements('w:style');
     if ($nodes->length > 0) {
         foreach ($nodes as $node) {
             $type = $xmlReader->getAttribute('w:type', $node);
             $name = $xmlReader->getAttribute('w:styleId', $node);
             if (is_null($name)) {
                 $name = $xmlReader->getAttribute('w:val', $node, 'w:name');
             }
             preg_match('/Heading(\\d)/', $name, $headingMatches);
             // $default = ($xmlReader->getAttribute('w:default', $node) == 1);
             switch ($type) {
                 case 'paragraph':
                     $paragraphStyle = $this->readParagraphStyle($xmlReader, $node);
                     $fontStyle = $this->readFontStyle($xmlReader, $node);
                     if (!empty($headingMatches)) {
                         $phpWord->addTitleStyle($headingMatches[1], $fontStyle, $paragraphStyle);
                     } else {
                         if (empty($fontStyle)) {
                             if (is_array($paragraphStyle)) {
                                 $phpWord->addParagraphStyle($name, $paragraphStyle);
                             }
                         } else {
                             $phpWord->addFontStyle($name, $fontStyle, $paragraphStyle);
                         }
                     }
                     break;
                 case 'character':
                     $fontStyle = $this->readFontStyle($xmlReader, $node);
                     if (!empty($fontStyle)) {
                         $phpWord->addFontStyle($name, $fontStyle);
                     }
                     break;
                 case 'table':
                     $tStyle = $this->readTableStyle($xmlReader, $node);
                     if (!empty($tStyle)) {
                         $phpWord->addTableStyle($name, $tStyle);
                     }
                     break;
             }
         }
     }
 }
예제 #2
3
 /**
  * Test write styles
  */
 public function testWriteStyles()
 {
     $phpWord = new PhpWord();
     $pStyle = array('align' => 'both');
     $pBase = array('basedOn' => 'Normal');
     $pNew = array('basedOn' => 'Base Style', 'next' => 'Normal');
     $rStyle = array('size' => 20);
     $tStyle = array('bgColor' => 'FF0000', 'cellMarginTop' => 120, 'cellMarginBottom' => 120, 'cellMarginLeft' => 120, 'cellMarginRight' => 120, 'borderTopSize' => 120, 'borderBottomSize' => 120, 'borderLeftSize' => 120, 'borderRightSize' => 120, 'borderInsideHSize' => 120, 'borderInsideVSize' => 120);
     $phpWord->setDefaultParagraphStyle($pStyle);
     $phpWord->addParagraphStyle('Base Style', $pBase);
     $phpWord->addParagraphStyle('New Style', $pNew);
     $phpWord->addFontStyle('New Style', $rStyle, $pStyle);
     $phpWord->addTableStyle('Table Style', $tStyle, $tStyle);
     $phpWord->addTitleStyle(1, $rStyle, $pStyle);
     $doc = TestHelperDOCX::getDocument($phpWord);
     $file = 'word/styles.xml';
     // Normal style generated?
     $path = '/w:styles/w:style[@w:styleId="Normal"]/w:name';
     $element = $doc->getElement($path, $file);
     $this->assertEquals('Normal', $element->getAttribute('w:val'));
     // Parent style referenced?
     $path = '/w:styles/w:style[@w:styleId="New Style"]/w:basedOn';
     $element = $doc->getElement($path, $file);
     $this->assertEquals('Base Style', $element->getAttribute('w:val'));
     // Next paragraph style correct?
     $path = '/w:styles/w:style[@w:styleId="New Style"]/w:next';
     $element = $doc->getElement($path, $file);
     $this->assertEquals('Normal', $element->getAttribute('w:val'));
 }
예제 #3
0
 /**
  * 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);
 }
예제 #4
0
 /**
  * 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);
 }
예제 #5
0
 /**
  * Write element with some styles
  */
 public function testElementStyles()
 {
     $objectSrc = __DIR__ . '/../../../_files/documents/sheet.xls';
     $tabs = array(new \PhpOffice\PhpWord\Style\Tab('right', 9090));
     $phpWord = new PhpWord();
     $phpWord->addParagraphStyle('pStyle', array('align' => 'center', 'tabs' => $tabs, 'shading' => array('fill' => 'FFFF99'), 'borderSize' => 4));
     // Style #1
     $phpWord->addFontStyle('fStyle', array('size' => '20', 'bold' => true, 'allCaps' => true, 'scale' => 200, 'spacing' => 240, 'kerning' => 10));
     // Style #2
     $phpWord->addTitleStyle(1, array('color' => '333333', 'doubleStrikethrough' => true));
     // Style #3
     $phpWord->addTableStyle('tStyle', array('borderSize' => 1));
     $fontStyle = new Font('text', array('align' => 'center'));
     $section = $phpWord->addSection();
     $section->addListItem(htmlspecialchars('List Item', ENT_COMPAT, 'UTF-8'), 0, null, null, 'pStyle');
     // Style #5
     $section->addObject($objectSrc, array('align' => 'center'));
     $section->addTOC($fontStyle);
     $section->addTitle(htmlspecialchars('Title 1', ENT_COMPAT, 'UTF-8'), 1);
     $section->addTOC('fStyle');
     $table = $section->addTable('tStyle');
     $table->setWidth(100);
     $doc = TestHelperDOCX::getDocument($phpWord);
     // List item
     $element = $doc->getElement('/w:document/w:body/w:p[1]/w:pPr/w:numPr/w:numId');
     $this->assertEquals(5, $element->getAttribute('w:val'));
     // Object
     $element = $doc->getElement('/w:document/w:body/w:p[2]/w:r/w:object/o:OLEObject');
     $this->assertEquals('Embed', $element->getAttribute('Type'));
     // TOC
     $element = $doc->getElement('/w:document/w:body/w:p[3]/w:pPr/w:tabs/w:tab');
     $this->assertEquals('right', $element->getAttribute('w:val'));
     $this->assertEquals('dot', $element->getAttribute('w:leader'));
     $this->assertEquals(9062, $element->getAttribute('w:pos'));
 }
예제 #6
0
 /**
  * Render Word object
  *
  * @return PhpWord
  */
 protected function renderWord()
 {
     //Init
     $this->init(true);
     //Build table
     $this->build();
     //PHPWord instance
     $word = new PhpWord();
     $section = $word->addSection();
     $word->addTableStyle('std-table', ['borderColor' => '000000', 'borderSize' => 2, 'cellMargin' => 10], ['bgColor' => 'CCCCCC', 'tblHeader' => true]);
     $font = ['name' => 'Calibri', 'size' => 10];
     $margin = 3000;
     //Title
     $section->addText($this->title, ['name' => 'Calibri', 'size' => 14, 'bold' => true]);
     $section->addTextBreak();
     //Create table
     $table = $section->addTable('std-table');
     $width = round(($section->getStyle()->getPageSizeW() - $margin) / count($this->columns));
     //Header
     $table->addRow();
     foreach (array_except($this->headLinks(), [':actions']) as $caption) {
         $table->addCell($width)->addText(htmlspecialchars(trim(strip_tags($caption))), $font + ['bold' => true]);
     }
     //Body
     foreach ($this->rendered as $row) {
         $table->addRow();
         foreach ($row->getCells() as $cell) {
             if ($cell->getField() != ':actions') {
                 $table->addCell($width)->addText(htmlspecialchars(strip_tags($cell->getCurrentValue())), $font);
             }
         }
     }
     return $word;
 }