Skip to content

Commit 82ec8e2

Browse files
committed
Add Support for Various Missing Features in HTML Writer
This PR supersedes PHPOffice#1814 and PHPOffice#2343, 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 a836c32 commit 82ec8e2

File tree

103 files changed

+2971
-645
lines changed

Some content is hidden

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

103 files changed

+2971
-645
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@
6767
"php": "^7.1|^8.0",
6868
"ext-dom": "*",
6969
"ext-json": "*",
70-
"ext-xml": "*",
71-
"laminas/laminas-escaper": ">=2.6"
70+
"ext-xml": "*"
7271
},
7372
"require-dev": {
7473
"ext-zip": "*",

composer.lock

Lines changed: 2 additions & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/usage/styles/font.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ Available Font style options:
2323
- ``lang``. Language, either a language code like *en-US*, *fr-BE*, etc. or an object (or as an array) if you need to set eastAsian or bidirectional languages
2424
See ``\PhpOffice\PhpWord\Style\Language`` class for some language codes.
2525
- ``position``. The text position, raised or lowered, in half points
26-
- ``hidden``. Hidden text, *true* or *false*.
26+
- ``hidden``. Hidden text, *true* or *false*.
27+
`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).
28+
- ``htmlGenericFont``. Fallback generic font for html/pdf. Possible values are *sans-serif*, *serif*, and *monospace* (other css values for generic fonts are accepted).

docs/usage/writers.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ $writer = IOFactory::createWriter($oPhpWord, 'HTML');
1010
$writer->save(__DIR__ . '/sample.html');
1111
```
1212

13+
14+
When generating html/pdf, you can alter the default handling of white space (normal),
15+
and/or supply a fallback generic font as follows:
16+
17+
```php
18+
$phpWord->setDefaultHtmlGenericFont('serif');
19+
$phpWord->setDefaultHtmlWhiteSpace('pre-wrap');
20+
```
21+
1322
## ODText
1423
The name of the writer is `ODText`.
1524

phpstan-baseline.neon

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -520,16 +520,6 @@ parameters:
520520
count: 1
521521
path: src/PhpWord/Shared/Microsoft/PasswordEncoder.php
522522

523-
-
524-
message: "#^Method PhpOffice\\\\PhpWord\\\\Shared\\\\XMLReader\\:\\:getElements\\(\\) should return DOMNodeList\\<DOMElement\\> but returns DOMNodeList\\<DOMNode\\>\\.$#"
525-
count: 1
526-
path: src/PhpWord/Shared/XMLReader.php
527-
528-
-
529-
message: "#^Method PhpOffice\\\\PhpWord\\\\Shared\\\\XMLReader\\:\\:getElements\\(\\) should return DOMNodeList\\<DOMElement\\> but returns DOMNodeList\\<DOMNode\\>\\|false\\.$#"
530-
count: 2
531-
path: src/PhpWord/Shared/XMLReader.php
532-
533523
-
534524
message: "#^Method PhpOffice\\\\PhpWord\\\\Shared\\\\XMLWriter\\:\\:getData\\(\\) should return string but returns string\\|false\\.$#"
535525
count: 1
@@ -1180,46 +1170,11 @@ parameters:
11801170
count: 1
11811171
path: src/PhpWord/Writer/HTML/Element/AbstractElement.php
11821172

1183-
-
1184-
message: "#^Call to an undefined method Laminas\\\\Escaper\\\\Escaper\\|PhpOffice\\\\PhpWord\\\\Escaper\\\\AbstractEscaper\\:\\:escapeHtml\\(\\)\\.$#"
1185-
count: 1
1186-
path: src/PhpWord/Writer/HTML/Element/Link.php
1187-
1188-
-
1189-
message: "#^Call to an undefined method Laminas\\\\Escaper\\\\Escaper\\|PhpOffice\\\\PhpWord\\\\Escaper\\\\AbstractEscaper\\:\\:escapeHtmlAttr\\(\\)\\.$#"
1190-
count: 1
1191-
path: src/PhpWord/Writer/HTML/Element/Link.php
1192-
1193-
-
1194-
message: "#^Call to an undefined method Laminas\\\\Escaper\\\\Escaper\\|PhpOffice\\\\PhpWord\\\\Escaper\\\\AbstractEscaper\\:\\:escapeHtml\\(\\)\\.$#"
1195-
count: 1
1196-
path: src/PhpWord/Writer/HTML/Element/ListItem.php
1197-
11981173
-
11991174
message: "#^Variable \\$row in PHPDoc tag @var does not match assigned variable \\$rowStyle\\.$#"
12001175
count: 1
12011176
path: src/PhpWord/Writer/HTML/Element/Table.php
12021177

1203-
-
1204-
message: "#^Call to an undefined method Laminas\\\\Escaper\\\\Escaper\\|PhpOffice\\\\PhpWord\\\\Escaper\\\\AbstractEscaper\\:\\:escapeHtml\\(\\)\\.$#"
1205-
count: 2
1206-
path: src/PhpWord/Writer/HTML/Element/Text.php
1207-
1208-
-
1209-
message: "#^Call to an undefined method Laminas\\\\Escaper\\\\Escaper\\|PhpOffice\\\\PhpWord\\\\Escaper\\\\AbstractEscaper\\:\\:escapeHtml\\(\\)\\.$#"
1210-
count: 1
1211-
path: src/PhpWord/Writer/HTML/Element/Title.php
1212-
1213-
-
1214-
message: "#^Method PhpOffice\\\\PhpWord\\\\Writer\\\\HTML\\\\Style\\\\AbstractStyle\\:\\:write\\(\\) has no return type specified\\.$#"
1215-
count: 1
1216-
path: src/PhpWord/Writer/HTML/Style/AbstractStyle.php
1217-
1218-
-
1219-
message: "#^Else branch is unreachable because previous condition is always true\\.$#"
1220-
count: 1
1221-
path: src/PhpWord/Writer/HTML/Style/Paragraph.php
1222-
12231178
-
12241179
message: "#^Method PhpOffice\\\\PhpWord\\\\Writer\\\\ODText\\\\Element\\\\Field\\:\\:writeDefault\\(\\) has parameter \\$type with no type specified\\.$#"
12251180
count: 1
@@ -1275,11 +1230,6 @@ parameters:
12751230
count: 1
12761231
path: src/PhpWord/Writer/ODText/Part/Styles.php
12771232

1278-
-
1279-
message: "#^Variable \\$indent in empty\\(\\) always exists and is not falsy\\.$#"
1280-
count: 1
1281-
path: src/PhpWord/Writer/ODText/Style/Paragraph.php
1282-
12831233
-
12841234
message: "#^Parameter \\#1 \\$callback of function call_user_func_array expects callable\\(\\)\\: mixed, array\\{PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\\\AbstractRenderer, string\\} given\\.$#"
12851235
count: 1
@@ -1330,11 +1280,6 @@ parameters:
13301280
count: 1
13311281
path: src/PhpWord/Writer/RTF/Element/AbstractElement.php
13321282

1333-
-
1334-
message: "#^Call to an undefined method Laminas\\\\Escaper\\\\Escaper\\|PhpOffice\\\\PhpWord\\\\Escaper\\\\AbstractEscaper\\:\\:escape\\(\\)\\.$#"
1335-
count: 1
1336-
path: src/PhpWord/Writer/RTF/Element/AbstractElement.php
1337-
13381283
-
13391284
message: "#^Parameter \\#1 \\$value of method PhpOffice\\\\PhpWord\\\\Writer\\\\RTF\\\\Style\\\\Font\\:\\:setNameIndex\\(\\) expects int, int\\|string given\\.$#"
13401285
count: 1
@@ -1415,21 +1360,6 @@ parameters:
14151360
count: 1
14161361
path: src/PhpWord/Writer/RTF/Style/Border.php
14171362

1418-
-
1419-
message: "#^Variable \\$spaceAfter on left side of \\?\\? always exists and is not nullable\\.$#"
1420-
count: 1
1421-
path: src/PhpWord/Writer/RTF/Style/Paragraph.php
1422-
1423-
-
1424-
message: "#^Variable \\$spaceBefore on left side of \\?\\? always exists and is not nullable\\.$#"
1425-
count: 1
1426-
path: src/PhpWord/Writer/RTF/Style/Paragraph.php
1427-
1428-
-
1429-
message: "#^Method PhpOffice\\\\PhpWord\\\\Writer\\\\RTF\\\\Style\\\\Tab\\:\\:write\\(\\) has no return type specified\\.$#"
1430-
count: 1
1431-
path: src/PhpWord/Writer/RTF/Style/Tab.php
1432-
14331363
-
14341364
message: "#^PHPDoc tag @param has invalid value \\(\\\\PhpOffice\\\\PhpWord\\\\PhpWord\\)\\: Unexpected token \"\\\\n \", expected variable at offset 86$#"
14351365
count: 1
@@ -1480,16 +1410,6 @@ parameters:
14801410
count: 1
14811411
path: src/PhpWord/Writer/Word2007/Element/SDT.php
14821412

1483-
-
1484-
message: "#^Parameter \\#1 \\$content of method PhpOffice\\\\PhpWord\\\\Writer\\\\Word2007\\\\Element\\\\AbstractElement\\:\\:writeText\\(\\) expects string, PhpOffice\\\\PhpWord\\\\Element\\\\TextRun\\|string given\\.$#"
1485-
count: 1
1486-
path: src/PhpWord/Writer/Word2007/Element/TOC.php
1487-
1488-
-
1489-
message: "#^Parameter \\#3 \\$indent of method PhpOffice\\\\PhpWord\\\\Writer\\\\Word2007\\\\Element\\\\TOC\\:\\:writeStyle\\(\\) expects int, float\\|int given\\.$#"
1490-
count: 1
1491-
path: src/PhpWord/Writer/Word2007/Element/TOC.php
1492-
14931413
-
14941414
message: "#^Property PhpOffice\\\\PhpWord\\\\Writer\\\\Word2007\\\\Element\\\\TableAlignment\\:\\:\\$attributes has no type specified\\.$#"
14951415
count: 1
@@ -1540,11 +1460,6 @@ parameters:
15401460
count: 1
15411461
path: src/PhpWord/Writer/Word2007/Style/Font.php
15421462

1543-
-
1544-
message: "#^Method PhpOffice\\\\PhpWord\\\\Writer\\\\WriterInterface\\:\\:save\\(\\) has no return type specified\\.$#"
1545-
count: 1
1546-
path: src/PhpWord/Writer/WriterInterface.php
1547-
15481463
-
15491464
message: "#^Call to an undefined method object\\:\\:read\\(\\)\\.$#"
15501465
count: 1
@@ -1910,21 +1825,6 @@ parameters:
19101825
count: 2
19111826
path: tests/PhpWordTests/TemplateProcessorTest.php
19121827

1913-
-
1914-
message: "#^Method PhpOffice\\\\PhpWordTests\\\\TemplateProcessorTest\\:\\:testTemplateCanBeSavedInTemporaryLocation\\(\\) has no return type specified\\.$#"
1915-
count: 1
1916-
path: tests/PhpWordTests/TemplateProcessorTest.php
1917-
1918-
-
1919-
message: "#^Parameter \\#1 \\$expectedXml of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertXmlStringEqualsXmlString\\(\\) expects DOMDocument\\|string, string\\|false given\\.$#"
1920-
count: 3
1921-
path: tests/PhpWordTests/TemplateProcessorTest.php
1922-
1923-
-
1924-
message: "#^Parameter \\#2 \\$actualXml of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertXmlStringEqualsXmlString\\(\\) expects DOMDocument\\|string, string\\|false given\\.$#"
1925-
count: 3
1926-
path: tests/PhpWordTests/TemplateProcessorTest.php
1927-
19281828
-
19291829
message: "#^Parameter \\#2 \\$haystack of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertStringContainsString\\(\\) expects string, string\\|false given\\.$#"
19301830
count: 6
@@ -2005,11 +1905,6 @@ parameters:
20051905
count: 1
20061906
path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php
20071907

2008-
-
2009-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:save\\(\\)\\.$#"
2010-
count: 1
2011-
path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php
2012-
20131908
-
20141909
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:setFont\\(\\)\\.$#"
20151910
count: 1
@@ -2035,26 +1930,11 @@ parameters:
20351930
count: 3
20361931
path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php
20371932

2038-
-
2039-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:save\\(\\)\\.$#"
2040-
count: 1
2041-
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php
2042-
2043-
-
2044-
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
2045-
count: 2
2046-
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php
2047-
20481933
-
20491934
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:getFont\\(\\)\\.$#"
20501935
count: 1
20511936
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php
20521937

2053-
-
2054-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:save\\(\\)\\.$#"
2055-
count: 1
2056-
path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php
2057-
20581938
-
20591939
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
20601940
count: 2
@@ -2065,16 +1945,6 @@ parameters:
20651945
count: 1
20661946
path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php
20671947

2068-
-
2069-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:save\\(\\)\\.$#"
2070-
count: 2
2071-
path: tests/PhpWordTests/Writer/PDFTest.php
2072-
2073-
-
2074-
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
2075-
count: 1
2076-
path: tests/PhpWordTests/Writer/PDFTest.php
2077-
20781948
-
20791949
message: "#^Method PhpOffice\\\\PhpWordTests\\\\Writer\\\\RTF\\\\ElementTest\\:\\:removeCr\\(\\) has no return type specified\\.$#"
20801950
count: 1

phpunit.xml.dist

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
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>
23-
<logging>
24-
<log type="coverage-html" target="./build/coverage" />
25-
<log type="coverage-clover" target="./build/logs/clover.xml" />
26-
</logging>
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" convertDeprecationsToExceptions="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/PhpWord/Shared/PCLZip</directory>
9+
</exclude>
10+
<report>
11+
<clover outputFile="./build/logs/clover.xml"/>
12+
<html outputDirectory="./build/coverage"/>
13+
</report>
14+
</coverage>
15+
<php>
16+
<ini name="error_reporting" value="E_ALL"/>
17+
</php>
18+
<testsuites>
19+
<testsuite name="PhpWord Test Suite">
20+
<directory>./tests/PhpWordTests</directory>
21+
</testsuite>
22+
</testsuites>
23+
<logging/>
2724
</phpunit>

0 commit comments

Comments
 (0)