diff --git a/docs/changes/1.x/1.2.0.md b/docs/changes/1.x/1.2.0.md index 5b055ca7c0..a6b88ca93c 100644 --- a/docs/changes/1.x/1.2.0.md +++ b/docs/changes/1.x/1.2.0.md @@ -12,6 +12,7 @@ - Word2007 Reader : Support for table cell borders and margins by [@kernusr](https://github.com/kernusr) in GH-2454 - PDF Writer : Add config for defining the default font by [@MikeMaldini](https://github.com/MikeMaldini) in [#2262](https://github.com/PHPOffice/PHPWord/pull/2262) & [#2468](https://github.com/PHPOffice/PHPWord/pull/2468) - Word2007 Reader : Added support for Comments by [@shaedrich](https://github.com/shaedrich) in [#2161](https://github.com/PHPOffice/PHPWord/pull/2161) & [#2469](https://github.com/PHPOffice/PHPWord/pull/2469) +- 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) ### Bug fixes diff --git a/docs/usage/introduction.md b/docs/usage/introduction.md index 94a6fd915d..b3a101ab0d 100644 --- a/docs/usage/introduction.md +++ b/docs/usage/introduction.md @@ -170,6 +170,34 @@ Use mirror margins to set up facing pages for double-sided documents, such as bo $phpWord->getSettings()->setMirrorMargins(true); ``` +!!! note annotate "Don't forget to set both paper size and page size" + + For example, to print a document on A4 paper (landscape) and fold it into A5 pages (portrait), use this section style: + + ``` php + getSettings()->setMirrorMargins(true); + $phpWord->addSection([ + 'paperSize' => 'A4', + 'orientation' => 'landscape', + 'pageSizeW' => Converter::cmToTwip(14.85), + 'pageSizeH' => Converter::cmToTwip(21), + ]); + ``` + +### Printing as folded booklet + +Use book-fold printing to set up documents to be printed as foldable pages. + +``` php +getSettings()->setBookFoldPrinting(true); +``` + ### Spelling and grammatical checks By default spelling and grammatical errors are shown as soon as you open a word document. diff --git a/docs/usage/writers.md b/docs/usage/writers.md index 8610ef3cf0..684abeeeac 100644 --- a/docs/usage/writers.md +++ b/docs/usage/writers.md @@ -38,6 +38,8 @@ You can define options like : Options must be defined before creating the writer. ``` php +doNotHyphenateCaps = (bool) $doNotHyphenateCaps; } + + public function hasBookFoldPrinting(): bool + { + return $this->bookFoldPrinting; + } + + public function setBookFoldPrinting(bool $bookFoldPrinting): self + { + $this->bookFoldPrinting = $bookFoldPrinting; + + return $this; + } } diff --git a/src/PhpWord/Reader/Word2007/Settings.php b/src/PhpWord/Reader/Word2007/Settings.php index 63578595f5..7466008d49 100644 --- a/src/PhpWord/Reader/Word2007/Settings.php +++ b/src/PhpWord/Reader/Word2007/Settings.php @@ -30,7 +30,10 @@ */ class Settings extends AbstractPart { - private static $booleanProperties = [ + /** + * @var array + */ + private $booleanProperties = [ 'mirrorMargins', 'hideSpellingErrors', 'hideGrammaticalErrors', @@ -41,6 +44,7 @@ class Settings extends AbstractPart 'updateFields', 'autoHyphenation', 'doNotHyphenateCaps', + 'bookFoldPrinting', ]; /** @@ -60,12 +64,8 @@ public function read(PhpWord $phpWord): void $value = $xmlReader->getAttribute('w:val', $node); $method = 'set' . $name; - if (in_array($name, $this::$booleanProperties)) { - if ($value == 'false') { - $docSettings->$method(false); - } else { - $docSettings->$method(true); - } + if (in_array($name, $this->booleanProperties)) { + $docSettings->$method($value !== 'false'); } elseif (method_exists($this, $method)) { $this->$method($xmlReader, $phpWord, $node); } elseif (method_exists($docSettings, $method)) { diff --git a/src/PhpWord/Writer/Word2007/Part/Settings.php b/src/PhpWord/Writer/Word2007/Part/Settings.php index 2b126bb0fb..d1f6296912 100644 --- a/src/PhpWord/Writer/Word2007/Part/Settings.php +++ b/src/PhpWord/Writer/Word2007/Part/Settings.php @@ -151,6 +151,7 @@ private function getSettings(): void $this->setOnOffValue('w:updateFields', $documentSettings->hasUpdateFields()); $this->setOnOffValue('w:autoHyphenation', $documentSettings->hasAutoHyphenation()); $this->setOnOffValue('w:doNotHyphenateCaps', $documentSettings->hasDoNotHyphenateCaps()); + $this->setOnOffValue('w:bookFoldPrinting', $documentSettings->hasBookFoldPrinting()); $this->setThemeFontLang($documentSettings->getThemeFontLang()); $this->setRevisionView($documentSettings->getRevisionView()); diff --git a/tests/PhpWordTests/Metadata/SettingsTest.php b/tests/PhpWordTests/Metadata/SettingsTest.php index 48af68eab8..14f19f318d 100644 --- a/tests/PhpWordTests/Metadata/SettingsTest.php +++ b/tests/PhpWordTests/Metadata/SettingsTest.php @@ -225,4 +225,19 @@ public function testDefaultDoNotHyphenateCaps(): void $oSettings = new Settings(); self::assertNull($oSettings->hasDoNotHyphenateCaps()); } + + public function testBookFoldPrinting(): void + { + $oSettings = new Settings(); + self::assertInstanceOf(Settings::class, $oSettings->setBookFoldPrinting(true)); + self::assertTrue($oSettings->hasBookFoldPrinting()); + self::assertInstanceOf(Settings::class, $oSettings->setBookFoldPrinting(false)); + self::assertFalse($oSettings->hasBookFoldPrinting()); + } + + public function testDefaultBookFoldPrinting(): void + { + $oSettings = new Settings(); + self::assertFalse($oSettings->hasBookFoldPrinting()); + } } diff --git a/tests/PhpWordTests/Writer/Word2007/Part/SettingsTest.php b/tests/PhpWordTests/Writer/Word2007/Part/SettingsTest.php index e60e03f74f..db8f5198a4 100644 --- a/tests/PhpWordTests/Writer/Word2007/Part/SettingsTest.php +++ b/tests/PhpWordTests/Writer/Word2007/Part/SettingsTest.php @@ -468,4 +468,20 @@ public function testDoNotHyphenateCaps(): void $element = $doc->getElement($path, $file); self::assertSame('true', $element->getAttribute('w:val')); } + + public function testBookFoldPrinting(): void + { + $phpWord = new PhpWord(); + $phpWord->getSettings()->setBookFoldPrinting(true); + + $doc = TestHelperDOCX::getDocument($phpWord); + + $file = 'word/settings.xml'; + + $path = '/w:settings/w:bookFoldPrinting'; + self::assertTrue($doc->elementExists($path, $file)); + + $element = $doc->getElement($path, $file); + self::assertSame('true', $element->getAttribute('w:val')); + } }