Exemplo n.º 1
0
 /**
  * Write element.
  *
  * @return void
  */
 public function write()
 {
     $xmlWriter = $this->getXmlWriter();
     $element = $this->getElement();
     if (!$element instanceof TableElement) {
         return;
     }
     $rows = $element->getRows();
     $rowCount = count($rows);
     if ($rowCount > 0) {
         $xmlWriter->startElement('w:tbl');
         // Write columns
         $this->writeColumns($xmlWriter, $element);
         // Write style
         $styleWriter = new TableStyleWriter($xmlWriter, $element->getStyle());
         $styleWriter->setWidth($element->getWidth());
         $styleWriter->write();
         // Write rows
         for ($i = 0; $i < $rowCount; $i++) {
             $this->writeRow($xmlWriter, $rows[$i]);
         }
         $xmlWriter->endElement();
         // w:tbl
     }
 }
Exemplo n.º 2
0
 /**
  * Write table style
  *
  * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  * @param string $styleName
  * @param \PhpOffice\PhpWord\Style\Table $style
  */
 private function writeTableStyle(XMLWriter $xmlWriter, $styleName, TableStyle $style)
 {
     $xmlWriter->startElement('w:style');
     $xmlWriter->writeAttribute('w:type', 'table');
     $xmlWriter->writeAttribute('w:customStyle', '1');
     $xmlWriter->writeAttribute('w:styleId', $styleName);
     $xmlWriter->startElement('w:name');
     $xmlWriter->writeAttribute('w:val', $styleName);
     $xmlWriter->endElement();
     $xmlWriter->startElement('w:uiPriority');
     $xmlWriter->writeAttribute('w:val', '99');
     $xmlWriter->endElement();
     $styleWriter = new TableStyleWriter($xmlWriter, $style);
     $styleWriter->write();
     $xmlWriter->endElement();
     // w:style
 }
Exemplo n.º 3
0
 /**
  * Write word/styles.xml
  *
  * @param \PhpOffice\PhpWord\PhpWord $phpWord
  */
 public function writeStyles(PhpWord $phpWord = null)
 {
     if (is_null($phpWord)) {
         throw new Exception("No PhpWord assigned.");
     }
     $xmlWriter = $this->getXmlWriter();
     $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
     $xmlWriter->startElement('w:styles');
     $xmlWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
     $xmlWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
     // Write default styles
     $styles = Style::getStyles();
     $this->writeDefaultStyles($xmlWriter, $phpWord, $styles);
     // Write styles
     if (count($styles) > 0) {
         foreach ($styles as $styleName => $style) {
             if ($styleName == 'Normal') {
                 continue;
             }
             // Font style
             if ($style instanceof Font) {
                 $paragraphStyle = $style->getParagraphStyle();
                 $styleType = $style->getStyleType();
                 $type = $styleType == 'title' ? 'paragraph' : 'character';
                 if (!is_null($paragraphStyle)) {
                     $type = 'paragraph';
                 }
                 $xmlWriter->startElement('w:style');
                 $xmlWriter->writeAttribute('w:type', $type);
                 if ($styleType == 'title') {
                     $arrStyle = explode('_', $styleName);
                     $styleId = 'Heading' . $arrStyle[1];
                     $styleName = 'heading ' . $arrStyle[1];
                     $styleLink = 'Heading' . $arrStyle[1] . 'Char';
                     $xmlWriter->writeAttribute('w:styleId', $styleId);
                     $xmlWriter->startElement('w:link');
                     $xmlWriter->writeAttribute('w:val', $styleLink);
                     $xmlWriter->endElement();
                 }
                 $xmlWriter->startElement('w:name');
                 $xmlWriter->writeAttribute('w:val', $styleName);
                 $xmlWriter->endElement();
                 if (!is_null($paragraphStyle)) {
                     // Point parent style to Normal
                     $xmlWriter->startElement('w:basedOn');
                     $xmlWriter->writeAttribute('w:val', 'Normal');
                     $xmlWriter->endElement();
                     $styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle);
                     $styleWriter->write();
                 }
                 $styleWriter = new FontStyleWriter($xmlWriter, $style);
                 $styleWriter->write();
                 $xmlWriter->endElement();
                 // Paragraph style
             } elseif ($style instanceof Paragraph) {
                 $xmlWriter->startElement('w:style');
                 $xmlWriter->writeAttribute('w:type', 'paragraph');
                 $xmlWriter->writeAttribute('w:customStyle', '1');
                 $xmlWriter->writeAttribute('w:styleId', $styleName);
                 $xmlWriter->startElement('w:name');
                 $xmlWriter->writeAttribute('w:val', $styleName);
                 $xmlWriter->endElement();
                 // Parent style
                 $basedOn = $style->getBasedOn();
                 if (!is_null($basedOn)) {
                     $xmlWriter->startElement('w:basedOn');
                     $xmlWriter->writeAttribute('w:val', $basedOn);
                     $xmlWriter->endElement();
                 }
                 // Next paragraph style
                 $next = $style->getNext();
                 if (!is_null($next)) {
                     $xmlWriter->startElement('w:next');
                     $xmlWriter->writeAttribute('w:val', $next);
                     $xmlWriter->endElement();
                 }
                 $styleWriter = new ParagraphStyleWriter($xmlWriter, $style);
                 $styleWriter->write();
                 $xmlWriter->endElement();
                 // Table style
             } elseif ($style instanceof Table) {
                 $xmlWriter->startElement('w:style');
                 $xmlWriter->writeAttribute('w:type', 'table');
                 $xmlWriter->writeAttribute('w:customStyle', '1');
                 $xmlWriter->writeAttribute('w:styleId', $styleName);
                 $xmlWriter->startElement('w:name');
                 $xmlWriter->writeAttribute('w:val', $styleName);
                 $xmlWriter->endElement();
                 $xmlWriter->startElement('w:uiPriority');
                 $xmlWriter->writeAttribute('w:val', '99');
                 $xmlWriter->endElement();
                 $styleWriter = new TableStyleWriter($xmlWriter, $style);
                 $styleWriter->write();
                 $xmlWriter->endElement();
                 // w:style
             }
         }
     }
     $xmlWriter->endElement();
     // w:styles
     return $xmlWriter->getData();
 }
