Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add header/footer support for RTF writer #1714

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/PhpWord/Writer/RTF/Part/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,26 @@ 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
$sectionParts = array('Header', 'Footer');
foreach ($sectionParts as $sectionPart) {
$getFunction = 'get' . $sectionPart . 's';
$className = 'PhpOffice\\PhpWord\\Writer\\RTF\\Part\\Section' . $sectionPart;
$parts = $section->$getFunction();
foreach ($parts 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();

Expand Down
78 changes: 78 additions & 0 deletions src/PhpWord/Writer/RTF/Part/SectionFooter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2019 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

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 AbstractPart
{
/**
* Root element name
*
* @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;
}
}
31 changes: 31 additions & 0 deletions src/PhpWord/Writer/RTF/Part/SectionHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2019 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Writer\RTF\Part;

/**
* RTF page header writer
*/
class SectionHeader extends SectionFooter
{
/**
* Root element name
*
* @var string
*/
protected $rootElement = '\header';
}