Skip to content

Commit

Permalink
Word2007 Reader/Writer: Permit book-fold printing
Browse files Browse the repository at this point in the history
  • Loading branch information
potofcoffee authored and Progi1984 committed Sep 14, 2023
1 parent d94b00c commit 068bba3
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 12 deletions.
1 change: 1 addition & 0 deletions docs/changes/1.x/1.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- HTML Writer : Added border-spacing to default styles for table by [@kernusr](https://github.com/kernusr) in GH-2451
- 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/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

Expand Down
28 changes: 28 additions & 0 deletions docs/usage/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

use PhpOffice\PhpWord\Shared\Converter;

$phpWord->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
<?php

$phpWord->getSettings()->setBookFoldPrinting(true);
```

### Spelling and grammatical checks

By default spelling and grammatical errors are shown as soon as you open a word document.
Expand Down
2 changes: 2 additions & 0 deletions docs/usage/writers.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ You can define options like :
Options must be defined before creating the writer.

``` php
<?php

use PhpOffice\PhpWord\Settings;

Settings::setPdfRendererOptions([
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,6 @@ parameters:
count: 1
path: src/PhpWord/Reader/Word2007/Settings.php

-
message: "#^Property PhpOffice\\\\PhpWord\\\\Reader\\\\Word2007\\\\Settings\\:\\:\\$booleanProperties has no type specified\\.$#"
count: 1
path: src/PhpWord/Reader/Word2007/Settings.php

-
message: "#^Parameter \\#1 \\$filename of function parse_ini_file expects string, string\\|false given\\.$#"
count: 1
Expand Down
19 changes: 19 additions & 0 deletions src/PhpWord/Metadata/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ class Settings
*/
private $doNotHyphenateCaps;

/**
* Enable or disable book-folded printing.
*
* @var bool
*/
private $bookFoldPrinting = false;

/**
* @return Protection
*/
Expand Down Expand Up @@ -481,4 +488,16 @@ public function setDoNotHyphenateCaps($doNotHyphenateCaps): void
{
$this->doNotHyphenateCaps = (bool) $doNotHyphenateCaps;
}

public function hasBookFoldPrinting(): bool
{
return $this->bookFoldPrinting;
}

public function setBookFoldPrinting(bool $bookFoldPrinting): self
{
$this->bookFoldPrinting = $bookFoldPrinting;

return $this;
}
}
14 changes: 7 additions & 7 deletions src/PhpWord/Reader/Word2007/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
*/
class Settings extends AbstractPart
{
private static $booleanProperties = [
/**
* @var array<string>
*/
private $booleanProperties = [
'mirrorMargins',
'hideSpellingErrors',
'hideGrammaticalErrors',
Expand All @@ -41,6 +44,7 @@ class Settings extends AbstractPart
'updateFields',
'autoHyphenation',
'doNotHyphenateCaps',
'bookFoldPrinting',
];

/**
Expand All @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions src/PhpWord/Writer/Word2007/Part/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
15 changes: 15 additions & 0 deletions tests/PhpWordTests/Metadata/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
16 changes: 16 additions & 0 deletions tests/PhpWordTests/Writer/Word2007/Part/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}

0 comments on commit 068bba3

Please sign in to comment.