From ea15477efd671bac473b6400fb98f8b2198d5ead Mon Sep 17 00:00:00 2001 From: Samuel BF Date: Thu, 5 Sep 2019 10:40:41 +0200 Subject: [PATCH 1/4] Add header/footer support for RTF writer. --- src/PhpWord/Writer/RTF/Part/Document.php | 14 ++++ src/PhpWord/Writer/RTF/Part/SectionFooter.php | 31 ++++++++ src/PhpWord/Writer/RTF/Part/SectionHeader.php | 71 +++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 src/PhpWord/Writer/RTF/Part/SectionFooter.php create mode 100644 src/PhpWord/Writer/RTF/Part/SectionHeader.php diff --git a/src/PhpWord/Writer/RTF/Part/Document.php b/src/PhpWord/Writer/RTF/Part/Document.php index d4bfadb4c5..c420372a0c 100644 --- a/src/PhpWord/Writer/RTF/Part/Document.php +++ b/src/PhpWord/Writer/RTF/Part/Document.php @@ -121,10 +121,24 @@ private function writeSections() $sections = $this->getParentWriter()->getPhpWord()->getSections(); foreach ($sections as $section) { + // Write styles $styleWriter = new SectionStyleWriter($section->getStyle()); $styleWriter->setParentWriter($this->getParentWriter()); $content .= $styleWriter->write(); + // Append headers / footers + foreach (array('Header', 'Footer') as $sectionPart) { + $getFunction = 'get' . $sectionPart . 's'; + $className = 'PhpOffice\\PhpWord\\Writer\\RTF\\Part\\Section' . $sectionPart; + foreach ($section->$getFunction() as &$part) { + $partWriter = new $className(); + $partWriter->setParentWriter($this->getParentWriter()); + $partWriter->setElement($part); + $content .= $partWriter->write(); + } + } + + // Write content $elementWriter = new Container($this->getParentWriter(), $section); $content .= $elementWriter->write(); diff --git a/src/PhpWord/Writer/RTF/Part/SectionFooter.php b/src/PhpWord/Writer/RTF/Part/SectionFooter.php new file mode 100644 index 0000000000..68117bf59c --- /dev/null +++ b/src/PhpWord/Writer/RTF/Part/SectionFooter.php @@ -0,0 +1,31 @@ +rootElement; + + $containerWriter = new Container($this->getParentWriter(), $this->element); + $content .= $containerWriter->write(); + + $content .= '}' . PHP_EOL; + + return $content; + } + + /** + * Set element + * + * @param \PhpOffice\PhpWord\Element\Footer|\PhpOffice\PhpWord\Element\Header $element + * @return self + */ + public function setElement($element) + { + $this->element = $element; + + return $this; + } +} From 74688c38ebd5cf7b61caac6184990e5b221f1ae6 Mon Sep 17 00:00:00 2001 From: Samuel BF Date: Thu, 5 Sep 2019 13:25:08 +0200 Subject: [PATCH 2/4] Fix code for PHP 5.3 and 5.4 (temporary array) --- src/PhpWord/Writer/RTF/Part/Document.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/Writer/RTF/Part/Document.php b/src/PhpWord/Writer/RTF/Part/Document.php index c420372a0c..0df923c147 100644 --- a/src/PhpWord/Writer/RTF/Part/Document.php +++ b/src/PhpWord/Writer/RTF/Part/Document.php @@ -127,7 +127,8 @@ private function writeSections() $content .= $styleWriter->write(); // Append headers / footers - foreach (array('Header', 'Footer') as $sectionPart) { + $sectionParts = array('Header', 'Footer'); + foreach ($sectionParts as $sectionPart) { $getFunction = 'get' . $sectionPart . 's'; $className = 'PhpOffice\\PhpWord\\Writer\\RTF\\Part\\Section' . $sectionPart; foreach ($section->$getFunction() as &$part) { From 9120fa54f10212e94aaf625d0d00b6cfd974825d Mon Sep 17 00:00:00 2001 From: Samuel BF <36996277+Samuel-BF@users.noreply.github.com> Date: Thu, 5 Sep 2019 14:01:56 +0200 Subject: [PATCH 3/4] array bug (old PHP), support for "1st page header" --- src/PhpWord/Writer/RTF/Part/Document.php | 3 ++- src/PhpWord/Writer/RTF/Part/SectionHeader.php | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/PhpWord/Writer/RTF/Part/Document.php b/src/PhpWord/Writer/RTF/Part/Document.php index 0df923c147..66dd8fd14d 100644 --- a/src/PhpWord/Writer/RTF/Part/Document.php +++ b/src/PhpWord/Writer/RTF/Part/Document.php @@ -131,7 +131,8 @@ private function writeSections() foreach ($sectionParts as $sectionPart) { $getFunction = 'get' . $sectionPart . 's'; $className = 'PhpOffice\\PhpWord\\Writer\\RTF\\Part\\Section' . $sectionPart; - foreach ($section->$getFunction() as &$part) { + $parts = $section->$getFunction(); + foreach ($parts as $part) { $partWriter = new $className(); $partWriter->setParentWriter($this->getParentWriter()); $partWriter->setElement($part); diff --git a/src/PhpWord/Writer/RTF/Part/SectionHeader.php b/src/PhpWord/Writer/RTF/Part/SectionHeader.php index 25a1b1cba5..e279ae07d9 100644 --- a/src/PhpWord/Writer/RTF/Part/SectionHeader.php +++ b/src/PhpWord/Writer/RTF/Part/SectionHeader.php @@ -17,6 +17,7 @@ namespace PhpOffice\PhpWord\Writer\RTF\Part; +use PhpOffice\PhpWord\Element\Header; use PhpOffice\PhpWord\Writer\RTF\Element\Container; /** @@ -47,6 +48,12 @@ public function write() { $content = '{'; $content .= $this->rootElement; + $type = $this->element->getType(); + if ($type == Header::FIRST) { + $content .= 'f'; + } elseif ($type == Header::EVEN) { + $content .= 'r'; + } $containerWriter = new Container($this->getParentWriter(), $this->element); $content .= $containerWriter->write(); From 46616a6fa340f69cefa2f468bb19de1bf12fa91e Mon Sep 17 00:00:00 2001 From: Samuel BF <36996277+Samuel-BF@users.noreply.github.com> Date: Thu, 5 Sep 2019 14:25:51 +0200 Subject: [PATCH 4/4] Reverse SectionFooter/Header(see PhpWord\Element\) --- src/PhpWord/Writer/RTF/Part/SectionFooter.php | 49 ++++++++++++++++++- src/PhpWord/Writer/RTF/Part/SectionHeader.php | 49 +------------------ 2 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/PhpWord/Writer/RTF/Part/SectionFooter.php b/src/PhpWord/Writer/RTF/Part/SectionFooter.php index 68117bf59c..12b0ae1502 100644 --- a/src/PhpWord/Writer/RTF/Part/SectionFooter.php +++ b/src/PhpWord/Writer/RTF/Part/SectionFooter.php @@ -17,10 +17,13 @@ namespace PhpOffice\PhpWord\Writer\RTF\Part; +use PhpOffice\PhpWord\Element\Footer; +use PhpOffice\PhpWord\Writer\RTF\Element\Container; + /** * RTF page footer writer */ -class SectionFooter extends SectionHeader +class SectionFooter extends AbstractPart { /** * Root element name @@ -28,4 +31,48 @@ class SectionFooter extends SectionHeader * @var string */ protected $rootElement = '\footer'; + + /** + * Footer/header element to be written + * + * @var \PhpOffice\PhpWord\Element\Footer|\PhpOffice\PhpWord\Element\Header + */ + protected $element; + + /** + * Write part + * + * @return string + */ + public function write() + { + $content = '{'; + $content .= $this->rootElement; + $type = $this->element->getType(); + if ($type == Footer::FIRST) { + $content .= 'f'; + } elseif ($type == Footer::EVEN) { + $content .= 'r'; + } + + $containerWriter = new Container($this->getParentWriter(), $this->element); + $content .= $containerWriter->write(); + + $content .= '}' . PHP_EOL; + + return $content; + } + + /** + * Set element + * + * @param \PhpOffice\PhpWord\Element\Footer|\PhpOffice\PhpWord\Element\Header $element + * @return self + */ + public function setElement($element) + { + $this->element = $element; + + return $this; + } } diff --git a/src/PhpWord/Writer/RTF/Part/SectionHeader.php b/src/PhpWord/Writer/RTF/Part/SectionHeader.php index e279ae07d9..c6b8f19d87 100644 --- a/src/PhpWord/Writer/RTF/Part/SectionHeader.php +++ b/src/PhpWord/Writer/RTF/Part/SectionHeader.php @@ -17,13 +17,10 @@ namespace PhpOffice\PhpWord\Writer\RTF\Part; -use PhpOffice\PhpWord\Element\Header; -use PhpOffice\PhpWord\Writer\RTF\Element\Container; - /** * RTF page header writer */ -class SectionHeader extends AbstractPart +class SectionHeader extends SectionFooter { /** * Root element name @@ -31,48 +28,4 @@ class SectionHeader extends AbstractPart * @var string */ protected $rootElement = '\header'; - - /** - * Footer/header element to be written - * - * @var \PhpOffice\PhpWord\Element\Header - */ - protected $element; - - /** - * Write part - * - * @return string - */ - public function write() - { - $content = '{'; - $content .= $this->rootElement; - $type = $this->element->getType(); - if ($type == Header::FIRST) { - $content .= 'f'; - } elseif ($type == Header::EVEN) { - $content .= 'r'; - } - - $containerWriter = new Container($this->getParentWriter(), $this->element); - $content .= $containerWriter->write(); - - $content .= '}' . PHP_EOL; - - return $content; - } - - /** - * Set element - * - * @param \PhpOffice\PhpWord\Element\Footer|\PhpOffice\PhpWord\Element\Header $element - * @return self - */ - public function setElement($element) - { - $this->element = $element; - - return $this; - } }