Skip to content

Word2007 Reader/Writer: Permit book-fold printing #2470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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

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 @@ -310,11 +310,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'));
}
}