From 932d84cdf64eceb49bdcf018640d559af427a401 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Wed, 8 Nov 2023 13:56:15 +0100 Subject: [PATCH] Changes by @Progi1984 --- docs/changes/1.x/1.2.0.md | 24 ++- docs/usage/styles/font.md | 4 +- docs/usage/writers.md | 36 +++- src/PhpWord/PhpWord.php | 110 +++++----- src/PhpWord/Shared/Handler.php | 46 ---- src/PhpWord/Shared/Text.php | 8 +- src/PhpWord/Shared/Validate.php | 76 +++++++ src/PhpWord/Style/Font.php | 105 +++------ src/PhpWord/TemplateProcessor.php | 8 +- src/PhpWord/Writer/HTML.php | 96 +++++++-- src/PhpWord/Writer/HTML/Element/Link.php | 4 +- src/PhpWord/Writer/HTML/Element/ListItem.php | 2 +- src/PhpWord/Writer/HTML/Element/PageBreak.php | 4 +- src/PhpWord/Writer/HTML/Element/Text.php | 4 +- src/PhpWord/Writer/HTML/Element/Title.php | 2 +- src/PhpWord/Writer/HTML/Part/Body.php | 5 +- src/PhpWord/Writer/HTML/Part/Head.php | 6 +- src/PhpWord/Writer/HTML/Style/Font.php | 4 +- src/PhpWord/Writer/HTML/Style/Paragraph.php | 3 +- src/PhpWord/Writer/PDF.php | 10 +- src/PhpWord/Writer/PDF/TCPDF.php | 18 +- src/PhpWord/Writer/RTF/Part/Document.php | 2 - src/PhpWord/Writer/Word2007/Element/TOC.php | 8 +- .../Writer/Word2007/Element/TOC.php.bak | 202 ------------------ tests/PhpWordTests/Shared/HandlerTest.php | 79 ------- tests/PhpWordTests/Shared/ValidateTest.php | 76 +++++++ tests/PhpWordTests/Style/FontTest.php | 8 +- .../Writer/HTML/Element/PageBreakTest.php | 76 +++++++ tests/PhpWordTests/Writer/HTML/FontTest.php | 28 ++- tests/PhpWordTests/Writer/HTML/Helper.php | 4 +- .../Writer/HTML/ParagraphTest.php | 2 +- tests/PhpWordTests/Writer/HTMLTest.php | 24 ++- tests/PhpWordTests/Writer/PDF/MPDFTest.php | 54 +++-- tests/PhpWordTests/Writer/PDFTest.php | 2 +- .../Writer/Word2007/Element/TOCTest.php | 2 +- 35 files changed, 557 insertions(+), 585 deletions(-) delete mode 100644 src/PhpWord/Shared/Handler.php create mode 100644 src/PhpWord/Shared/Validate.php delete mode 100644 src/PhpWord/Writer/Word2007/Element/TOC.php.bak delete mode 100644 tests/PhpWordTests/Shared/HandlerTest.php create mode 100644 tests/PhpWordTests/Shared/ValidateTest.php create mode 100644 tests/PhpWordTests/Writer/HTML/Element/PageBreakTest.php diff --git a/docs/changes/1.x/1.2.0.md b/docs/changes/1.x/1.2.0.md index 16d16a47d0..8460aa2673 100644 --- a/docs/changes/1.x/1.2.0.md +++ b/docs/changes/1.x/1.2.0.md @@ -15,6 +15,24 @@ - Word2007 Reader/Writer: Permit book-fold printing by [@potofcoffee](https://github.com/potofcoffee) in [#2225](https://github.com/PHPOffice/PHPWord/pull/2225) & [#2470](https://github.com/PHPOffice/PHPWord/pull/2470) - Word2007 Writer : Add PageNumber to TOC by [@jet-desk](https://github.com/jet-desk) in [#1652](https://github.com/PHPOffice/PHPWord/pull/1652) & [#2471](https://github.com/PHPOffice/PHPWord/pull/2471) - Word2007 Reader/Writer + ODText Reader/Writer : Add Element Formula in by [@Progi1984](https://github.com/Progi1984) in [#2477](https://github.com/PHPOffice/PHPWord/pull/2477) +- Add Support for Various Missing Features in HTML Writer by [@oleibman](https://github.com/oleibman) in [#2475](https://github.com/PHPOffice/PHPWord/pull/2475) + - HTML Writer : Added ability to specify generic fallback font + - HTML Writer : Added ability to specify handling of whitespace + - PDF Writer : Added support for PageBreak + - PDF Writer : Added callback for modifying the HTML + ### Bug fixes @@ -41,4 +59,8 @@ - Bump phpunit/phpunit from 9.6.11 to 9.6.13 by [@dependabot](https://github.com/dependabot) in [#2481](https://github.com/PHPOffice/PHPWord/pull/2481) - Bump tecnickcom/tcpdf from 6.6.2 to 6.6.5 by [@dependabot](https://github.com/dependabot) in [#2482](https://github.com/PHPOffice/PHPWord/pull/2482) - Bump phpmd/phpmd from 2.13.0 to 2.14.1 by [@dependabot](https://github.com/dependabot) in [#2483](https://github.com/PHPOffice/PHPWord/pull/2483) -- Bump phpstan/phpstan-phpunit from 1.3.14 to 1.3.15 by [@dependabot](https://github.com/dependabot) in [#2494](https://github.com/PHPOffice/PHPWord/pull/2494) \ No newline at end of file +- Bump phpstan/phpstan-phpunit from 1.3.14 to 1.3.15 by [@dependabot](https://github.com/dependabot) in [#2494](https://github.com/PHPOffice/PHPWord/pull/2494) + + +### BC Breaks +- Removed dependency `laminas/laminas-escaper` \ No newline at end of file diff --git a/docs/usage/styles/font.md b/docs/usage/styles/font.md index 94b59b6500..921dc7fd85 100644 --- a/docs/usage/styles/font.md +++ b/docs/usage/styles/font.md @@ -24,5 +24,5 @@ Available Font style options: See ``\PhpOffice\PhpWord\Style\Language`` class for some language codes. - ``position``. The text position, raised or lowered, in half points - ``hidden``. Hidden text, *true* or *false*. -`htmlWhiteSpace``. How white space is handled when generating html/pdf. Possible values are *pre-wrap* and *normal* (other css values for white space are accepted, but are not expected to be useful). -- ``htmlGenericFont``. Fallback generic font for html/pdf. Possible values are *sans-serif*, *serif*, and *monospace* (other css values for generic fonts are accepted). +- ``whiteSpace``. How white space is handled when generating html/pdf. Possible values are *pre-wrap* and *normal* (other css values for white space are accepted, but are not expected to be useful). +- ``fallbackFont``. Fallback generic font for html/pdf. Possible values are *sans-serif*, *serif*, and *monospace* (other css values for generic fonts are accepted). diff --git a/docs/usage/writers.md b/docs/usage/writers.md index f68008bf87..81fb2b99b4 100644 --- a/docs/usage/writers.md +++ b/docs/usage/writers.md @@ -11,12 +11,13 @@ $writer->save(__DIR__ . '/sample.html'); ``` -When generating html/pdf, you can alter the default handling of white space (normal), -and/or supply a fallback generic font as follows: +When generating html/pdf, you can alter the default handling of white space (normal), and/or supply a fallback generic font as follows: ```php - $phpWord->setDefaultHtmlGenericFont('serif'); - $phpWord->setDefaultHtmlWhiteSpace('pre-wrap'); +$writer = IOFactory::createWriter($oPhpWord, 'HTML'); +$writer->setDefaultGenericFont('serif'); +$writer->setDefaultWhiteSpace('pre-wrap'); +$writer->save(__DIR__ . '/sample.html'); ``` ## ODText @@ -39,6 +40,33 @@ $writer = IOFactory::createWriter($oPhpWord, 'PDF'); $writer->save(__DIR__ . '/sample.pdf'); ``` +To generate a PDF, the PhpWord object passes through HTML before generating the PDF. +This HTML can be modified using a callback. + +``` php +setEditCallback('cbEditHTML'); +$writer->save(__DIR__ . '/sample.pdf'); + +/** + * Add a meta tag generator + */ +function cbEditHTML(string $inputHTML): string +{ + $beforeBody = ''; + $needle = ''; + + $pos = strpos($inputHTML, $needle); + if ($pos !== false) { + $inputHTML = (string) substr_replace($inputHTML, "$beforeBody\n$needle", $pos, strlen($needle)); + } + + return $inputHTML; +} +``` + ### Options You can define options like : diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index 489119052b..da57f38d29 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -256,68 +256,6 @@ public function setDefaultFontName($fontName): void Settings::setDefaultFontName($fontName); } - /** - * Default generic name for default font for html. - * - * @var string - */ - private $defaultHtmlGenericFont = ''; - - /** - * Get generic name for default font for html. - * - * @return string - */ - public function getDefaultHtmlGenericFont() - { - return $this->defaultHtmlGenericFont; - } - - /** - * Set generic name for default font for html. - * - * @param string $value - * - * @return bool - */ - public function setDefaultHtmlGenericFont($value) - { - $this->defaultHtmlGenericFont = \PhpOffice\PhpWord\Style\Font::validateGenericFont($value); - - return '' !== $this->defaultHtmlGenericFont; - } - - /** - * Default white space style for html. - * - * @var string - */ - private $defaultHtmlWhiteSpace = ''; - - /** - * Get default white space style for html. - * - * @return string - */ - public function getDefaultHtmlWhiteSpace() - { - return $this->defaultHtmlWhiteSpace; - } - - /** - * Set default white space style for html. - * - * @param string $value - * - * @return bool - */ - public function setDefaultHtmlWhiteSpace($value) - { - $this->defaultHtmlWhiteSpace = \PhpOffice\PhpWord\Style\Font::validateWhiteSpace($value); - - return '' !== $this->defaultHtmlWhiteSpace; - } - /** * Get default font size. * @@ -387,4 +325,52 @@ public function save($filename, $format = 'Word2007', $download = false) return true; } + + /** + * Create new section. + * + * @deprecated 0.10.0 + * + * @param array $settings + * + * @return \PhpOffice\PhpWord\Element\Section + * + * @codeCoverageIgnore + */ + public function createSection($settings = null) + { + return $this->addSection($settings); + } + + /** + * Get document properties object. + * + * @deprecated 0.12.0 + * + * @return \PhpOffice\PhpWord\Metadata\DocInfo + * + * @codeCoverageIgnore + */ + public function getDocumentProperties() + { + return $this->getDocInfo(); + } + + /** + * Set document properties object. + * + * @deprecated 0.12.0 + * + * @param \PhpOffice\PhpWord\Metadata\DocInfo $documentProperties + * + * @return self + * + * @codeCoverageIgnore + */ + public function setDocumentProperties($documentProperties) + { + $this->metadata['Document'] = $documentProperties; + + return $this; + } } diff --git a/src/PhpWord/Shared/Handler.php b/src/PhpWord/Shared/Handler.php deleted file mode 100644 index 72232cc9e6..0000000000 --- a/src/PhpWord/Shared/Handler.php +++ /dev/null @@ -1,46 +0,0 @@ - $this->getStyleName(), 'basic' => [ 'name' => $this->getName(), @@ -323,11 +336,9 @@ public function getStyleValues() 'rtl' => $this->isRTL(), 'shading' => $this->getShading(), 'lang' => $this->getLang(), - $hws => $this->getHtmlWhiteSpace(), - $hgf => $this->getHtmlGenericFont(), + 'whiteSpace' => $this->getWhiteSpace(), + 'fallbackFont' => $this->getFallbackFont(), ]; - - return $styles; } /** @@ -953,46 +964,16 @@ public function setPosition($value = null) return $this; } - /** - * Preservation of white space in html. - * - * @var string Value used for css white-space - */ - private $htmlWhiteSpace = ''; - - /** - * Validate html css white-space value. It is expected that only pre-wrap and normal (default) are useful. - * - * @param string $value Should be one of pre-wrap, normal, nowrap, pre, pre-line, initial, inherit - * - * @return string value if valid, null string if not - */ - public static function validateWhiteSpace($value) - { - switch ($value) { - case 'pre-wrap': - case 'normal': - case 'nowrap': - case 'pre': - case 'pre-line': - case 'initial': - case 'inherit': - return $value; - default: - return ''; - } - } - /** * Set html css white-space value. It is expected that only pre-wrap and normal (default) are useful. * - * @param string $value Should be one of pre-wrap, normal, nowrap, pre, pre-line, initial, inherit + * @param string|null $value Should be one of pre-wrap, normal, nowrap, pre, pre-line, initial, inherit * * @return self */ - public function setHtmlWhiteSpace($value) + public function setWhiteSpace(?string $value): self { - $this->htmlWhiteSpace = self::validateWhiteSpace($value); + $this->whiteSpace = Validate::validateCSSWhiteSpace($value); return $this; } @@ -1002,41 +983,9 @@ public function setHtmlWhiteSpace($value) * * @return string */ - public function getHtmlWhiteSpace() + public function getWhiteSpace(): string { - return $this->htmlWhiteSpace; - } - - /** - * Generic font as fallback for html. - * - * @var string generic font name - */ - private $htmlGenericFont = ''; - - /** - * Validate generic font for fallback for html. - * - * @param string $value generic font name - * - * @return string value if legitimate, null string if not - */ - public static function validateGenericFont($value) - { - switch ($value) { - case 'serif': - case 'sans-serif': - case 'monospace': - case 'cursive': - case 'fantasy': - case 'system-ui': - case 'math': - case 'emoji': - case 'fangsong': - return $value; - default: - return ''; - } + return $this->whiteSpace; } /** @@ -1046,9 +995,9 @@ public static function validateGenericFont($value) * * @return self */ - public function setHtmlGenericFont($value) + public function setFallbackFont(?string $value): self { - $this->htmlGenericFont = self::validateGenericFont($value); + $this->fallbackFont = Validate::validateCSSGenericFont($value); return $this; } @@ -1058,8 +1007,8 @@ public function setHtmlGenericFont($value) * * @return string */ - public function getHtmlGenericFont() + public function getFallbackFont(): string { - return $this->htmlGenericFont; + return $this->fallbackFont; } } diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 5624c93560..f740212a2c 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -138,13 +138,15 @@ public function __construct($documentTemplate) public function __destruct() { - if ($this->zipClass !== null) { + // ZipClass + if ($this->zipClass) { try { $this->zipClass->close(); } catch (Throwable $e) { // Nothing to do here. } } + // Temporary file if ($this->tempDocumentFilename && file_exists($this->tempDocumentFilename)) { unlink($this->tempDocumentFilename); } @@ -279,7 +281,7 @@ protected static function ensureMacroCompleted($macro) */ protected static function ensureUtf8Encoded($subject) { - return (null !== $subject) ? Text::toUTF8($subject) : ''; + return $subject ? Text::toUTF8($subject) : ''; } /** @@ -454,7 +456,7 @@ private function chooseImageDimension($baseValue, $inlineValue, $defaultValue) if (null === $value && isset($inlineValue)) { $value = $inlineValue; } - if (!preg_match('/^([0-9]*(cm|mm|in|pt|pc|px|%|em|ex|)|auto)$/i', $value ?? '')) { + if (!preg_match('/^([0-9\.]*(cm|mm|in|pt|pc|px|%|em|ex|)|auto)$/i', $value ?? '')) { $value = null; } if (null === $value) { diff --git a/src/PhpWord/Writer/HTML.php b/src/PhpWord/Writer/HTML.php index ca9784a83b..c8789686e5 100644 --- a/src/PhpWord/Writer/HTML.php +++ b/src/PhpWord/Writer/HTML.php @@ -18,6 +18,9 @@ namespace PhpOffice\PhpWord\Writer; use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Settings; +use PhpOffice\PhpWord\Shared\Validate; +use PhpOffice\PhpWord\Style\Font; /** * HTML writer. @@ -35,13 +38,6 @@ class HTML extends AbstractWriter implements WriterInterface */ protected $isPdf = false; - /** - * Is the current writer creating TCPDF? - * - * @var bool - */ - protected $isTcpdf = false; - /** * Footnotes and endnotes collection. * @@ -54,7 +50,21 @@ class HTML extends AbstractWriter implements WriterInterface * * @var null|callable */ - private $editHtmlCallback; + private $editCallback; + + /** + * Default generic name for default font for html. + * + * @var string + */ + private $defaultGenericFont = ''; + + /** + * Default white space style for html. + * + * @var string + */ + private $defaultWhiteSpace = ''; /** * Create new instance. @@ -115,7 +125,9 @@ public function getContent() $content .= $this->getWriterPart('Head')->write(); $content .= $this->getWriterPart('Body')->write(); $content .= '' . PHP_EOL; - $callback = $this->editHtmlCallback; + + // Trigger a callback for editing the entire HTML + $callback = $this->editCallback; if ($callback !== null) { $content = $callback($content); } @@ -129,9 +141,9 @@ public function getContent() * The callback must accept the HTML as string as first parameter, * and it must return the edited HTML as string. */ - public function setEditHtmlCallback(?callable $callback): void + public function setEditCallback(?callable $callback): void { - $this->editHtmlCallback = $callback; + $this->editCallback = $callback; } /** @@ -144,16 +156,6 @@ public function isPdf() return $this->isPdf; } - /** - * Get is TCPDF. - * - * @return bool - */ - public function isTcpdf() - { - return $this->isTcpdf; - } - /** * Get notes. * @@ -175,14 +177,62 @@ public function addNote($noteId, $noteMark): void $this->notes[$noteId] = $noteMark; } + /** + * Get generic name for default font for html. + * + * @return string + */ + public function getDefaultGenericFont(): string + { + return $this->defaultGenericFont; + } + + /** + * Set generic name for default font for html. + * + * @param string $value + * + * @return self + */ + public function setDefaultGenericFont(string $value): self + { + $this->defaultGenericFont = Validate::validateCSSGenericFont($value); + + return $this; + } + + /** + * Get default white space style for html. + * + * @return string + */ + public function getDefaultWhiteSpace(): string + { + return $this->defaultWhiteSpace; + } + + /** + * Set default white space style for html. + * + * @param string $value + * + * @return self + */ + public function setDefaultWhiteSpace(string $value): self + { + $this->defaultWhiteSpace = Validate::validateCSSWhiteSpace($value); + + return $this; + } + /** * Escape string or not depending on setting. * * @param string $txt */ - public static function escapeOrNot($txt): string + public function escapeHTML(string $txt): string { - if (\PhpOffice\PhpWord\Settings::isOutputEscapingEnabled()) { + if (Settings::isOutputEscapingEnabled()) { return htmlspecialchars($txt, ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8'); } diff --git a/src/PhpWord/Writer/HTML/Element/Link.php b/src/PhpWord/Writer/HTML/Element/Link.php index 5ff7030118..ac48c865bd 100644 --- a/src/PhpWord/Writer/HTML/Element/Link.php +++ b/src/PhpWord/Writer/HTML/Element/Link.php @@ -40,9 +40,9 @@ public function write() $prefix = $this->element->isInternal() ? '#' : ''; $content = $this->writeOpening(); $content .= "element->getSource()) + . $this->parentWriter->escapeHTML($this->element->getSource()) . '">' - . HTML::escapeOrNot($this->element->getText()) + . $this->parentWriter->escapeHTML($this->element->getText()) . ''; $content .= $this->writeClosing(); diff --git a/src/PhpWord/Writer/HTML/Element/ListItem.php b/src/PhpWord/Writer/HTML/Element/ListItem.php index 4dd61ff321..ddc3ecf0fd 100644 --- a/src/PhpWord/Writer/HTML/Element/ListItem.php +++ b/src/PhpWord/Writer/HTML/Element/ListItem.php @@ -37,7 +37,7 @@ public function write() return ''; } - $content = '

' . HTML::escapeOrNot($this->element->getTextObject()->getText()) . '

' . PHP_EOL; + $content = '

' . $this->parentWriter->escapeHTML($this->element->getTextObject()->getText()) . '

' . PHP_EOL; return $content; } diff --git a/src/PhpWord/Writer/HTML/Element/PageBreak.php b/src/PhpWord/Writer/HTML/Element/PageBreak.php index 8e3971781c..e5c48cc789 100644 --- a/src/PhpWord/Writer/HTML/Element/PageBreak.php +++ b/src/PhpWord/Writer/HTML/Element/PageBreak.php @@ -17,6 +17,8 @@ namespace PhpOffice\PhpWord\Writer\HTML\Element; +use PhpOffice\PhpWord\Writer\PDF\TCPDF; + /** * PageBreak element HTML writer. * @@ -35,7 +37,7 @@ public function write() { /** @var \PhpOffice\PhpWord\Writer\HTML $parentWriter Type hint */ $parentWriter = $this->parentWriter; - if ($parentWriter->isTcpdf()) { + if ($parentWriter instanceof TCPDF) { return '
'; } if ($parentWriter->isPdf()) { diff --git a/src/PhpWord/Writer/HTML/Element/Text.php b/src/PhpWord/Writer/HTML/Element/Text.php index 35aa00d2d8..0b908cd3d0 100644 --- a/src/PhpWord/Writer/HTML/Element/Text.php +++ b/src/PhpWord/Writer/HTML/Element/Text.php @@ -75,7 +75,7 @@ public function write() $content .= $this->writeOpening(); $content .= $this->openingText; $content .= $this->openingTags; - $contenx = HTML::escapeOrNot($element->getText()); + $contenx = $this->parentWriter->escapeHTML($element->getText()); if (!$this->withoutP && !trim(/** @scrutinizer ignore-type */ $contenx)) { $contenx = ' '; } @@ -139,7 +139,7 @@ protected function writeClosing() $content .= $this->writeTrackChangeClosing(); if (!$this->withoutP) { - $content .= HTML::escapeOrNot($this->closingText); + $content .= $this->parentWriter->escapeHTML($this->closingText); $content .= '

' . PHP_EOL; } diff --git a/src/PhpWord/Writer/HTML/Element/Title.php b/src/PhpWord/Writer/HTML/Element/Title.php index c46249f373..65e6cb090b 100644 --- a/src/PhpWord/Writer/HTML/Element/Title.php +++ b/src/PhpWord/Writer/HTML/Element/Title.php @@ -41,7 +41,7 @@ public function write() $text = $this->element->getText(); if (is_string($text)) { - $text = HTML::escapeOrNot($text); + $text = $this->parentWriter->escapeHTML($text); } else { $writer = new Container($this->parentWriter, $text); $text = $writer->write(); diff --git a/src/PhpWord/Writer/HTML/Part/Body.php b/src/PhpWord/Writer/HTML/Part/Body.php index fe25df5f90..e5e2a5b80f 100644 --- a/src/PhpWord/Writer/HTML/Part/Body.php +++ b/src/PhpWord/Writer/HTML/Part/Body.php @@ -19,6 +19,7 @@ use PhpOffice\PhpWord\Writer\HTML\Element\Container; use PhpOffice\PhpWord\Writer\HTML\Element\TextRun as TextRunWriter; +use PhpOffice\PhpWord\Writer\PDF\TCPDF; /** * RTF body part writer. @@ -41,10 +42,10 @@ public function write() $content .= '' . PHP_EOL; $sections = $phpWord->getSections(); $secno = 0; - $tcpdf = $this->getParentWriter()->isTcpdf(); + $isTCPDFWriter = $this->getParentWriter() instanceof TCPDF; foreach ($sections as $section) { ++$secno; - if ($tcpdf && $secno > 1) { + if ($isTCPDFWriter && $secno > 1) { $content .= "
" . PHP_EOL; } else { $content .= "
" . PHP_EOL; diff --git a/src/PhpWord/Writer/HTML/Part/Head.php b/src/PhpWord/Writer/HTML/Part/Head.php index b8daf4dab2..dc8c0b2016 100644 --- a/src/PhpWord/Writer/HTML/Part/Head.php +++ b/src/PhpWord/Writer/HTML/Part/Head.php @@ -67,7 +67,7 @@ public function write() if ($docProps->$method() != '') { $content .= '' . PHP_EOL; } @@ -89,12 +89,12 @@ private function writeStyles() // Default styles $astarray = [ - 'font-family' => FontStyleWriter::getFontFamily(Settings::getDefaultFontName(), $this->getParentWriter()->getPhpWord()->getDefaultHtmlGenericFont()), + 'font-family' => FontStyleWriter::getFontFamily(Settings::getDefaultFontName(), $this->getParentWriter()->getDefaultGenericFont()), 'font-size' => Settings::getDefaultFontSize() . 'pt', ]; // Mpdf sometimes needs separate tag for body; doesn't harm others. $bodyarray = $astarray; - $hws = $this->getParentWriter()->getPhpWord()->getDefaultHtmlWhiteSpace(); + $hws = $this->getParentWriter()->getDefaultWhiteSpace(); if ($hws) { $astarray['white-space'] = $hws; } diff --git a/src/PhpWord/Writer/HTML/Style/Font.php b/src/PhpWord/Writer/HTML/Style/Font.php index 5aead50c07..b2115578db 100644 --- a/src/PhpWord/Writer/HTML/Style/Font.php +++ b/src/PhpWord/Writer/HTML/Style/Font.php @@ -39,7 +39,7 @@ public function write() } $css = []; - $font = self::getFontFamily($style->getName(), $style->getHtmlGenericFont()); + $font = self::getFontFamily($style->getName(), $style->getFallbackFont()); $size = $style->getSize(); $color = $style->getColor(); $fgColor = $style->getFgColor(); @@ -61,7 +61,7 @@ public function write() $css['text-transform'] = $this->getValueIf($style->isAllCaps(), 'uppercase'); $css['font-variant'] = $this->getValueIf($style->isSmallCaps(), 'small-caps'); $css['display'] = $this->getValueIf($style->isHidden(), 'none'); - $whitespace = $style->getHtmlWhiteSpace(); + $whitespace = $style->getWhiteSpace(); if ($whitespace) { $css['white-space'] = $whitespace; } diff --git a/src/PhpWord/Writer/HTML/Style/Paragraph.php b/src/PhpWord/Writer/HTML/Style/Paragraph.php index 73286bda8b..105883a4a5 100644 --- a/src/PhpWord/Writer/HTML/Style/Paragraph.php +++ b/src/PhpWord/Writer/HTML/Style/Paragraph.php @@ -95,10 +95,9 @@ public function write() } $ind = $style->getIndentation(); if ($ind != null) { - $tcpdf = $this->getParentWriter()->isTcpdf(); $left = $ind->getLeft(); $inches = $left * 1.0 / \PhpOffice\PhpWord\Shared\Converter::INCH_TO_TWIP; - $css[$tcpdf ? 'text-indent' : 'margin-left'] = ((string) $inches) . 'in'; + $css[$this->getParentWriter() instanceof TCPDF ? 'text-indent' : 'margin-left'] = ((string) $inches) . 'in'; $left = $ind->getRight(); $inches = $left * 1.0 / \PhpOffice\PhpWord\Shared\Converter::INCH_TO_TWIP; $css['margin-right'] = ((string) $inches) . 'in'; diff --git a/src/PhpWord/Writer/PDF.php b/src/PhpWord/Writer/PDF.php index b24e66efb8..8cd52e6884 100644 --- a/src/PhpWord/Writer/PDF.php +++ b/src/PhpWord/Writer/PDF.php @@ -19,6 +19,7 @@ use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Writer\PDF\AbstractRenderer; use PhpOffice\PhpWord\Settings; /** @@ -71,11 +72,16 @@ public function __call($name, $arguments) // throw new Exception("PDF Rendering library has not been defined."); // } - return call_user_func_array([$this->renderer, $name], $arguments); + return call_user_func_array([$this->getRenderer(), $name], $arguments); } public function save(string $filename): void { - $this->renderer->save($filename); + $this->getRenderer()->save($filename); + } + + public function getRenderer(): AbstractRenderer + { + return $this->renderer; } } diff --git a/src/PhpWord/Writer/PDF/TCPDF.php b/src/PhpWord/Writer/PDF/TCPDF.php index 5ef92f451b..3a8206ce22 100644 --- a/src/PhpWord/Writer/PDF/TCPDF.php +++ b/src/PhpWord/Writer/PDF/TCPDF.php @@ -21,6 +21,7 @@ use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Writer\WriterInterface; +use TCPDF as TCPDFBase; /** * TCPDF writer. @@ -38,17 +39,6 @@ class TCPDF extends AbstractRenderer implements WriterInterface */ protected $includeFile = 'tcpdf.php'; - /** - * Overridden to set isTcpdf. - * - * @codeCoverageIgnore - */ - public function __construct(PhpWord $phpWord) - { - parent::__construct($phpWord); - $this->isTcpdf = true; - } - /** * Gets the implementation of external PDF library that should be used. * @@ -56,11 +46,11 @@ public function __construct(PhpWord $phpWord) * @param string $unit Unit measure * @param string $paperSize Paper size * - * @return \TCPDF implementation + * @return TCPDFBase implementation */ protected function createExternalWriterInstance($orientation, $unit, $paperSize) { - $instance = new \TCPDF($orientation, $unit, $paperSize); + $instance = new TCPDFBase($orientation, $unit, $paperSize); if ($this->getFont()) { $instance->setFont($this->getFont(), $instance->getFontStyle(), $instance->getFontSizePt()); @@ -77,7 +67,7 @@ protected function createExternalWriterInstance($orientation, $unit, $paperSize) * between paragaraphs when the user has * explicitly set those values to numeric in default style. */ - protected function prepareToWrite(\TCPDF $pdf): void + protected function prepareToWrite(TCPDFBase $pdf): void { $pdf->AddPage(); $customStyles = Style::getStyles(); diff --git a/src/PhpWord/Writer/RTF/Part/Document.php b/src/PhpWord/Writer/RTF/Part/Document.php index 1d55d43fa1..a0002583a9 100644 --- a/src/PhpWord/Writer/RTF/Part/Document.php +++ b/src/PhpWord/Writer/RTF/Part/Document.php @@ -203,8 +203,6 @@ private function writeSections() $elementWriter = new Container($this->getParentWriter(), $section); $content .= $elementWriter->write(); - - //$content .= '\sect' . PHP_EOL; } return $content; diff --git a/src/PhpWord/Writer/Word2007/Element/TOC.php b/src/PhpWord/Writer/Word2007/Element/TOC.php index f7d00c14c8..2cf76155e4 100644 --- a/src/PhpWord/Writer/Word2007/Element/TOC.php +++ b/src/PhpWord/Writer/Word2007/Element/TOC.php @@ -71,7 +71,7 @@ private function writeTitle(XMLWriter $xmlWriter, TOCElement $element, Title $ti $fontStyle = $element->getStyleFont(); $isObject = ($fontStyle instanceof Font) ? true : false; $rId = $title->getRelationId(); - $indent = ($title->getDepth() - 1) * $tocStyle->getIndent(); + $indent = (int) (($title->getDepth() - 1) * $tocStyle->getIndent()); $xmlWriter->startElement('w:p'); @@ -93,8 +93,10 @@ private function writeTitle(XMLWriter $xmlWriter, TOCElement $element, Title $ti $styleWriter->write(); } $xmlWriter->startElement('w:t'); + $titleText = $title->getText(); $this->writeText(is_string($titleText) ? $titleText : ''); + $xmlWriter->endElement(); // w:t $xmlWriter->endElement(); // w:r @@ -142,10 +144,8 @@ private function writeTitle(XMLWriter $xmlWriter, TOCElement $element, Title $ti /** * Write style. - * - * @param float|int $indent */ - private function writeStyle(XMLWriter $xmlWriter, TOCElement $element, $indent): void + private function writeStyle(XMLWriter $xmlWriter, TOCElement $element, int $indent): void { $tocStyle = $element->getStyleTOC(); $fontStyle = $element->getStyleFont(); diff --git a/src/PhpWord/Writer/Word2007/Element/TOC.php.bak b/src/PhpWord/Writer/Word2007/Element/TOC.php.bak deleted file mode 100644 index a86199bf23..0000000000 --- a/src/PhpWord/Writer/Word2007/Element/TOC.php.bak +++ /dev/null @@ -1,202 +0,0 @@ -getXmlWriter(); - $element = $this->getElement(); - if (!$element instanceof TOCElement) { - return; - } - - $titles = $element->getTitles(); - $writeFieldMark = true; - - foreach ($titles as $title) { - $this->writeTitle($xmlWriter, $element, $title, $writeFieldMark); - if ($writeFieldMark) { - $writeFieldMark = false; - } - } - - $xmlWriter->startElement('w:p'); - $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:fldChar'); - $xmlWriter->writeAttribute('w:fldCharType', 'end'); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - } - - /** - * Write title. - * - * @param \PhpOffice\PhpWord\Element\Title $title - * @param bool $writeFieldMark - */ - private function writeTitle(XMLWriter $xmlWriter, TOCElement $element, $title, $writeFieldMark): void - {echo "here2\n"; - $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'); - $titleText = $title->getText(); - $this->writeText(is_string($titleText) ? $titleText : ''); - $xmlWriter->endElement(); // w:t - $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->text("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 - } - - /** - * Write style. - * - * @param float|int $indent - */ - private function writeStyle(XMLWriter $xmlWriter, TOCElement $element, $indent): void - {echo "here3\n"; - $tocStyle = $element->getStyleTOC(); - $fontStyle = $element->getStyleFont(); - $isObject = ($fontStyle instanceof Font) ? true : false; - - $xmlWriter->startElement('w:pPr'); - - // Paragraph - if ($isObject && null !== $fontStyle->getParagraph()) { - $styleWriter = new ParagraphStyleWriter($xmlWriter, $fontStyle->getParagraph()); - $styleWriter->write(); - } - - // Font - if (!empty($fontStyle) && !$isObject) { - $xmlWriter->startElement('w:rPr'); - $xmlWriter->startElement('w:rStyle'); - $xmlWriter->writeAttribute('w:val', $fontStyle); - $xmlWriter->endElement(); - $xmlWriter->endElement(); // w:rPr - } - - // Tab - $xmlWriter->startElement('w:tabs'); - $styleWriter = new TabStyleWriter($xmlWriter, $tocStyle); - $styleWriter->write(); - $xmlWriter->endElement(); - - // Indent - if ($indent > 0) { - $xmlWriter->startElement('w:ind'); - $xmlWriter->writeAttribute('w:left', $indent); - $xmlWriter->endElement(); - } - - $xmlWriter->endElement(); // w:pPr - } - - /** - * Write TOC Field. - */ - private function writeFieldMark(XMLWriter $xmlWriter, TOCElement $element): void - {echo "here4\n"; - $minDepth = $element->getMinDepth(); - $maxDepth = $element->getMaxDepth(); - - $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->text("TOC \\o {$minDepth}-{$maxDepth} \\h \\z \\u"); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - - $xmlWriter->startElement('w:r'); - $xmlWriter->startElement('w:fldChar'); - $xmlWriter->writeAttribute('w:fldCharType', 'separate'); - $xmlWriter->endElement(); - $xmlWriter->endElement(); - } -} diff --git a/tests/PhpWordTests/Shared/HandlerTest.php b/tests/PhpWordTests/Shared/HandlerTest.php deleted file mode 100644 index 4aa051d3ce..0000000000 --- a/tests/PhpWordTests/Shared/HandlerTest.php +++ /dev/null @@ -1,79 +0,0 @@ -getMessage()); - } - } - } - - public function testNotice(): void - { - try { - Handler::notice('invalidtz'); - self::fail('Expected error/exception did not happen'); - } catch (Throwable $e) { - self::assertStringContainsString('Timezone', $e->getMessage()); - } - } - - public function testWarning(): void - { - try { - Handler::warning(); - self::fail('Expected error/exception did not happen'); - } catch (Throwable $e) { - self::assertStringContainsString('ailed to open stream', $e->getMessage()); - } - } - - public function testUserDeprecated(): void - { - try { - Handler::userDeprecated(); - self::fail('Expected error/exception did not happen'); - } catch (Throwable $e) { - self::assertStringContainsString('hello', $e->getMessage()); - } - } - - public function testUserNotice(): void - { - try { - Handler::userNotice(); - self::fail('Expected error/exception did not happen'); - } catch (Throwable $e) { - self::assertStringContainsString('userNotice', $e->getMessage()); - } - } - - public function testUserWarning(): void - { - try { - Handler::userWarning(); - self::fail('Expected error/exception did not happen'); - } catch (Throwable $e) { - self::assertStringContainsString('userWarning', $e->getMessage()); - } - } -} diff --git a/tests/PhpWordTests/Shared/ValidateTest.php b/tests/PhpWordTests/Shared/ValidateTest.php new file mode 100644 index 0000000000..b9d5549cec --- /dev/null +++ b/tests/PhpWordTests/Shared/ValidateTest.php @@ -0,0 +1,76 @@ +assertEquals($expected, Validate::validateCSSGenericFont($value)); + } + + public static function providerCSSGenericFont(): iterable + { + $data = []; + // Valid data + foreach(Validate::CSS_GENERICFONT as $value) { + $data[] = [ + $value, + $value, + ]; + } + // Invalid data + $data[] = ['invalidData', '']; + $data[] = ['', '']; + $data[] = [null, '']; + + return $data; + } + + /** + * @dataProvider providerCSSWhiteSpace + */ + public function testValidateCSSWhiteSpace(?string $value, string $expected): void + { + $this->assertEquals($expected, Validate::validateCSSWhiteSpace($value)); + } + + public static function providerCSSWhiteSpace(): iterable + { + $data = []; + // Valid data + foreach(Validate::CSS_WHITESPACE as $value) { + $data[] = [ + $value, + $value, + ]; + } + // Invalid data + $data[] = ['invalidData', '']; + $data[] = ['', '']; + $data[] = [null, '']; + + return $data; + } +} diff --git a/tests/PhpWordTests/Style/FontTest.php b/tests/PhpWordTests/Style/FontTest.php index e2d7d4f582..e11b8604ac 100644 --- a/tests/PhpWordTests/Style/FontTest.php +++ b/tests/PhpWordTests/Style/FontTest.php @@ -79,8 +79,8 @@ public function testSetStyleValueWithNullOrEmpty(): void 'kerning' => null, 'lang' => null, 'hidden' => false, - 'htmlWhiteSpace' => '', - 'htmlGenericFont' => '', + 'whiteSpace' => '', + 'fallbackFont' => '', ]; foreach ($attributes as $key => $default) { $get = is_bool($default) ? "is{$key}" : "get{$key}"; @@ -123,8 +123,8 @@ public function testSetStyleValueNormal(): void 'noProof' => true, 'lang' => new Language(Language::EN_US), 'hidden' => true, - 'htmlWhiteSpace' => 'pre-wrap', - 'htmlGenericFont' => 'serif', + 'whiteSpace' => 'pre-wrap', + 'fallbackFont' => 'serif', ]; $object->setStyleByArray($attributes); foreach ($attributes as $key => $value) { diff --git a/tests/PhpWordTests/Writer/HTML/Element/PageBreakTest.php b/tests/PhpWordTests/Writer/HTML/Element/PageBreakTest.php new file mode 100644 index 0000000000..1cf2208bd2 --- /dev/null +++ b/tests/PhpWordTests/Writer/HTML/Element/PageBreakTest.php @@ -0,0 +1,76 @@ + 
'. PHP_EOL, $object->write()); + } + + public function testMPDF(): void + { + $rendererName = Settings::PDF_RENDERER_MPDF; + $rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/mpdf/mpdf'); + Settings::setPdfRenderer($rendererName, $rendererLibraryPath); + $writer = new PDF(new PhpWord()); + + $object = new PageBreak($writer->getRenderer(), new BasePageBreak()); + + self::assertEquals('', $object->write()); + } + + public function testDOMPDF(): void + { + $rendererName = Settings::PDF_RENDERER_DOMPDF; + $rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/dompdf/dompdf'); + Settings::setPdfRenderer($rendererName, $rendererLibraryPath); + $writer = new PDF(new PhpWord()); + + $object = new PageBreak($writer->getRenderer(), new BasePageBreak()); + + self::assertEquals('', $object->write()); + } + + public function testTCPDF(): void + { + $rendererName = Settings::PDF_RENDERER_TCPDF; + $rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/tecnickcom/tcpdf'); + Settings::setPdfRenderer($rendererName, $rendererLibraryPath); + $writer = new PDF(new PhpWord()); + + $object = new PageBreak($writer->getRenderer(), new BasePageBreak()); + + self::assertEquals('
', $object->write()); + } +} diff --git a/tests/PhpWordTests/Writer/HTML/FontTest.php b/tests/PhpWordTests/Writer/HTML/FontTest.php index cffcd6c7de..442c2639c9 100644 --- a/tests/PhpWordTests/Writer/HTML/FontTest.php +++ b/tests/PhpWordTests/Writer/HTML/FontTest.php @@ -113,9 +113,9 @@ public function testFontNames2(): void $phpWord->setDefaultFontName('Courier New'); $phpWord->setDefaultFontSize(12); $phpWord->addFontStyle('style1', ['name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true]); - $phpWord->addFontStyle('style2', ['name' => 'Arial', 'size' => 10, 'htmlGenericFont' => 'sans-serif']); - $phpWord->addFontStyle('style3', ['name' => 'DejaVu Sans Monospace', 'size' => 10, 'htmlGenericFont' => 'monospace']); - $phpWord->addFontStyle('style4', ['name' => 'Arial', 'size' => 10, 'htmlGenericFont' => 'invalid']); + $phpWord->addFontStyle('style2', ['name' => 'Arial', 'size' => 10, 'fallbackFont' => 'sans-serif']); + $phpWord->addFontStyle('style3', ['name' => 'DejaVu Sans Monospace', 'size' => 10, 'fallbackFont' => 'monospace']); + $phpWord->addFontStyle('style4', ['name' => 'Arial', 'size' => 10, 'fallbackFont' => 'invalid']); $section1 = $phpWord->addSection(); $section1->addText('Default font'); $section1->addText('Tahoma', 'style1'); @@ -159,11 +159,10 @@ public function testFontNames3(): void $phpWord = new PhpWord(); $phpWord->setDefaultFontName('Courier New'); $phpWord->setDefaultFontSize(12); - $phpWord->setDefaultHtmlGenericFont('monospace'); $phpWord->addFontStyle('style1', ['name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true]); - $phpWord->addFontStyle('style2', ['name' => 'Arial', 'size' => 10, 'htmlGenericFont' => 'sans-serif']); - $phpWord->addFontStyle('style3', ['name' => 'DejaVu Sans Monospace', 'size' => 10, 'htmlGenericFont' => 'monospace']); - $phpWord->addFontStyle('style4', ['name' => 'Arial', 'size' => 10, 'htmlGenericFont' => 'invalid']); + $phpWord->addFontStyle('style2', ['name' => 'Arial', 'size' => 10, 'fallbackFont' => 'sans-serif']); + $phpWord->addFontStyle('style3', ['name' => 'DejaVu Sans Monospace', 'size' => 10, 'fallbackFont' => 'monospace']); + $phpWord->addFontStyle('style4', ['name' => 'Arial', 'size' => 10, 'fallbackFont' => 'invalid']); $section1 = $phpWord->addSection(); $section1->addText('Default font'); $section1->addText('Tahoma', 'style1'); @@ -171,7 +170,7 @@ public function testFontNames3(): void $section1->addText('DejaVu Sans Monospace', 'style3'); $section1->addText('Arial with invalid fallback', 'style4'); - $dom = Helper::getAsHTML($phpWord); + $dom = Helper::getAsHTML($phpWord, '', 'monospace'); $xpath = new DOMXPath($dom); self::assertEmpty(Helper::getNamedItem($xpath, '/html/body/div/p[1]', 'class')); @@ -205,12 +204,11 @@ public function testFontNames3(): void public function testWhiteSpace(): void { $phpWord = new PhpWord(); - $phpWord->setDefaultHtmlWhiteSpace('pre-wrap'); $phpWord->setDefaultFontSize(12); - $phpWord->addFontStyle('style1', ['name' => 'Courier New', 'size' => 10, 'htmlWhiteSpace' => 'pre-wrap']); - $phpWord->addFontStyle('style2', ['name' => 'Courier New', 'size' => 10, 'htmlWhiteSpace' => 'invalid']); - $phpWord->addFontStyle('style3', ['name' => 'Courier New', 'size' => 10, 'htmlWhiteSpace' => 'normal']); - $phpWord->addFontStyle('style4', ['name' => 'Courier New', 'size' => 10, 'htmlWhiteSpace' => 'invalid']); + $phpWord->addFontStyle('style1', ['name' => 'Courier New', 'size' => 10, 'whiteSpace' => 'pre-wrap']); + $phpWord->addFontStyle('style2', ['name' => 'Courier New', 'size' => 10, 'whiteSpace' => 'invalid']); + $phpWord->addFontStyle('style3', ['name' => 'Courier New', 'size' => 10, 'whiteSpace' => 'normal']); + $phpWord->addFontStyle('style4', ['name' => 'Courier New', 'size' => 10, 'whiteSpace' => 'invalid']); $text = 'This is a long line which will be split over 2 lines with pre-wrap'; $section1 = $phpWord->addSection(); $section1->addText($text); @@ -219,7 +217,7 @@ public function testWhiteSpace(): void $section1->addText($text, 'style3'); $section1->addText($text, 'style4'); - $dom = Helper::getAsHTML($phpWord); + $dom = Helper::getAsHTML($phpWord, 'pre-wrap'); $xpath = new DOMXPath($dom); $style = Helper::getTextContent($xpath, '/html/head/style'); @@ -245,7 +243,7 @@ public function testWhiteSpace(): void public function testInline(): void { $phpWord = new PhpWord(); - $style1 = ['name' => 'Courier New', 'size' => 10, 'htmlWhiteSpace' => 'pre-wrap']; + $style1 = ['name' => 'Courier New', 'size' => 10, 'whiteSpace' => 'pre-wrap']; $style2 = ['name' => 'Verdana', 'size' => 8.5]; $text = 'This is a paragraph.'; $section1 = $phpWord->addSection(); diff --git a/tests/PhpWordTests/Writer/HTML/Helper.php b/tests/PhpWordTests/Writer/HTML/Helper.php index ccd542a1c6..b777d4be14 100644 --- a/tests/PhpWordTests/Writer/HTML/Helper.php +++ b/tests/PhpWordTests/Writer/HTML/Helper.php @@ -84,9 +84,11 @@ public static function getLength(DOMXPath $xpath, string $query): int return $returnVal; } - public static function getAsHTML(PhpWord $phpWord): DOMDocument + public static function getAsHTML(PhpWord $phpWord, string $defaultWhiteSpace = '', string $defaultGenericFont = ''): DOMDocument { $htmlWriter = new HTML($phpWord); + $htmlWriter->setDefaultWhiteSpace($defaultWhiteSpace); + $htmlWriter->setDefaultGenericFont($defaultGenericFont); $dom = new DOMDocument(); $dom->loadHTML($htmlWriter->getContent()); diff --git a/tests/PhpWordTests/Writer/HTML/ParagraphTest.php b/tests/PhpWordTests/Writer/HTML/ParagraphTest.php index 42c338cdb8..dc306df978 100644 --- a/tests/PhpWordTests/Writer/HTML/ParagraphTest.php +++ b/tests/PhpWordTests/Writer/HTML/ParagraphTest.php @@ -67,7 +67,7 @@ public function testParagraphAndFontStyles(): void $phpWord->addParagraphStyle('indented', [ 'indentation' => ['left' => 0.50 * Converter::INCH_TO_TWIP, 'right' => 0.60 * Converter::INCH_TO_TWIP], ]); - $phpWord->addFontStyle('style1', ['name' => 'Courier New', 'size' => 10, 'htmlWhiteSpace' => 'pre-wrap', 'htmlGenericFont' => 'monospace']); + $phpWord->addFontStyle('style1', ['name' => 'Courier New', 'size' => 10, 'whiteSpace' => 'pre-wrap', 'fallbackFont' => 'monospace']); $text = 'This is a paragraph. It should be long enough to show the effects of indentation on both the right and left sides.'; $section1 = $phpWord->addSection(); $section1->addText($text, 'style1', $pstyle1); diff --git a/tests/PhpWordTests/Writer/HTMLTest.php b/tests/PhpWordTests/Writer/HTMLTest.php index 8303570e92..fbf5fd1a0e 100644 --- a/tests/PhpWordTests/Writer/HTMLTest.php +++ b/tests/PhpWordTests/Writer/HTMLTest.php @@ -37,7 +37,7 @@ public function testConstruct(): void { $object = new HTML(new PhpWord()); - self::assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $object->getPhpWord()); + self::assertInstanceOf(PhpWord::class, $object->getPhpWord()); } /** @@ -51,6 +51,28 @@ public function testConstructWithNull(): void $object->getPhpWord(); } + public function testDefaultGenericFont(): void + { + $object = new HTML(new PhpWord()); + + self::assertEquals('', $object->getDefaultGenericFont()); + self::assertInstanceOf(HTML::class, $object->setDefaultGenericFont('test')); + self::assertEquals('', $object->getDefaultGenericFont()); + self::assertInstanceOf(HTML::class, $object->setDefaultGenericFont('cursive')); + self::assertEquals('cursive', $object->getDefaultGenericFont()); + } + + public function testDefaultWhiteSpace(): void + { + $object = new HTML(new PhpWord()); + + self::assertEquals('', $object->getDefaultWhiteSpace()); + self::assertInstanceOf(HTML::class, $object->setDefaultWhiteSpace('test')); + self::assertEquals('', $object->getDefaultWhiteSpace()); + self::assertInstanceOf(HTML::class, $object->setDefaultWhiteSpace('pre-line')); + self::assertEquals('pre-line', $object->getDefaultWhiteSpace()); + } + /** * Save. */ diff --git a/tests/PhpWordTests/Writer/PDF/MPDFTest.php b/tests/PhpWordTests/Writer/PDF/MPDFTest.php index 9dbbf9d5ac..0fe53456ba 100644 --- a/tests/PhpWordTests/Writer/PDF/MPDFTest.php +++ b/tests/PhpWordTests/Writer/PDF/MPDFTest.php @@ -47,9 +47,6 @@ public function testConstruct(): void $section->addText('Section 2 - landscape'); $writer = new MPDF($phpWord); - /** @var callable */ - $callback = [self::class, 'editContent']; - $writer->setEditHtmlCallback($callback); $writer->save($file); self::assertFileExists($file); @@ -57,23 +54,33 @@ public function testConstruct(): void unlink($file); } - /** - * Test set/get abstract renderer options. - */ - public function testSetGetAbstractRendererOptions(): void + public function testEditCallback(): void { - $rendererName = Settings::PDF_RENDERER_MPDF; - $rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/mpdf/mpdf'); - Settings::setPdfRenderer($rendererName, (string) $rendererLibraryPath); - Settings::setPdfRendererOptions([ - 'font' => 'Arial', - ]); - $writer = new PDF(new PhpWord()); - self::assertEquals('Arial', $writer->getFont()); + $file = __DIR__ . '/../../_files/mpdf.pdf'; + + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + $section->addText('Test 1'); + $section->addPageBreak(); + $section->addText('Test 2'); + $oSettings = new \PhpOffice\PhpWord\Style\Section(); + $oSettings->setSettingValue('orientation', 'landscape'); + $section = $phpWord->addSection($oSettings); // @phpstan-ignore-line + $section->addText('Section 2 - landscape'); + + $writer = new MPDF($phpWord); + /** @var callable */ + $callback = [self::class, 'cbEditContent']; + $writer->setEditCallback($callback); + $writer->save($file); + + self::assertFileExists($file); + + unlink($file); } // add a footer - public static function editContent(string $html): string + public static function cbEditContent(string $html): string { $afterBody = '
{PAGENO}
' . MPDF::SIMULATED_BODY_START; $beforeBody = ''; @@ -90,4 +97,19 @@ public static function editContent(string $html): string return $html; } + + /** + * Test set/get abstract renderer options. + */ + public function testSetGetAbstractRendererOptions(): void + { + $rendererName = Settings::PDF_RENDERER_MPDF; + $rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/mpdf/mpdf'); + Settings::setPdfRenderer($rendererName, $rendererLibraryPath); + Settings::setPdfRendererOptions([ + 'font' => 'Arial', + ]); + $writer = new PDF(new PhpWord()); + self::assertEquals('Arial', $writer->getFont()); + } } diff --git a/tests/PhpWordTests/Writer/PDFTest.php b/tests/PhpWordTests/Writer/PDFTest.php index 5a2883feed..fc4064a23f 100644 --- a/tests/PhpWordTests/Writer/PDFTest.php +++ b/tests/PhpWordTests/Writer/PDFTest.php @@ -38,7 +38,7 @@ public function testConstruct(): void $rendererName = Settings::PDF_RENDERER_DOMPDF; $rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/dompdf/dompdf'); - Settings::setPdfRenderer($rendererName, (string) $rendererLibraryPath); + Settings::setPdfRenderer($rendererName, $rendererLibraryPath); $writer = new PDF(new PhpWord()); $writer->save($file); diff --git a/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php b/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php index c87379678d..537fb93d1a 100644 --- a/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Element/TOCTest.php @@ -31,7 +31,7 @@ class TOCTest extends \PHPUnit\Framework\TestCase */ protected function tearDown(): void { - //TestHelperDOCX::clear(); + TestHelperDOCX::clear(); } public function testWriteTitlePageNumber(): void