Esempio n. 1
0
 /**
  * Write text break element
  */
 public function write()
 {
     if (!$this->withoutP) {
         $hasStyle = false;
         $fontStyle = null;
         $paragraphStyle = null;
         if (!is_null($this->element)) {
             $fontStyle = $this->element->getFontStyle();
             $paragraphStyle = $this->element->getParagraphStyle();
             $hasStyle = !is_null($fontStyle) || !is_null($paragraphStyle);
         }
         if ($hasStyle) {
             $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
             $styleWriter->setIsInline(true);
             $this->xmlWriter->startElement('w:p');
             $styleWriter->write();
             if (!is_null($fontStyle)) {
                 $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
                 $styleWriter->setIsInline(true);
                 $this->xmlWriter->startElement('w:pPr');
                 $styleWriter->write();
                 $this->xmlWriter->endElement();
                 // w:pPr
             }
             $this->xmlWriter->endElement();
             // w:p
         } else {
             $this->xmlWriter->writeElement('w:p');
         }
     } else {
         $this->xmlWriter->writeElement('w:br');
     }
 }
Esempio n. 2
0
 /**
  * Write link element
  */
 public function write()
 {
     $rId = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0);
     $fontStyle = $this->element->getFontStyle();
     $paragraphStyle = $this->element->getParagraphStyle();
     if (!$this->withoutP) {
         $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
         $styleWriter->setIsInline(true);
         $this->xmlWriter->startElement('w:p');
         $styleWriter->write();
     }
     $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
     $styleWriter->setIsInline(true);
     $this->xmlWriter->startElement('w:hyperlink');
     $this->xmlWriter->writeAttribute('r:id', 'rId' . $rId);
     $this->xmlWriter->writeAttribute('w:history', '1');
     $this->xmlWriter->startElement('w:r');
     $styleWriter->write();
     $this->xmlWriter->startElement('w:t');
     $this->xmlWriter->writeAttribute('xml:space', 'preserve');
     $this->xmlWriter->writeRaw($this->element->getText());
     $this->xmlWriter->endElement();
     // w:t
     $this->xmlWriter->endElement();
     // w:r
     $this->xmlWriter->endElement();
     // w:hyperlink
     if (!$this->withoutP) {
         $this->xmlWriter->endElement();
         // w:p
     }
 }
Esempio n. 3
0
 /**
  * Write text element
  */
 public function write()
 {
     $fontStyle = $this->element->getFontStyle();
     $paragraphStyle = $this->element->getParagraphStyle();
     $text = htmlspecialchars($this->element->getText());
     $text = String::controlCharacterPHP2OOXML($text);
     if (!$this->withoutP) {
         $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
         $styleWriter->setIsInline(true);
         $this->xmlWriter->startElement('w:p');
         $styleWriter->write();
     }
     $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
     $styleWriter->setIsInline(true);
     $this->xmlWriter->startElement('w:r');
     $styleWriter->write();
     $this->xmlWriter->startElement('w:t');
     $this->xmlWriter->writeAttribute('xml:space', 'preserve');
     $this->xmlWriter->writeRaw($text);
     $this->xmlWriter->endElement();
     $this->xmlWriter->endElement();
     // w:r
     if (!$this->withoutP) {
         $this->xmlWriter->endElement();
         // w:p
     }
 }
Esempio n. 4
0
 /**
  * Write preserve text element
  */
 public function write()
 {
     $fontStyle = $this->element->getFontStyle();
     $paragraphStyle = $this->element->getParagraphStyle();
     $texts = $this->element->getText();
     if (!is_array($texts)) {
         $texts = array($texts);
     }
     $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
     $styleWriter->setIsInline(true);
     $this->xmlWriter->startElement('w:p');
     $styleWriter->write();
     foreach ($texts as $text) {
         if (substr($text, 0, 1) == '{') {
             $text = substr($text, 1, -1);
             $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
             $styleWriter->setIsInline(true);
             $this->xmlWriter->startElement('w:r');
             $this->xmlWriter->startElement('w:fldChar');
             $this->xmlWriter->writeAttribute('w:fldCharType', 'begin');
             $this->xmlWriter->endElement();
             $this->xmlWriter->endElement();
             $this->xmlWriter->startElement('w:r');
             $styleWriter->write();
             $this->xmlWriter->startElement('w:instrText');
             $this->xmlWriter->writeAttribute('xml:space', 'preserve');
             $this->xmlWriter->writeRaw($text);
             $this->xmlWriter->endElement();
             $this->xmlWriter->endElement();
             $this->xmlWriter->startElement('w:r');
             $this->xmlWriter->startElement('w:fldChar');
             $this->xmlWriter->writeAttribute('w:fldCharType', 'separate');
             $this->xmlWriter->endElement();
             $this->xmlWriter->endElement();
             $this->xmlWriter->startElement('w:r');
             $this->xmlWriter->startElement('w:fldChar');
             $this->xmlWriter->writeAttribute('w:fldCharType', 'end');
             $this->xmlWriter->endElement();
             $this->xmlWriter->endElement();
         } else {
             $text = htmlspecialchars($text);
             $text = String::controlCharacterPHP2OOXML($text);
             $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
             $styleWriter->setIsInline(true);
             $this->xmlWriter->startElement('w:r');
             $styleWriter->write();
             $this->xmlWriter->startElement('w:t');
             $this->xmlWriter->writeAttribute('xml:space', 'preserve');
             $this->xmlWriter->writeRaw($text);
             $this->xmlWriter->endElement();
             $this->xmlWriter->endElement();
         }
     }
     $this->xmlWriter->endElement();
     // p
 }
