Skip to content

Commit 41a5b74

Browse files
committed
Make Default Paper Configurable
Each section currently has a hard-coded default paper of A4. It would make sense to allow the user to set this default, and specify it in a configuration file, just as is done with default font name and size.
1 parent 2d60f32 commit 41a5b74

File tree

6 files changed

+173
-2
lines changed

6 files changed

+173
-2
lines changed

docs/general.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ To turn it on set ``outputEscapingEnabled`` option to ``true`` in your PHPWord c
130130
131131
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
132132
133+
Default Paper
134+
~~~~~~~~~~~~~
135+
136+
By default, all sections of the document will print on A4 paper.
137+
You can alter the default paper by using the following function:
138+
139+
.. code-block:: php
140+
141+
\PhpOffice\PhpWord\Settings::setDefaultPaper('Letter');
142+
133143
Default font
134144
~~~~~~~~~~~~
135145

phpword.ini.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ outputEscapingEnabled = false
1414

1515
defaultFontName = Arial
1616
defaultFontSize = 10
17+
18+
[Paper]
19+
20+
defaultPaper = "A4"

src/PhpWord/Settings.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class Settings
7070
const DEFAULT_FONT_SIZE = 10;
7171
const DEFAULT_FONT_COLOR = '000000';
7272
const DEFAULT_FONT_CONTENT_TYPE = 'default'; // default|eastAsia|cs
73+
const DEFAULT_PAPER = 'A4';
7374

7475
/**
7576
* Compatibility option for XMLWriter
@@ -119,6 +120,12 @@ class Settings
119120
*/
120121
private static $defaultFontSize = self::DEFAULT_FONT_SIZE;
121122

