diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a9b172f..daaddb60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r - ODT Reader: Ability to read standard and custom document properties - @ivanlanin - Word2007 Writer: Enable the missing custom document properties writer - @ivanlanin - Image: Enable "image float left" - @ivanlanin GH-244 +- RTF Writer: Ability to write document properties - @ivanlanin ### Bugfixes diff --git a/docs/intro.rst b/docs/intro.rst index 3c6277f5..c33e778f 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -61,7 +61,7 @@ Writers +---------------------------+----------------------+--------+-------+-------+--------+-------+ | Features | | DOCX | ODT | RTF | HTML | PDF | +===========================+======================+========+=======+=======+========+=======+ -| **Document Properties** | Standard | ✓ | ✓ | | | | +| **Document Properties** | Standard | ✓ | ✓ | ✓ | ✓ | ✓ | +---------------------------+----------------------+--------+-------+-------+--------+-------+ | | Custom | ✓ | ✓ | | | | +---------------------------+----------------------+--------+-------+-------+--------+-------+ diff --git a/docs/src/documentation.md b/docs/src/documentation.md index ffa3edc3..03c495ab 100644 --- a/docs/src/documentation.md +++ b/docs/src/documentation.md @@ -78,7 +78,7 @@ Below are the supported features for each file formats. | Features | | DOCX | ODT | RTF | HTML | PDF | |-------------------------|--------------------|------|-----|-----|------|-----| -| **Document Properties** | Standard | ✓ | ✓ | | | | +| **Document Properties** | Standard | ✓ | ✓ | ✓ | ✓ | | | | Custom | ✓ | ✓ | | | | | **Element Type** | Text | ✓ | ✓ | ✓ | ✓ | ✓ | | | Text Run | ✓ | ✓ | ✓ | ✓ | ✓ | diff --git a/src/PhpWord/Writer/RTF.php b/src/PhpWord/Writer/RTF.php index 315e6007..aaf412de 100644 --- a/src/PhpWord/Writer/RTF.php +++ b/src/PhpWord/Writer/RTF.php @@ -19,11 +19,6 @@ namespace PhpOffice\PhpWord\Writer; use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Settings; -use PhpOffice\PhpWord\Shared\Drawing; -use PhpOffice\PhpWord\Style; -use PhpOffice\PhpWord\Style\Font; -use PhpOffice\PhpWord\Writer\RTF\Element\Container; /** * RTF writer @@ -32,20 +27,6 @@ use PhpOffice\PhpWord\Writer\RTF\Element\Container; */ class RTF extends AbstractWriter implements WriterInterface { - /** - * Color register - * - * @var array - */ - private $colorTable; - - /** - * Font register - * - * @var array - */ - private $fontTable; - /** * Last paragraph style * @@ -60,6 +41,18 @@ class RTF extends AbstractWriter implements WriterInterface public function __construct(PhpWord $phpWord = null) { $this->setPhpWord($phpWord); + + $this->parts = array('Header', 'Document'); + foreach ($this->parts as $partName) { + $partClass = get_class($this) . '\\Part\\' . $partName; + if (class_exists($partClass)) { + /** @var \PhpOffice\PhpWord\Writer\RTF\Part\AbstractPart $part Type hint */ + $part = new $partClass(); + $part->setParentWriter($this); + $this->writerParts[strtolower($partName)] = $part; + } + } + } /** @@ -70,10 +63,17 @@ class RTF extends AbstractWriter implements WriterInterface */ public function save($filename = null) { + $content = ''; $filename = $this->getTempFile($filename); $hFile = fopen($filename, 'w'); if ($hFile !== false) { - fwrite($hFile, $this->writeDocument()); + $content .= '{'; + $content .= '\rtf1' . PHP_EOL; + $content .= $this->getWriterPart('Header')->write(); + $content .= $this->getWriterPart('Document')->write(); + $content .= '}'; + + fwrite($hFile, $content); fclose($hFile); } else { throw new Exception("Can't open file"); @@ -81,20 +81,20 @@ class RTF extends AbstractWriter implements WriterInterface $this->cleanupTempFile(); } - /** - * Get color table - */ - public function getColorTable() - { - return $this->colorTable; - } - /** * Get font table */ public function getFontTable() { - return $this->fontTable; + return $this->getWriterPart('Header')->getFontTable(); + } + + /** + * Get color table + */ + public function getColorTable() + { + return $this->getWriterPart('Header')->getColorTable(); } /** @@ -114,172 +114,4 @@ class RTF extends AbstractWriter implements WriterInterface { $this->lastParagraphStyle = $value; } - - /** - * Get all data - * - * @return string - */ - private function writeDocument() - { - $this->fontTable = $this->populateFontTable(); - $this->colorTable = $this->populateColorTable(); - - // Set the default character set - $content = '{\rtf1'; - $content .= '\ansi\ansicpg1252'; // Set the default font (the first one) - $content .= '\deff0'; // Set the default tab size (720 twips) - $content .= '\deftab720'; - $content .= PHP_EOL; - - // Set the font tbl group - $content .= '{\fonttbl'; - foreach ($this->fontTable as $idx => $font) { - $content .= '{\f' . $idx . '\fnil\fcharset0 ' . $font . ';}'; - } - $content .= '}' . PHP_EOL; - - // Set the color tbl group - $content .= '{\colortbl '; - foreach ($this->colorTable as $color) { - $arrColor = Drawing::htmlToRGB($color); - $content .= ';\red' . $arrColor[0] . '\green' . $arrColor[1] . '\blue' . $arrColor[2] . ''; - } - $content .= ';}' . PHP_EOL; - - $content .= '{\*\generator PhpWord;}' . PHP_EOL; // Set the generator - $content .= '\viewkind4'; // Set the view mode of the document - $content .= '\uc1'; // Set the numberof bytes that follows a unicode character - $content .= '\pard'; // Resets to default paragraph properties. - $content .= '\nowidctlpar'; // No widow/orphan control - $content .= '\lang1036'; // Applies a language to a text run (1036 : French (France)) - $content .= '\kerning1'; // Point size (in half-points) above which to kern character pairs - $content .= '\fs' . (Settings::getDefaultFontSize() * 2); // Set the font size in half-points - $content .= PHP_EOL; - - // Body - $content .= $this->writeContent(); - - $content .= '}'; - - return $content; - } - - /** - * Get content data - * - * @return string - */ - private function writeContent() - { - $content = ''; - - $sections = $this->getPhpWord()->getSections(); - foreach ($sections as $section) { - $writer = new Container($this, $section); - $content .= $writer->write(); - } - - return $content; - } - - /** - * Get all fonts - * - * @return array - */ - private function populateFontTable() - { - $phpWord = $this->getPhpWord(); - $fontTable = array(); - $fontTable[] = Settings::getDefaultFontName(); - - // Browse styles - $styles = Style::getStyles(); - if (count($styles) > 0) { - foreach ($styles as $style) { - // Font - if ($style instanceof Font) { - if (in_array($style->getName(), $fontTable) == false) { - $fontTable[] = $style->getName(); - } - } - } - } - - // Search all fonts used - $sections = $phpWord->getSections(); - $countSections = count($sections); - if ($countSections > 0) { - foreach ($sections as $section) { - $elements = $section->getElements(); - foreach ($elements as $element) { - if (method_exists($element, 'getFontStyle')) { - $fontStyle = $element->getFontStyle(); - if ($fontStyle instanceof Font) { - if (in_array($fontStyle->getName(), $fontTable) == false) { - $fontTable[] = $fontStyle->getName(); - } - } - } - } - } - } - - return $fontTable; - } - - /** - * Get all colors - * - * @return array - */ - private function populateColorTable() - { - $phpWord = $this->getPhpWord(); - $defaultFontColor = Settings::DEFAULT_FONT_COLOR; - $colorTable = array(); - - // Browse styles - $styles = Style::getStyles(); - if (count($styles) > 0) { - foreach ($styles as $style) { - // Font - if ($style instanceof Font) { - $color = $style->getColor(); - $fgcolor = $style->getFgColor(); - if (!in_array($color, $colorTable) && $color != $defaultFontColor && !empty($color)) { - $colorTable[] = $color; - } - if (!in_array($fgcolor, $colorTable) && $fgcolor != $defaultFontColor && !empty($fgcolor)) { - $colorTable[] = $fgcolor; - } - } - } - } - - // Search all fonts used - $sections = $phpWord->getSections(); - $countSections = count($sections); - if ($countSections > 0) { - foreach ($sections as $section) { - $elements = $section->getElements(); - foreach ($elements as $element) { - if (method_exists($element, 'getFontStyle')) { - $fontStyle = $element->getFontStyle(); - if ($fontStyle instanceof Font) { - if (in_array($fontStyle->getColor(), $colorTable) == false) { - $colorTable[] = $fontStyle->getColor(); - } - if (in_array($fontStyle->getFgColor(), $colorTable) == false) { - $colorTable[] = $fontStyle->getFgColor(); - } - } - } - } - } - } - - return $colorTable; - } } diff --git a/src/PhpWord/Writer/RTF/Part/AbstractPart.php b/src/PhpWord/Writer/RTF/Part/AbstractPart.php new file mode 100644 index 00000000..3a73a226 --- /dev/null +++ b/src/PhpWord/Writer/RTF/Part/AbstractPart.php @@ -0,0 +1,68 @@ +parentWriter = $writer; + } + + /** + * Get parent writer + * + * @return \PhpOffice\PhpWord\Writer\AbstractWriter + * @throws \PhpOffice\PhpWord\Exception\Exception + */ + public function getParentWriter() + { + if ($this->parentWriter !== null) { + return $this->parentWriter; + } else { + throw new Exception('No parent WriterInterface assigned.'); + } + } +} diff --git a/src/PhpWord/Writer/RTF/Part/Document.php b/src/PhpWord/Writer/RTF/Part/Document.php new file mode 100644 index 00000000..71565044 --- /dev/null +++ b/src/PhpWord/Writer/RTF/Part/Document.php @@ -0,0 +1,142 @@ +writeInfo(); + $content .= $this->writeFormatting(); + $content .= $this->writeSections(); + + return $content; + } + + /** + * Write document information + * + * @return string + */ + private function writeInfo() + { + $docProps = $this->getParentWriter()->getPhpWord()->getDocumentProperties(); + $properties = array('title', 'subject', 'category', 'keywords', 'comment', + 'author', 'operator', 'creatim', 'revtim', 'company', 'manager'); + $mapping = array('comment' => 'description', 'author' => 'creator', 'operator' => 'lastModifiedBy', + 'creatim' => 'created', 'revtim' => 'modified'); + $dateFields = array('creatim', 'revtim'); + + $content = ''; + + $content .= '{'; + $content .= '\info'; + foreach ($properties as $property) { + $method = 'get' . (array_key_exists($property, $mapping) ? $mapping[$property] : $property); + $value = $docProps->$method(); + $value = in_array($property, $dateFields) ? $this->getDateValue($value) : $value; + $content .= "{\\{$property} {$value}}"; + } + $content .= '}'; + $content .= PHP_EOL; + + return $content; + } + + /** + * Write document formatting properties + * + * @return string + */ + private function writeFormatting() + { + $content = ''; + + $content .= '\deftab720'; // Set the default tab size (720 twips) + $content .= '\viewkind1'; // Set the view mode of the document + + $content .= '\uc1'; // Set the numberof bytes that follows a unicode character + $content .= '\pard'; // Resets to default paragraph properties. + $content .= '\nowidctlpar'; // No widow/orphan control + $content .= '\lang1036'; // Applies a language to a text run (1036 : French (France)) + $content .= '\kerning1'; // Point size (in half-points) above which to kern character pairs + $content .= '\fs' . (Settings::getDefaultFontSize() * 2); // Set the font size in half-points + $content .= PHP_EOL; + + return $content; + } + + /** + * Write sections + * + * @return string + */ + private function writeSections() + { + $content = ''; + + $sections = $this->getParentWriter()->getPhpWord()->getSections(); + foreach ($sections as $section) { + $writer = new Container($this->getParentWriter(), $section); + $content .= $writer->write(); + } + + return $content; + } + + /** + * Get date value + * + * The format of date value is `\yr?\mo?\dy?\hr?\min?\sec?` + * + * @param int $value + * @return string + */ + private function getDateValue($value) + { + $dateParts = array( + 'Y' => 'yr', + 'm' => 'mo', + 'd' => 'dy', + 'H' => 'hr', + 'i' => 'min', + 's' => 'sec', + ); + $result = ''; + foreach ($dateParts as $dateFormat => $controlWord) { + $result .= '\\' . $controlWord . date($dateFormat, $value); + } + + return $result; + } +} diff --git a/src/PhpWord/Writer/RTF/Part/Header.php b/src/PhpWord/Writer/RTF/Part/Header.php new file mode 100644 index 00000000..c401b500 --- /dev/null +++ b/src/PhpWord/Writer/RTF/Part/Header.php @@ -0,0 +1,227 @@ +fontTable; + } + + /** + * Get color table + */ + public function getColorTable() + { + return $this->colorTable; + } + + /** + * Write part + * + * @return string + */ + public function write() + { + $this->registerFont(); + + $content = ''; + + $content .= $this->writeCharset(); + $content .= $this->writeDefaults(); + $content .= $this->writeFontTable(); + $content .= $this->writeColorTable(); + $content .= $this->writeGenerator(); + $content .= PHP_EOL; + + return $content; + } + + /** + * Write character set + * + * @return string + */ + private function writeCharset() + { + $content = ''; + + $content .= '\ansi'; + $content .= '\ansicpg1252'; + $content .= PHP_EOL; + + return $content; + } + + /** + * Write header defaults + * + * @return string + */ + private function writeDefaults() + { + $content = ''; + + $content .= '\deff0'; + $content .= PHP_EOL; + + return $content; + } + + /** + * Write font table + * + * @return string + */ + private function writeFontTable() + { + $content = ''; + + $content .= '{'; + $content .= '\fonttbl'; + foreach ($this->fontTable as $index => $font) { + $content .= "{\\f{$index}\\fnil\\fcharset0{$font};}"; + } + $content .= '}'; + $content .= PHP_EOL; + + return $content; + } + + /** + * Write color table + * + * @return string + */ + private function writeColorTable() + { + $content = ''; + + $content .= '{'; + $content .= '\colortbl'; + foreach ($this->colorTable as $color) { + list($red, $green, $blue) = Drawing::htmlToRGB($color); + $content .= ";\\red{$red}\\green{$green}\\blue{$blue}"; + } + $content .= '}'; + $content .= PHP_EOL; + + return $content; + } + + /** + * Write + * + * @return string + */ + private function writeGenerator() + { + $content = ''; + + $content .= '{\*\generator PhpWord;}'; // Set the generator + $content .= PHP_EOL; + + return $content; + } + + /** + * Register all fonts and colors in both named and inline styles to appropriate header table + */ + private function registerFont() + { + $phpWord = $this->getParentWriter()->getPhpWord(); + $this->fontTable[] = Settings::getDefaultFontName(); + + // Search named styles + $styles = Style::getStyles(); + foreach ($styles as $style) { + $this->registerFontItems($style); + } + + // Search inline styles + $sections = $phpWord->getSections(); + foreach ($sections as $section) { + $elements = $section->getElements(); + foreach ($elements as $element) { + if (method_exists($element, 'getFontStyle')) { + $style = $element->getFontStyle(); + $this->registerFontItems($style); + } + } + } + } + + /** + * Register fonts and colors + * + * @param \PhpOffice\PhpWord\Style\AbstractStyle $style + */ + private function registerFontItems($style) + { + $defaultFont = Settings::getDefaultFontName(); + $defaultColor = Settings::DEFAULT_FONT_COLOR; + + if ($style instanceof Font) { + $this->registerFontItem($this->fontTable, $style->getName(), $defaultFont); + $this->registerFontItem($this->colorTable, $style->getColor(), $defaultColor); + $this->registerFontItem($this->colorTable, $style->getFgColor(), $defaultColor); + } + } + + /** + * Register individual font and color + * + * @param array $table + * @param string $value + * @param string $default + */ + private function registerFontItem(&$table, $value, $default) + { + if (in_array($value, $table) === false && $value !== null && $value != $default) { + $table[] = $value; + } + } +}