Esempio n. 5
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();
 }
Esempio n. 6
0
 /**
  * Write ending
  */
 protected function writeFontStyle()
 {
     $xmlWriter = $this->getXmlWriter();
     /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
     $element = $this->getElement();
     $fontStyle = $element->getFontStyle();
     $styleWriter = new FontStyleWriter($xmlWriter, $fontStyle);
     $styleWriter->setIsInline(true);
     $styleWriter->write();
 }
Esempio n. 7
0
 /**
  * Write title
  *
  * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  * @param \PhpOffice\PhpWord\Element\TOC $element
  * @param \PhpOffice\PhpWord\Element\Title $title
  * @param bool $writeFieldMark
  */
 private function writeTitle(XMLWriter $xmlWriter, TOCElement $element, $title, $writeFieldMark)
 {
     $tocStyle = $element->getStyleTOC();
     $fontStyle = $element->getStyleFont();
     $isObject = $fontStyle instanceof Font ? true : false;
     $rId = $title->getRelationId();
     $indent = ($title->getDepth() - 1) * $tocStyle->getIndent();
     $xmlWriter->startElement('w:p');
     // Write style and field mark
     $this->writeStyle($xmlWriter, $element, $indent);
     if ($writeFieldMark) {
         $this->writeFieldMark($xmlWriter, $element);
     }
     // Hyperlink
     $xmlWriter->startElement('w:hyperlink');
     $xmlWriter->writeAttribute('w:anchor', "_Toc{$rId}");
     $xmlWriter->writeAttribute('w:history', '1');
     // Title text
     $xmlWriter->startElement('w:r');
     if ($isObject) {
         $styleWriter = new FontStyleWriter($xmlWriter, $fontStyle);
         $styleWriter->write();
     }
     $xmlWriter->startElement('w:t');
     $xmlWriter->writeRaw($title->getText());
     $xmlWriter->endElement();
     $xmlWriter->endElement();
     // w:r
     $xmlWriter->startElement('w:r');
     $xmlWriter->writeElement('w:tab', null);
     $xmlWriter->endElement();
     $xmlWriter->startElement('w:r');
     $xmlWriter->startElement('w:fldChar');
     $xmlWriter->writeAttribute('w:fldCharType', 'begin');
     $xmlWriter->endElement();
     $xmlWriter->endElement();
     $xmlWriter->startElement('w:r');
     $xmlWriter->startElement('w:instrText');
     $xmlWriter->writeAttribute('xml:space', 'preserve');
     $xmlWriter->writeRaw("PAGEREF _Toc{$rId} \\h");
     $xmlWriter->endElement();
     $xmlWriter->endElement();
     $xmlWriter->startElement('w:r');
     $xmlWriter->startElement('w:fldChar');
     $xmlWriter->writeAttribute('w:fldCharType', 'end');
     $xmlWriter->endElement();
     $xmlWriter->endElement();
     $xmlWriter->endElement();
     // w:hyperlink
     $xmlWriter->endElement();
     // w:p
 }
