Skip to content

Commit 1af6c6b

Browse files
committed
Add Support for Various Missing Features in HTML Writer
This PR supersedes PHPOffice#1814, which had become badly out of sync due to recent changes to the repository. Implement a number of features implemented in PhpWord, but not yet supported in PhpWord HTML Writer. 1. Use css @page and page declarations for sections. 2. Wrap sections in div, with page break before each (except first). 3. Add ability to specify generic fallback font for html (documentation change). 4. Add ability to specify handling of whitespace in html (documentation change). Currently, Word writer preserves space but HTML writer does not. 5. Support for Language, both for document overall and individual text elements. 6. Support for PageBreak for HTML (currently only PDF is supported). 7. Support for Table Border style, color, and size. 8. Support for empty paragraphs (Word writer permits, browsers generally suppress). 9. Default paragraph style should apply to all paragraphs, as well as class Normal. 10. Paragraph style should support line-height. 11. Paragraph style should support indentation. 12. Paragraph style should support page-break-before. 13. Paragraph style should not specify margin-top/bottom when spacing is null.
1 parent f195d28 commit 1af6c6b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1383
-171
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@
6161
"php": "^7.1|^8.0",
6262
"ext-dom": "*",
6363
"ext-json": "*",
64-
"ext-xml": "*",
65-
"laminas/laminas-escaper": ">=2.6"
64+
"ext-xml": "*"
6665
},
6766
"require-dev": {
6867
"ext-zip": "*",
@@ -74,7 +73,8 @@
7473
"phpunit/phpunit": ">=7.0",
7574
"tecnickcom/tcpdf": "^6.5",
7675
"symfony/process": "^4.4",
77-
"friendsofphp/php-cs-fixer": "^3.3"
76+
"friendsofphp/php-cs-fixer": "^3.3",
77+
"squizlabs/php_codesniffer": "^3.7"
7878
},
7979
"suggest": {
8080
"ext-zip": "Allows writing OOXML and ODF",

docs/general.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ default font by using the following two functions:
151151
$phpWord->setDefaultFontName('Times New Roman');
152152
$phpWord->setDefaultFontSize(12);
153153
154+
When generating html/pdf, you can alter the default handling of white space (normal),
155+
and/or supply a fallback generic font as follows:
156+
157+
.. code-block:: php
158+
159+
$phpWord->setDefaultHtmlGenericFont('serif');
160+
$phpWord->setDefaultHtmlWhiteSpace('pre-wrap');
161+
154162
Document settings
155163
-----------------
156164
Settings for the generated document can be set using ``$phpWord->getSettings()``

docs/styles.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ Available Font style options:
6464
See ``\PhpOffice\PhpWord\Style\Language`` class for some language codes.
6565
- ``position``. The text position, raised or lowered, in half points
6666
- ``hidden``. Hidden text, *true* or *false*.
67+
- ``htmlWhiteSpace``. How white space is handled when generating html/pdf. Possible values are *pre-wrap* and *normal* (other css values for white space are accepted, but are not expected to be useful).
68+
- ``htmlGenericFont``. Fallback generic font for html/pdf. Possible values are *sans-serif*, *serif*, and *monospace* (other css values for generic fonts are accepted).
6769

6870
.. _paragraph-style:
6971

phpunit.xml.dist

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1-
<phpunit backupGlobals="false"
2-
backupStaticAttributes="false"
3-
bootstrap="./tests/bootstrap.php"
4-
colors="true"
5-
convertErrorsToExceptions="true"
6-
convertNoticesToExceptions="true"
7-
convertWarningsToExceptions="true"
8-
processIsolation="false"
9-
stopOnFailure="false">
10-
<testsuites>
11-
<testsuite name="PhpWord Test Suite">
12-
<directory>./tests/PhpWordTests</directory>
13-
</testsuite>
14-
</testsuites>
15-
<filter>
16-
<whitelist>
17-
<directory suffix=".php">./src</directory>
18-
<exclude>
19-
<directory suffix=".php">./src/PhpWordTests/Shared/PCLZip</directory>
20-
</exclude>
21-
</whitelist>
22-
</filter>
1+
<?xml version="1.0"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="./tests/bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">./src</directory>
6+
</include>
7+
<exclude>
8+
<directory suffix=".php">./src/PhpWordTests/Shared/PCLZip</directory>
9+
</exclude>
10+
</coverage>
11+
<testsuites>
12+
<testsuite name="PhpWord Test Suite">
13+
<directory>./tests/PhpWordTests</directory>
14+
</testsuite>
15+
</testsuites>
2316
</phpunit>

src/PhpWord/Element/Cell.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Cell extends AbstractContainer
3232
/**
3333
* Cell width.
3434
*
35-
* @var int
35+
* @var null|int
3636
*/
3737
private $width;
3838

@@ -46,7 +46,7 @@ class Cell extends AbstractContainer
4646
/**
4747
* Create new instance.
4848
*
49-
* @param int $width
49+
* @param null|int $width
5050
* @param array|\PhpOffice\PhpWord\Style\Cell $style
5151
*/
5252
public function __construct($width = null, $style = null)

src/PhpWord/Element/Title.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function __construct($text, $depth = 1)
8080
/**
8181
* Get Title Text content.
8282
*
83-
* @return string
83+
* @return string|TextRun
8484
*/
8585
public function getText()
8686
{

src/PhpWord/PhpWord.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,68 @@ public function setDefaultFontName($fontName): void
256256
Settings::setDefaultFontName($fontName);
257257
}
258258

259+
/**
260+
* Default generic name for default font for html.
261+
*
262+
* @var string
263+
*/
264+
private $defaultHtmlGenericFont = '';
265+
266+
/**
267+
* Get generic name for default font for html.
268+
*
269+
* @return string
270+
*/
271+
public function getDefaultHtmlGenericFont()
272+
{
273+
return $this->defaultHtmlGenericFont;
274+
}
275+
276+
/**
277+
* Set generic name for default font for html.
278+
*
279+
* @param string $value
280+
*
281+
* @return bool
282+
*/
283+
public function setDefaultHtmlGenericFont($value)
284+
{
285+
$this->defaultHtmlGenericFont = \PhpOffice\PhpWord\Style\Font::validateGenericFont($value);
286+
287+
return '' !== $this->defaultHtmlGenericFont;
288+
}
289+
290+
/**
291+
* Default white space style for html.
292+
*
293+
* @var string
294+
*/
295+
private $defaultHtmlWhiteSpace = '';
296+
297+
/**
298+
* Get default white space style for html.
299+
*
300+
* @return string
301+
*/
302+
public function getDefaultHtmlWhiteSpace()
303+
{
304+
return $this->defaultHtmlWhiteSpace;
305+
}
306+
307+
/**
308+
* Set default white space style for html.
309+
*
310+
* @param string $value
311+
*
312+
* @return bool
313+
*/
314+
public function setDefaultHtmlWhiteSpace($value)
315+
{
316+
$this->defaultHtmlWhiteSpace = \PhpOffice\PhpWord\Style\Font::validateWhiteSpace($value);
317+
318+
return '' !== $this->defaultHtmlWhiteSpace;
319+
}
320+
259321
/**
260322
* Get default font size.
261323
*

src/PhpWord/Style/Border.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Border extends AbstractStyle
2525
/**
2626
* Border Top Size.
2727
*
28-
* @var float|int
28+
* @var null|float|int
2929
*/
3030
protected $borderTopSize;
3131

@@ -46,7 +46,7 @@ class Border extends AbstractStyle
4646
/**
4747
* Border Left Size.
4848
*
49-
* @var float|int
49+
* @var null|float|int
5050
*/
5151
protected $borderLeftSize;
5252

@@ -67,7 +67,7 @@ class Border extends AbstractStyle
6767
/**
6868
* Border Right Size.
6969
*
70-
* @var float|int
70+
* @var null|float|int
7171
*/
7272
protected $borderRightSize;
7373

@@ -88,7 +88,7 @@ class Border extends AbstractStyle
8888
/**
8989
* Border Bottom Size.
9090
*
91-
* @var float|int
91+
* @var null|float|int
9292
*/
9393
protected $borderBottomSize;
9494

@@ -124,7 +124,7 @@ public function getBorderSize()
124124
/**
125125
* Set border size.
126126
*
127-
* @param float|int $value
127+
* @param null|float|int $value
128128
*
129129
* @return self
130130
*/
@@ -205,7 +205,7 @@ public function setBorderStyle($value = null)
205205
/**
206206
* Get border top size.
207207
*
208-
* @return float|int
208+
* @return null|float|int
209209
*/
210210
public function getBorderTopSize()
211211
{
@@ -215,7 +215,7 @@ public function getBorderTopSize()
215215
/**
216216
* Set border top size.
217217
*
218-
* @param float|int $value
218+
* @param null|float|int $value
219219
*
220220
* @return self
221221
*/
@@ -287,7 +287,7 @@ public function getBorderLeftSize()
287287
/**
288288
* Set border left size.
289289
*
290-
* @param float|int $value
290+
* @param null|float|int $value
291291
*
292292
* @return self
293293
*/
@@ -349,7 +349,7 @@ public function setBorderLeftStyle($value = null)
349349
/**
350350
* Get border right size.
351351
*
352-
* @return float|int
352+
* @return null|float|int
353353
*/
354354
public function getBorderRightSize()
355355
{
@@ -421,7 +421,7 @@ public function setBorderRightStyle($value = null)
421421
/**
422422
* Get border bottom size.
423423
*
424-
* @return float|int
424+
* @return null|float|int
425425
*/
426426
public function getBorderBottomSize()
427427
{

0 commit comments

Comments
 (0)