123+
/**
124+
* Default paper
125+
* @var int
126+
*/
127+
private static $defaultPaper = self::DEFAULT_PAPER;
128+
122129
/**
123130
* The user defined temporary directory.
124131
*
@@ -432,6 +439,33 @@ public static function loadConfig($filename = null)
432439
return $config;
433440
}
434441

442+
/**
443+
* Get default paper
444+
*
445+
* @return string
446+
*/
447+
public static function getDefaultPaper()
448+
{
449+
return self::$defaultPaper;
450+
}
451+
452+
/**
453+
* Set default paper
454+
*
455+
* @param string $value
456+
* @return bool
457+
*/
458+
public static function setDefaultPaper($value)
459+
{
460+
if (is_string($value) && trim($value) !== '') {
461+
self::$defaultPaper = $value;
462+
463+
return true;
464+
}
465+
466+
return false;
467+
}
468+
435469
/**
436470
* Return the compatibility option used by the XMLWriter
437471
*

src/PhpWord/Style/Section.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace PhpOffice\PhpWord\Style;
1919

20+
use PhpOffice\PhpWord\Settings;
2021
use PhpOffice\PhpWord\SimpleType\VerticalJc;
2122

2223
/**
@@ -200,8 +201,11 @@ public function getPaperSize()
200201
* @param string $value
201202
* @return self
202203
*/
203-
public function setPaperSize($value = 'A4')
204+
public function setPaperSize($value = '')
204205
{
206+
if (!$value) {
207+
$value = Settings::getDefaultPaper();
208+
}
205209
if ($this->paper === null) {
206210
$this->paper = new Paper();
207211
}

tests/PhpWord/SettingsTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,45 @@
2525
*/
2626
class SettingsTest extends \PHPUnit\Framework\TestCase
2727
{
28+
private $compatibility;
29+
private $defaultFontSize;
30+
private $defaultFontName;
31+
private $defaultPaper;
32+
private $measurementUnit;
33+
private $outputEscapingEnabled;
34+
private $pdfRendererName;
35+
private $pdfRendererPath;
36+
private $tempDir;
37+
private $zipClass;
38+
39+
public function setUp()
40+
{
41+
$this->compatibility = Settings::hasCompatibility();
42+
$this->defaultFontSize = Settings::getDefaultFontSize();
43+
$this->defaultFontName = Settings::getDefaultFontName();
44+
$this->defaultPaper = Settings::getDefaultPaper();
45+
$this->measurementUnit = Settings::getMeasurementUnit();
46+
$this->outputEscapingEnabled = Settings::isOutputEscapingEnabled();
47+
$this->pdfRendererName = Settings::getPdfRendererName();
48+
$this->pdfRendererPath = Settings::getPdfRendererPath();
49+
$this->tempDir = Settings::getTempDir();
50+
$this->zipClass = Settings::getZipClass();
51+
}
52+
53+
public function tearDown()
54+
{
55+
Settings::setCompatibility($this->compatibility);
56+
Settings::setDefaultFontSize($this->defaultFontSize);
57+
Settings::setDefaultFontName($this->defaultFontName);
58+
Settings::setDefaultPaper($this->defaultPaper);
59+
Settings::setMeasurementUnit($this->measurementUnit);
60+
Settings::setOutputEscapingEnabled($this->outputEscapingEnabled);
61+
Settings::setPdfRendererName($this->pdfRendererName);
62+
Settings::setPdfRendererPath($this->pdfRendererPath);
63+
Settings::setTempDir($this->tempDir);
64+
Settings::setZipClass($this->zipClass);
65+
}
66+
2867
/**
2968
* Test set/get compatibity option
3069
*/
@@ -35,14 +74,28 @@ public function testSetGetCompatibility()
3574
$this->assertFalse(Settings::hasCompatibility());
3675
}
3776

77+
/**
78+
* Test set/get outputEscapingEnabled option
79+
*/
80+
public function testSetGetOutputEscapingEnabled()
81+
{
82+
$this->assertFalse(Settings::isOutputEscapingEnabled());
83+
Settings::setOutputEscapingEnabled(true);
84+
$this->assertTrue(Settings::isOutputEscapingEnabled());
85+
}
86+
3887
/**
3988
* Test set/get zip class
4089
*/
4190
public function testSetGetZipClass()
4291
{
92+
$this->assertEquals(Settings::ZIPARCHIVE, Settings::getZipClass());
93+
$this->assertFalse(Settings::setZipClass('foo'));
4394
$this->assertEquals(Settings::ZIPARCHIVE, Settings::getZipClass());
4495
$this->assertTrue(Settings::setZipClass(Settings::PCLZIP));
96+
$this->assertEquals(Settings::getZipClass(), Settings::PCLZIP);
4597
$this->assertFalse(Settings::setZipClass('foo'));
98+
$this->assertEquals(Settings::getZipClass(), Settings::PCLZIP);
4699
}
47100

48101
/**
@@ -57,16 +110,21 @@ public function testSetGetPdfRenderer()
57110
$this->assertEquals(Settings::PDF_RENDERER_DOMPDF, Settings::getPdfRendererName());
58111
$this->assertEquals($domPdfPath, Settings::getPdfRendererPath());
59112
$this->assertFalse(Settings::setPdfRendererPath('dummy/path'));
113+
$this->assertEquals($domPdfPath, Settings::getPdfRendererPath());
60114
}
61115

62116
/**
63117
* Test set/get measurement unit
64118
*/
65119
public function testSetGetMeasurementUnit()
66120
{
121+
$this->assertEquals(Settings::UNIT_TWIP, Settings::getMeasurementUnit());
122+
$this->assertFalse(Settings::setMeasurementUnit('foo'));
67123
$this->assertEquals(Settings::UNIT_TWIP, Settings::getMeasurementUnit());
68124
$this->assertTrue(Settings::setMeasurementUnit(Settings::UNIT_INCH));
125+
$this->assertEquals(Settings::UNIT_INCH, Settings::getMeasurementUnit());
69126
$this->assertFalse(Settings::setMeasurementUnit('foo'));
127+
$this->assertEquals(Settings::UNIT_INCH, Settings::getMeasurementUnit());
70128
}
71129

72130
/**
@@ -98,19 +156,50 @@ public function testTempDirCanBeSet()
98156
*/
99157
public function testSetGetDefaultFontName()
100158
{
159+
$this->assertEquals(Settings::DEFAULT_FONT_NAME, Settings::getDefaultFontName());
160+
$this->assertFalse(Settings::setDefaultFontName(' '));
101161
$this->assertEquals(Settings::DEFAULT_FONT_NAME, Settings::getDefaultFontName());
102162
$this->assertTrue(Settings::setDefaultFontName('Times New Roman'));
163+
$this->assertEquals('Times New Roman', Settings::getDefaultFontName());
103164
$this->assertFalse(Settings::setDefaultFontName(' '));
165+
$this->assertEquals('Times New Roman', Settings::getDefaultFontName());
104166
}
105167

106168
/**
107169
* Test set/get default font size
108170
*/
109171
public function testSetGetDefaultFontSize()
110172
{
173+
$this->assertEquals(Settings::DEFAULT_FONT_SIZE, Settings::getDefaultFontSize());
174+
$this->assertFalse(Settings::setDefaultFontSize(null));
111175
$this->assertEquals(Settings::DEFAULT_FONT_SIZE, Settings::getDefaultFontSize());
112176
$this->assertTrue(Settings::setDefaultFontSize(12));
177+
$this->assertEquals(12, Settings::getDefaultFontSize());
113178
$this->assertFalse(Settings::setDefaultFontSize(null));
179+
$this->assertEquals(12, Settings::getDefaultFontSize());
180+
}
181+
182+
/**
183+
* Test set/get default paper
184+
*/
185+
public function testSetGetDefaultPaper()
186+
{
187+
$dflt = Settings::DEFAULT_PAPER;
188+
$chng = ($dflt === 'A4') ? 'Letter' : 'A4';
189+
$doc = new PhpWord();
190+
$this->assertEquals($dflt, Settings::getDefaultPaper());
191+
$sec1 = $doc->addSection();
192+
$this->assertEquals($dflt, $sec1->getStyle()->getPaperSize());
193+
$this->assertFalse(Settings::setDefaultPaper(''));
194+
$this->assertEquals($dflt, Settings::getDefaultPaper());
195+
$this->assertTrue(Settings::setDefaultPaper($chng));
196+
$this->assertEquals($chng, Settings::getDefaultPaper());
197+
$sec2 = $doc->addSection();
198+
$this->assertEquals($chng, $sec2->getStyle()->getPaperSize());
199+
$sec3 = $doc->addSection(array('paperSize' => 'Legal'));
200+
$this->assertEquals('Legal', $sec3->getStyle()->getPaperSize());
201+
$this->assertFalse(Settings::setDefaultPaper(''));
202+
$this->assertEquals($chng, Settings::getDefaultPaper());
114203
}
115204

116205
/**
@@ -126,13 +215,24 @@ public function testLoadConfig()
126215
'defaultFontName' => 'Arial',
127216
'defaultFontSize' => 10,
128217
'outputEscapingEnabled' => false,
218+
'defaultPaper' => 'A4',
129219
);
130220

131221
// Test default value
132222
$this->assertEquals($expected, Settings::loadConfig());
133223

134224
// Test with valid file
135225
$this->assertEquals($expected, Settings::loadConfig(__DIR__ . '/../../phpword.ini.dist'));
226+
foreach ($expected as $key => $value) {
227+
if ($key === 'compatibility') {
228+
$meth = 'hasCompatibility';
229+
} elseif ($key === 'outputEscapingEnabled') {
230+
$meth = 'isOutputEscapingEnabled';
231+
} else {
232+
$meth = 'get' . ucfirst($key);
233+
}
234+
$this->assertEquals(Settings::$meth(), $value);
235+
}
136236

137237
// Test with invalid file
138238
$this->assertEmpty(Settings::loadConfig(__DIR__ . '/../../phpunit.xml.dist'));

tests/PhpWord/_includes/AbstractWebServerEmbeddedTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,26 @@ abstract class AbstractWebServerEmbeddedTest extends \PHPUnit\Framework\TestCase
2626
public static function setUpBeforeClass()
2727
{
2828
if (self::isBuiltinServerSupported()) {
29-
self::$httpServer = new Process('php -S localhost:8080 -t tests/PhpWord/_files');
29+
$commandLine = 'php -S localhost:8080 -t tests/PhpWord/_files';
30+
31+
/*
32+
* Make sure to invoke \Symfony\Component\Process\Process correctly
33+
* regardless of PHP version used.
34+
*
35+
* In Process version >= 5 / PHP >= 7.2.5, the constructor requires
36+
* an array, while in version < 3.3 / PHP < 5.5.9 it requires a string.
37+
* In between, it can accept both.
38+
*
39+
* Process::fromShellCommandLine() was introduced in version 4.2.0,
40+
* to enable recent versions of Process to parse a command string,
41+
* so if it is not available it means it is still possible to pass
42+
* a string to the constructor.
43+
*/
44+
if (method_exists('Symfony\Component\Process\Process', 'fromShellCommandLine')) {
45+
self::$httpServer = Process::fromShellCommandline($commandLine);
46+
} else {
47+
self::$httpServer = new Process($commandLine);
48+
}
3049
self::$httpServer->start();
3150
while (!self::$httpServer->isRunning()) {
3251
usleep(1000);

0 commit comments

Comments
 (0)