Skip to content

Commit 7d5816b

Browse files
authored
Merge pull request #2470 from PHPOffice/pr2225
Word2007 Reader/Writer: Permit book-fold printing
2 parents 8c23738 + 5d3ca3b commit 7d5816b

File tree

9 files changed

+89
-12
lines changed

9 files changed

+89
-12
lines changed

docs/changes/1.x/1.2.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Word2007 Reader : Support for table cell borders and margins by [@kernusr](https://github.com/kernusr) in GH-2454
1313
- 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)
1414
- 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)
15+
- 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)
1516

1617
### Bug fixes
1718

docs/usage/introduction.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,34 @@ Use mirror margins to set up facing pages for double-sided documents, such as bo
170170
$phpWord->getSettings()->setMirrorMargins(true);
171171
```
172172

173+
!!! note annotate "Don't forget to set both paper size and page size"
174+
175+
For example, to print a document on A4 paper (landscape) and fold it into A5 pages (portrait), use this section style:
176+
177+
``` php
178+
<?php
179+
180+
use PhpOffice\PhpWord\Shared\Converter;
181+
182+
$phpWord->getSettings()->setMirrorMargins(true);
183+
$phpWord->addSection([
184+
'paperSize' => 'A4',
185+
'orientation' => 'landscape',
186+
'pageSizeW' => Converter::cmToTwip(14.85),
187+
'pageSizeH' => Converter::cmToTwip(21),
188+
]);
189+
```
190+
191+
### Printing as folded booklet
192+
193+
Use book-fold printing to set up documents to be printed as foldable pages.
194+
195+
``` php
196+
<?php
197+
198+
$phpWord->getSettings()->setBookFoldPrinting(true);
199+
```
200+
173201
### Spelling and grammatical checks
174202

175203
By default spelling and grammatical errors are shown as soon as you open a word document.

docs/usage/writers.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ You can define options like :
3838
Options must be defined before creating the writer.
3939

4040
``` php
41+
<?php
42+
4143
use PhpOffice\PhpWord\Settings;
4244

4345
Settings::setPdfRendererOptions([

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,6 @@ parameters:
310310
count: 1
311311
path: src/PhpWord/Reader/Word2007/Settings.php
312312

313-
-
314-
message: "#^Property PhpOffice\\\\PhpWord\\\\Reader\\\\Word2007\\\\Settings\\:\\:\\$booleanProperties has no type specified\\.$#"
315-
count: 1
316-
path: src/PhpWord/Reader/Word2007/Settings.php
317-
318313
-
319314
message: "#^Parameter \\#1 \\$filename of function parse_ini_file expects string, string\\|false given\\.$#"
320315
count: 1

src/PhpWord/Metadata/Settings.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ class Settings
160160
*/
161161
private $doNotHyphenateCaps;
162162

163+
/**
164+
* Enable or disable book-folded printing.
165+
*
166+
* @var bool
167+
*/
168+
private $bookFoldPrinting = false;
169+
163170
/**
164171
* @return Protection
165172
*/
@@ -481,4 +488,16 @@ public function setDoNotHyphenateCaps($doNotHyphenateCaps): void
481488
{
482489
$this->doNotHyphenateCaps = (bool) $doNotHyphenateCaps;
483490
}
491+
492+
public function hasBookFoldPrinting(): bool
493+
{
494+
return $this->bookFoldPrinting;
495+
}
496+
497+
public function setBookFoldPrinting(bool $bookFoldPrinting): self
498+
{
499+
$this->bookFoldPrinting = $bookFoldPrinting;
500+
501+
return $this;
502+
}
484503
}

src/PhpWord/Reader/Word2007/Settings.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
*/
3131
class Settings extends AbstractPart
3232
{
33-
private static $booleanProperties = [
33+
/**
34+
* @var array<string>
35+
*/
36+
private $booleanProperties = [
3437
'mirrorMargins',
3538
'hideSpellingErrors',
3639
'hideGrammaticalErrors',
@@ -41,6 +44,7 @@ class Settings extends AbstractPart
4144
'updateFields',
4245
'autoHyphenation',
4346
'doNotHyphenateCaps',
47+
'bookFoldPrinting',
4448
];
4549

4650
/**
@@ -60,12 +64,8 @@ public function read(PhpWord $phpWord): void
6064
$value = $xmlReader->getAttribute('w:val', $node);
6165
$method = 'set' . $name;
6266

63-
if (in_array($name, $this::$booleanProperties)) {
64-
if ($value == 'false') {
65-
$docSettings->$method(false);
66-
} else {
67-
$docSettings->$method(true);
68-
}
67+
if (in_array($name, $this->booleanProperties)) {
68+
$docSettings->$method($value !== 'false');
6969
} elseif (method_exists($this, $method)) {
7070
$this->$method($xmlReader, $phpWord, $node);
7171
} elseif (method_exists($docSettings, $method)) {

src/PhpWord/Writer/Word2007/Part/Settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ private function getSettings(): void
151151
$this->setOnOffValue('w:updateFields', $documentSettings->hasUpdateFields());
152152
$this->setOnOffValue('w:autoHyphenation', $documentSettings->hasAutoHyphenation());
153153
$this->setOnOffValue('w:doNotHyphenateCaps', $documentSettings->hasDoNotHyphenateCaps());
154+
$this->setOnOffValue('w:bookFoldPrinting', $documentSettings->hasBookFoldPrinting());
154155

155156
$this->setThemeFontLang($documentSettings->getThemeFontLang());
156157
$this->setRevisionView($documentSettings->getRevisionView());

tests/PhpWordTests/Metadata/SettingsTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,19 @@ public function testDefaultDoNotHyphenateCaps(): void
225225
$oSettings = new Settings();
226226
self::assertNull($oSettings->hasDoNotHyphenateCaps());
227227
}
228+
229+
public function testBookFoldPrinting(): void
230+
{
231+
$oSettings = new Settings();
232+
self::assertInstanceOf(Settings::class, $oSettings->setBookFoldPrinting(true));
233+
self::assertTrue($oSettings->hasBookFoldPrinting());
234+
self::assertInstanceOf(Settings::class, $oSettings->setBookFoldPrinting(false));
235+
self::assertFalse($oSettings->hasBookFoldPrinting());
236+
}
237+
238+
public function testDefaultBookFoldPrinting(): void
239+
{
240+
$oSettings = new Settings();
241+
self::assertFalse($oSettings->hasBookFoldPrinting());
242+
}
228243
}

tests/PhpWordTests/Writer/Word2007/Part/SettingsTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,4 +468,20 @@ public function testDoNotHyphenateCaps(): void
468468
$element = $doc->getElement($path, $file);
469469
self::assertSame('true', $element->getAttribute('w:val'));
470470
}
471+
472+
public function testBookFoldPrinting(): void
473+
{
474+
$phpWord = new PhpWord();
475+
$phpWord->getSettings()->setBookFoldPrinting(true);
476+
477+
$doc = TestHelperDOCX::getDocument($phpWord);
478+
479+
$file = 'word/settings.xml';
480+
481+
$path = '/w:settings/w:bookFoldPrinting';
482+
self::assertTrue($doc->elementExists($path, $file));
483+
484+
$element = $doc->getElement($path, $file);
485+
self::assertSame('true', $element->getAttribute('w:val'));
486+
}
471487
}

0 commit comments

Comments
 (0)