Esempio n. 8
0
 /**
  * Write font style
  *
  * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter
  * @param string $styleName
  * @param \PhpOffice\PhpWord\Style\Font $style
  */
 private function writeFontStyle(XMLWriter $xmlWriter, $styleName, FontStyle $style)
 {
     $paragraphStyle = $style->getParagraph();
     $styleType = $style->getStyleType();
     $type = $styleType == 'title' ? 'paragraph' : 'character';
     if (!is_null($paragraphStyle)) {
         $type = 'paragraph';
     }
     $xmlWriter->startElement('w:style');
     $xmlWriter->writeAttribute('w:type', $type);
     // Heading style
     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();
     }
     // Style name
     $xmlWriter->startElement('w:name');
     $xmlWriter->writeAttribute('w:val', $styleName);
     $xmlWriter->endElement();
     // Parent style
     $xmlWriter->writeElementIf(!is_null($paragraphStyle), 'w:basedOn', 'w:val', 'Normal');
     // w:pPr
     if (!is_null($paragraphStyle)) {
         $styleWriter = new ParagraphStyleWriter($xmlWriter, $paragraphStyle);
         $styleWriter->write();
     }
     // w:rPr
     $styleWriter = new FontStyleWriter($xmlWriter, $style);
     $styleWriter->write();
     $xmlWriter->endElement();
 }
Esempio n. 9
0
 /**
  * Write element
  */
 public function write()
 {
     $name = htmlspecialchars($this->element->getName());
     $name = String::controlCharacterPHP2OOXML($name);
     $text = htmlspecialchars($this->element->getText());
     $text = String::controlCharacterPHP2OOXML($text);
     $fontStyle = $this->element->getFontStyle();
     $paragraphStyle = $this->element->getParagraphStyle();
     if (!$this->withoutP) {
         $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
         $styleWriter->setIsInline(true);
         $this->xmlWriter->startElement('w:p');
         $styleWriter->write();
     }
     $this->xmlWriter->startElement('w:r');
     $this->xmlWriter->startElement('w:fldChar');
     $this->xmlWriter->writeAttribute('w:fldCharType', 'begin');
     $this->xmlWriter->startElement('w:ffData');
     $this->xmlWriter->startElement('w:name');
     $this->xmlWriter->writeAttribute('w:val', $name);
     $this->xmlWriter->endElement();
     //w:name
     $this->xmlWriter->writeAttribute('w:enabled', '');
     $this->xmlWriter->startElement('w:calcOnExit');
     $this->xmlWriter->writeAttribute('w:val', '0');
     $this->xmlWriter->endElement();
     //w:calcOnExit
     $this->xmlWriter->startElement('w:checkBox');
     $this->xmlWriter->writeAttribute('w:sizeAuto', '');
     $this->xmlWriter->startElement('w:default');
     $this->xmlWriter->writeAttribute('w:val', 0);
     $this->xmlWriter->endElement();
     //w:default
     $this->xmlWriter->endElement();
     //w:checkBox
     $this->xmlWriter->endElement();
     // w:ffData
     $this->xmlWriter->endElement();
     // w:fldChar
     $this->xmlWriter->endElement();
     // w:r
     $this->xmlWriter->startElement('w:r');
     $this->xmlWriter->startElement('w:instrText');
     $this->xmlWriter->writeAttribute('xml:space', 'preserve');
     $this->xmlWriter->writeRaw(' FORMCHECKBOX ');
     $this->xmlWriter->endElement();
     // w:instrText
     $this->xmlWriter->endElement();
     // w:r
     $this->xmlWriter->startElement('w:r');
     $this->xmlWriter->startElement('w:fldChar');
     $this->xmlWriter->writeAttribute('w:fldCharType', 'seperate');
     $this->xmlWriter->endElement();
     // w:fldChar
     $this->xmlWriter->endElement();
     // w:r
     $this->xmlWriter->startElement('w:r');
     $this->xmlWriter->startElement('w:fldChar');
     $this->xmlWriter->writeAttribute('w:fldCharType', 'end');
     $this->xmlWriter->endElement();
     // w:fldChar
     $this->xmlWriter->endElement();
     // w:r
     $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
     $styleWriter->setIsInline(true);
     $this->xmlWriter->startElement('w:r');
     $styleWriter->write();
     $this->xmlWriter->startElement('w:t');
     $this->xmlWriter->writeAttribute('xml:space', 'preserve');
     $this->xmlWriter->writeRaw($text);
     $this->xmlWriter->endElement();
     // w:t
     $this->xmlWriter->endElement();
     // w:r
     if (!$this->withoutP) {
         $this->xmlWriter->endElement();
         // w:p
     }
 }
