Skip to content

Commit 21f71c9

Browse files
oleibmanProgi1984
authored andcommitted
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 d5ca5b4 commit 21f71c9

File tree

103 files changed

+2968
-641
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

+2968
-641
lines changed

Diff for: composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
"ext-dom": "*",
6969
"ext-json": "*",
7070
"ext-xml": "*",
71-
"laminas/laminas-escaper": ">=2.6",
7271
"phpoffice/math": "^0.1"
7372
},
7473
"require-dev": {

Diff for: composer.lock

-62
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: docs/usage/styles/font.md

+3-1
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).

Diff for: docs/usage/writers.md

+9
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

Diff for: phpstan-baseline.neon

-130
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
@@ -1905,21 +1820,6 @@ parameters:
19051820
count: 2
19061821
path: tests/PhpWordTests/TemplateProcessorTest.php
19071822

1908-
-
1909-
message: "#^Method PhpOffice\\\\PhpWordTests\\\\TemplateProcessorTest\\:\\:testTemplateCanBeSavedInTemporaryLocation\\(\\) has no return type specified\\.$#"
1910-
count: 1
1911-
path: tests/PhpWordTests/TemplateProcessorTest.php
1912-
1913-
-
1914-
message: "#^Parameter \\#1 \\$expectedXml of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertXmlStringEqualsXmlString\\(\\) expects DOMDocument\\|string, string\\|false given\\.$#"
1915-
count: 3
1916-
path: tests/PhpWordTests/TemplateProcessorTest.php
1917-
1918-
-
1919-
message: "#^Parameter \\#2 \\$actualXml of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertXmlStringEqualsXmlString\\(\\) expects DOMDocument\\|string, string\\|false given\\.$#"
1920-
count: 3
1921-
path: tests/PhpWordTests/TemplateProcessorTest.php
1922-
19231823
-
19241824
message: "#^Parameter \\#2 \\$haystack of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertStringContainsString\\(\\) expects string, string\\|false given\\.$#"
19251825
count: 6
@@ -2000,11 +1900,6 @@ parameters:
20001900
count: 1
20011901
path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php
20021902

2003-
-
2004-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:save\\(\\)\\.$#"
2005-
count: 1
2006-
path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php
2007-
20081903
-
20091904
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:setFont\\(\\)\\.$#"
20101905
count: 1
@@ -2030,26 +1925,11 @@ parameters:
20301925
count: 3
20311926
path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php
20321927

2033-
-
2034-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:save\\(\\)\\.$#"
2035-
count: 1
2036-
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php
2037-
2038-
-
2039-
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
2040-
count: 2
2041-
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php
2042-
20431928
-
20441929
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:getFont\\(\\)\\.$#"
20451930
count: 1
20461931
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php
20471932

2048-
-
2049-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:save\\(\\)\\.$#"
2050-
count: 1
2051-
path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php
2052-
20531933
-
20541934
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
20551935
count: 2
@@ -2060,16 +1940,6 @@ parameters:
20601940
count: 1
20611941
path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php
20621942

2063-
-
2064-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:save\\(\\)\\.$#"
2065-
count: 2
2066-
path: tests/PhpWordTests/Writer/PDFTest.php
2067-
2068-
-
2069-
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
2070-
count: 1
2071-
path: tests/PhpWordTests/Writer/PDFTest.php
2072-
20731943
-
20741944
message: "#^Method PhpOffice\\\\PhpWordTests\\\\Writer\\\\RTF\\\\ElementTest\\:\\:removeCr\\(\\) has no return type specified\\.$#"
20751945
count: 1

Diff for: phpunit.xml.dist

+23-26
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)