Exemplo n.º 4
0
 /**
  * Write element
  */
 public function write()
 {
     $rows = $this->element->getRows();
     $rowCount = count($rows);
     if ($rowCount > 0) {
         $this->xmlWriter->startElement('w:tbl');
         // Table grid
         $cellWidths = array();
         for ($i = 0; $i < $rowCount; $i++) {
             $row = $rows[$i];
             $cells = $row->getCells();
             if (count($cells) <= count($cellWidths)) {
                 continue;
             }
             $cellWidths = array();
             foreach ($cells as $cell) {
                 $cellWidths[] = $cell->getWidth();
             }
         }
         $this->xmlWriter->startElement('w:tblGrid');
         foreach ($cellWidths as $width) {
             $this->xmlWriter->startElement('w:gridCol');
             if (!is_null($width)) {
                 $this->xmlWriter->writeAttribute('w:w', $width);
                 $this->xmlWriter->writeAttribute('w:type', 'dxa');
             }
             $this->xmlWriter->endElement();
         }
         $this->xmlWriter->endElement();
         // w:tblGrid
         // Table style
         $tblStyle = $this->element->getStyle();
         $tblWidth = $this->element->getWidth();
         if ($tblStyle instanceof TableStyle) {
             $styleWriter = new TableStyleWriter($this->xmlWriter, $tblStyle);
             $styleWriter->setIsFullStyle(false);
             $styleWriter->write();
         } else {
             if (!empty($tblStyle)) {
                 $this->xmlWriter->startElement('w:tblPr');
                 $this->xmlWriter->startElement('w:tblStyle');
                 $this->xmlWriter->writeAttribute('w:val', $tblStyle);
                 $this->xmlWriter->endElement();
                 if (!is_null($tblWidth)) {
                     $this->xmlWriter->startElement('w:tblW');
                     $this->xmlWriter->writeAttribute('w:w', $tblWidth);
                     $this->xmlWriter->writeAttribute('w:type', 'pct');
                     $this->xmlWriter->endElement();
                 }
                 $this->xmlWriter->endElement();
             }
         }
         // Table rows
         for ($i = 0; $i < $rowCount; $i++) {
             $row = $rows[$i];
             $height = $row->getHeight();
             $rowStyle = $row->getStyle();
             $tblHeader = $rowStyle->getTblHeader();
             $cantSplit = $rowStyle->getCantSplit();
             $exactHeight = $rowStyle->getExactHeight();
             $this->xmlWriter->startElement('w:tr');
             if (!is_null($height) || !is_null($tblHeader) || !is_null($cantSplit)) {
                 $this->xmlWriter->startElement('w:trPr');
                 if (!is_null($height)) {
                     $this->xmlWriter->startElement('w:trHeight');
                     $this->xmlWriter->writeAttribute('w:val', $height);
                     $this->xmlWriter->writeAttribute('w:hRule', $exactHeight ? 'exact' : 'atLeast');
                     $this->xmlWriter->endElement();
                 }
                 if ($tblHeader) {
                     $this->xmlWriter->startElement('w:tblHeader');
                     $this->xmlWriter->writeAttribute('w:val', '1');
                     $this->xmlWriter->endElement();
                 }
                 if ($cantSplit) {
                     $this->xmlWriter->startElement('w:cantSplit');
                     $this->xmlWriter->writeAttribute('w:val', '1');
                     $this->xmlWriter->endElement();
                 }
                 $this->xmlWriter->endElement();
             }
             foreach ($row->getCells() as $cell) {
                 $cellStyle = $cell->getStyle();
                 $width = $cell->getWidth();
                 $this->xmlWriter->startElement('w:tc');
                 $this->xmlWriter->startElement('w:tcPr');
                 $this->xmlWriter->startElement('w:tcW');
                 $this->xmlWriter->writeAttribute('w:w', $width);
                 $this->xmlWriter->writeAttribute('w:type', 'dxa');
                 $this->xmlWriter->endElement();
                 // w:tcW
                 if ($cellStyle instanceof Cell) {
                     $styleWriter = new CellStyleWriter($this->xmlWriter, $cellStyle);
                     $styleWriter->write();
                 }
                 $this->xmlWriter->endElement();
                 // w:tcPr
                 $this->parentWriter->writeContainerElements($this->xmlWriter, $cell);
                 $this->xmlWriter->endElement();
                 // w:tc
             }
             $this->xmlWriter->endElement();
             // w:tr
         }
         $this->xmlWriter->endElement();
     }
 }