Esempio n. 10
0
 /**
  * Write element
  */
 public function write()
 {
     $titles = $this->element->getTitles();
     $fontStyle = $this->element->getStyleFont();
     $tocStyle = $this->element->getStyleTOC();
     $fIndent = $tocStyle->getIndent();
     $tabLeader = $tocStyle->getTabLeader();
     $tabPos = $tocStyle->getTabPos();
     $maxDepth = $this->element->getMaxDepth();
     $minDepth = $this->element->getMinDepth();
     $isObject = $fontStyle instanceof Font ? true : false;
     for ($i = 0; $i < count($titles); $i++) {
         $title = $titles[$i];
         $indent = ($title['depth'] - 1) * $fIndent;
         $this->xmlWriter->startElement('w:p');
         $this->xmlWriter->startElement('w:pPr');
         if ($isObject && !is_null($fontStyle->getParagraphStyle())) {
             $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $fontStyle->getParagraphStyle());
             $styleWriter->write();
         }
         if ($indent > 0) {
             $this->xmlWriter->startElement('w:ind');
             $this->xmlWriter->writeAttribute('w:left', $indent);
             $this->xmlWriter->endElement();
         }
         if (!empty($fontStyle) && !$isObject) {
             $this->xmlWriter->startElement('w:pPr');
             $this->xmlWriter->startElement('w:pStyle');
             $this->xmlWriter->writeAttribute('w:val', $fontStyle);
             $this->xmlWriter->endElement();
             $this->xmlWriter->endElement();
         }
         $this->xmlWriter->startElement('w:tabs');
         $this->xmlWriter->startElement('w:tab');
         $this->xmlWriter->writeAttribute('w:val', 'right');
         if (!empty($tabLeader)) {
             $this->xmlWriter->writeAttribute('w:leader', $tabLeader);
         }
         $this->xmlWriter->writeAttribute('w:pos', $tabPos);
         $this->xmlWriter->endElement();
         $this->xmlWriter->endElement();
         $this->xmlWriter->endElement();
         // w:pPr
         if ($i == 0) {
             $this->xmlWriter->startElement('w:r');
             $this->xmlWriter->startElement('w:fldChar');
             $this->xmlWriter->writeAttribute('w:fldCharType', 'begin');
             $this->xmlWriter->endElement();
             $this->xmlWriter->endElement();
             $this->xmlWriter->startElement('w:r');
             $this->xmlWriter->startElement('w:instrText');
             $this->xmlWriter->writeAttribute('xml:space', 'preserve');
             $this->xmlWriter->writeRaw('TOC \\o "' . $minDepth . '-' . $maxDepth . '" \\h \\z \\u');
             $this->xmlWriter->endElement();
             $this->xmlWriter->endElement();
             $this->xmlWriter->startElement('w:r');
             $this->xmlWriter->startElement('w:fldChar');
             $this->xmlWriter->writeAttribute('w:fldCharType', 'separate');
             $this->xmlWriter->endElement();
             $this->xmlWriter->endElement();
         }
         $this->xmlWriter->startElement('w:hyperlink');
         $this->xmlWriter->writeAttribute('w:anchor', $title['anchor']);
         $this->xmlWriter->writeAttribute('w:history', '1');
         $this->xmlWriter->startElement('w:r');
         if ($isObject) {
             $styleWriter = new FontStyleWriter($this->xmlWriter, $fontStyle);
             $styleWriter->write();
         }
         $this->xmlWriter->startElement('w:t');
         $this->xmlWriter->writeRaw($title['text']);
         $this->xmlWriter->endElement();
         $this->xmlWriter->endElement();
         $this->xmlWriter->startElement('w:r');
         $this->xmlWriter->writeElement('w:tab', null);
         $this->xmlWriter->endElement();
         $this->xmlWriter->startElement('w:r');
         $this->xmlWriter->startElement('w:fldChar');
         $this->xmlWriter->writeAttribute('w:fldCharType', 'begin');
         $this->xmlWriter->endElement();
         $this->xmlWriter->endElement();
         $this->xmlWriter->startElement('w:r');
         $this->xmlWriter->startElement('w:instrText');
         $this->xmlWriter->writeAttribute('xml:space', 'preserve');
         $this->xmlWriter->writeRaw('PAGEREF ' . $title['anchor'] . ' \\h');
         $this->xmlWriter->endElement();
         $this->xmlWriter->endElement();
         $this->xmlWriter->startElement('w:r');
         $this->xmlWriter->startElement('w:fldChar');
         $this->xmlWriter->writeAttribute('w:fldCharType', 'end');
         $this->xmlWriter->endElement();
         $this->xmlWriter->endElement();
         $this->xmlWriter->endElement();
         // w:hyperlink
         $this->xmlWriter->endElement();
         // w:p
     }
     $this->xmlWriter->startElement('w:p');
     $this->xmlWriter->startElement('w:r');
     $this->xmlWriter->startElement('w:fldChar');
     $this->xmlWriter->writeAttribute('w:fldCharType', 'end');
     $this->xmlWriter->endElement();
     $this->xmlWriter->endElement();
     $this->xmlWriter->endElement();
 }