Skip to content

Commit

Permalink
Add Support for Various Missing Features in HTML Writer
Browse files Browse the repository at this point in the history
This PR supersedes #1814 and #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.
  • Loading branch information
oleibman committed Sep 18, 2023
1 parent a836c32 commit 82ec8e2
Show file tree
Hide file tree
Showing 103 changed files with 2,971 additions and 645 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@
"php": "^7.1|^8.0",
"ext-dom": "*",
"ext-json": "*",
"ext-xml": "*",
"laminas/laminas-escaper": ">=2.6"
"ext-xml": "*"
},
"require-dev": {
"ext-zip": "*",
Expand Down
67 changes: 2 additions & 65 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion docs/usage/styles/font.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ Available Font style options:
- ``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
See ``\PhpOffice\PhpWord\Style\Language`` class for some language codes.
- ``position``. The text position, raised or lowered, in half points
- ``hidden``. Hidden text, *true* or *false*.
- ``hidden``. Hidden text, *true* or *false*.
`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).
- ``htmlGenericFont``. Fallback generic font for html/pdf. Possible values are *sans-serif*, *serif*, and *monospace* (other css values for generic fonts are accepted).
9 changes: 9 additions & 0 deletions docs/usage/writers.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ $writer = IOFactory::createWriter($oPhpWord, 'HTML');
$writer->save(__DIR__ . '/sample.html');
```


When generating html/pdf, you can alter the default handling of white space (normal),
and/or supply a fallback generic font as follows:

```php
$phpWord->setDefaultHtmlGenericFont('serif');
$phpWord->setDefaultHtmlWhiteSpace('pre-wrap');
```

## ODText
The name of the writer is `ODText`.

Expand Down
130 changes: 0 additions & 130 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -520,16 +520,6 @@ parameters:
count: 1
path: src/PhpWord/Shared/Microsoft/PasswordEncoder.php

-
message: "#^Method PhpOffice\\\\PhpWord\\\\Shared\\\\XMLReader\\:\\:getElements\\(\\) should return DOMNodeList\\<DOMElement\\> but returns DOMNodeList\\<DOMNode\\>\\.$#"
count: 1
path: src/PhpWord/Shared/XMLReader.php

-
message: "#^Method PhpOffice\\\\PhpWord\\\\Shared\\\\XMLReader\\:\\:getElements\\(\\) should return DOMNodeList\\<DOMElement\\> but returns DOMNodeList\\<DOMNode\\>\\|false\\.$#"
count: 2
path: src/PhpWord/Shared/XMLReader.php

-
message: "#^Method PhpOffice\\\\PhpWord\\\\Shared\\\\XMLWriter\\:\\:getData\\(\\) should return string but returns string\\|false\\.$#"
count: 1
Expand Down Expand Up @@ -1180,46 +1170,11 @@ parameters:
count: 1
path: src/PhpWord/Writer/HTML/Element/AbstractElement.php

-
message: "#^Call to an undefined method Laminas\\\\Escaper\\\\Escaper\\|PhpOffice\\\\PhpWord\\\\Escaper\\\\AbstractEscaper\\:\\:escapeHtml\\(\\)\\.$#"
count: 1
path: src/PhpWord/Writer/HTML/Element/Link.php

-
message: "#^Call to an undefined method Laminas\\\\Escaper\\\\Escaper\\|PhpOffice\\\\PhpWord\\\\Escaper\\\\AbstractEscaper\\:\\:escapeHtmlAttr\\(\\)\\.$#"
count: 1
path: src/PhpWord/Writer/HTML/Element/Link.php

-
message: "#^Call to an undefined method Laminas\\\\Escaper\\\\Escaper\\|PhpOffice\\\\PhpWord\\\\Escaper\\\\AbstractEscaper\\:\\:escapeHtml\\(\\)\\.$#"
count: 1
path: src/PhpWord/Writer/HTML/Element/ListItem.php

-
message: "#^Variable \\$row in PHPDoc tag @var does not match assigned variable \\$rowStyle\\.$#"
count: 1
path: src/PhpWord/Writer/HTML/Element/Table.php

-
message: "#^Call to an undefined method Laminas\\\\Escaper\\\\Escaper\\|PhpOffice\\\\PhpWord\\\\Escaper\\\\AbstractEscaper\\:\\:escapeHtml\\(\\)\\.$#"
count: 2
path: src/PhpWord/Writer/HTML/Element/Text.php

-
message: "#^Call to an undefined method Laminas\\\\Escaper\\\\Escaper\\|PhpOffice\\\\PhpWord\\\\Escaper\\\\AbstractEscaper\\:\\:escapeHtml\\(\\)\\.$#"
count: 1
path: src/PhpWord/Writer/HTML/Element/Title.php

-
message: "#^Method PhpOffice\\\\PhpWord\\\\Writer\\\\HTML\\\\Style\\\\AbstractStyle\\:\\:write\\(\\) has no return type specified\\.$#"
count: 1
path: src/PhpWord/Writer/HTML/Style/AbstractStyle.php

-
message: "#^Else branch is unreachable because previous condition is always true\\.$#"
count: 1
path: src/PhpWord/Writer/HTML/Style/Paragraph.php

-
message: "#^Method PhpOffice\\\\PhpWord\\\\Writer\\\\ODText\\\\Element\\\\Field\\:\\:writeDefault\\(\\) has parameter \\$type with no type specified\\.$#"
count: 1
Expand Down Expand Up @@ -1275,11 +1230,6 @@ parameters:
count: 1
path: src/PhpWord/Writer/ODText/Part/Styles.php

-
message: "#^Variable \\$indent in empty\\(\\) always exists and is not falsy\\.$#"
count: 1
path: src/PhpWord/Writer/ODText/Style/Paragraph.php

-
message: "#^Parameter \\#1 \\$callback of function call_user_func_array expects callable\\(\\)\\: mixed, array\\{PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\\\AbstractRenderer, string\\} given\\.$#"
count: 1
Expand Down Expand Up @@ -1330,11 +1280,6 @@ parameters:
count: 1
path: src/PhpWord/Writer/RTF/Element/AbstractElement.php

-
message: "#^Call to an undefined method Laminas\\\\Escaper\\\\Escaper\\|PhpOffice\\\\PhpWord\\\\Escaper\\\\AbstractEscaper\\:\\:escape\\(\\)\\.$#"
count: 1
path: src/PhpWord/Writer/RTF/Element/AbstractElement.php

-
message: "#^Parameter \\#1 \\$value of method PhpOffice\\\\PhpWord\\\\Writer\\\\RTF\\\\Style\\\\Font\\:\\:setNameIndex\\(\\) expects int, int\\|string given\\.$#"
count: 1
Expand Down Expand Up @@ -1415,21 +1360,6 @@ parameters:
count: 1
path: src/PhpWord/Writer/RTF/Style/Border.php

-
message: "#^Variable \\$spaceAfter on left side of \\?\\? always exists and is not nullable\\.$#"
count: 1
path: src/PhpWord/Writer/RTF/Style/Paragraph.php

-
message: "#^Variable \\$spaceBefore on left side of \\?\\? always exists and is not nullable\\.$#"
count: 1
path: src/PhpWord/Writer/RTF/Style/Paragraph.php

-
message: "#^Method PhpOffice\\\\PhpWord\\\\Writer\\\\RTF\\\\Style\\\\Tab\\:\\:write\\(\\) has no return type specified\\.$#"
count: 1
path: src/PhpWord/Writer/RTF/Style/Tab.php

-
message: "#^PHPDoc tag @param has invalid value \\(\\\\PhpOffice\\\\PhpWord\\\\PhpWord\\)\\: Unexpected token \"\\\\n \", expected variable at offset 86$#"
count: 1
Expand Down Expand Up @@ -1480,16 +1410,6 @@ parameters:
count: 1
path: src/PhpWord/Writer/Word2007/Element/SDT.php

-
message: "#^Parameter \\#1 \\$content of method PhpOffice\\\\PhpWord\\\\Writer\\\\Word2007\\\\Element\\\\AbstractElement\\:\\:writeText\\(\\) expects string, PhpOffice\\\\PhpWord\\\\Element\\\\TextRun\\|string given\\.$#"
count: 1
path: src/PhpWord/Writer/Word2007/Element/TOC.php

-
message: "#^Parameter \\#3 \\$indent of method PhpOffice\\\\PhpWord\\\\Writer\\\\Word2007\\\\Element\\\\TOC\\:\\:writeStyle\\(\\) expects int, float\\|int given\\.$#"
count: 1
path: src/PhpWord/Writer/Word2007/Element/TOC.php

-
message: "#^Property PhpOffice\\\\PhpWord\\\\Writer\\\\Word2007\\\\Element\\\\TableAlignment\\:\\:\\$attributes has no type specified\\.$#"
count: 1
Expand Down Expand Up @@ -1540,11 +1460,6 @@ parameters:
count: 1
path: src/PhpWord/Writer/Word2007/Style/Font.php

-
message: "#^Method PhpOffice\\\\PhpWord\\\\Writer\\\\WriterInterface\\:\\:save\\(\\) has no return type specified\\.$#"
count: 1
path: src/PhpWord/Writer/WriterInterface.php

-
message: "#^Call to an undefined method object\\:\\:read\\(\\)\\.$#"
count: 1
Expand Down Expand Up @@ -1910,21 +1825,6 @@ parameters:
count: 2
path: tests/PhpWordTests/TemplateProcessorTest.php

-
message: "#^Method PhpOffice\\\\PhpWordTests\\\\TemplateProcessorTest\\:\\:testTemplateCanBeSavedInTemporaryLocation\\(\\) has no return type specified\\.$#"
count: 1
path: tests/PhpWordTests/TemplateProcessorTest.php

-
message: "#^Parameter \\#1 \\$expectedXml of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertXmlStringEqualsXmlString\\(\\) expects DOMDocument\\|string, string\\|false given\\.$#"
count: 3
path: tests/PhpWordTests/TemplateProcessorTest.php

-
message: "#^Parameter \\#2 \\$actualXml of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertXmlStringEqualsXmlString\\(\\) expects DOMDocument\\|string, string\\|false given\\.$#"
count: 3
path: tests/PhpWordTests/TemplateProcessorTest.php

-
message: "#^Parameter \\#2 \\$haystack of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertStringContainsString\\(\\) expects string, string\\|false given\\.$#"
count: 6
Expand Down Expand Up @@ -2005,11 +1905,6 @@ parameters:
count: 1
path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php

-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:save\\(\\)\\.$#"
count: 1
path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php

-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:setFont\\(\\)\\.$#"
count: 1
Expand All @@ -2035,26 +1930,11 @@ parameters:
count: 3
path: tests/PhpWordTests/Writer/PDF/DomPDFTest.php

-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:save\\(\\)\\.$#"
count: 1
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php

-
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
count: 2
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php

-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:getFont\\(\\)\\.$#"
count: 1
path: tests/PhpWordTests/Writer/PDF/MPDFTest.php

-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:save\\(\\)\\.$#"
count: 1
path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php

-
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
count: 2
Expand All @@ -2065,16 +1945,6 @@ parameters:
count: 1
path: tests/PhpWordTests/Writer/PDF/TCPDFTest.php

-
message: "#^Call to an undefined method PhpOffice\\\\PhpWord\\\\Writer\\\\PDF\\:\\:save\\(\\)\\.$#"
count: 2
path: tests/PhpWordTests/Writer/PDFTest.php

-
message: "#^Parameter \\#2 \\$libraryBaseDir of static method PhpOffice\\\\PhpWord\\\\Settings\\:\\:setPdfRenderer\\(\\) expects string, string\\|false given\\.$#"
count: 1
path: tests/PhpWordTests/Writer/PDFTest.php

-
message: "#^Method PhpOffice\\\\PhpWordTests\\\\Writer\\\\RTF\\\\ElementTest\\:\\:removeCr\\(\\) has no return type specified\\.$#"
count: 1
Expand Down
49 changes: 23 additions & 26 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="PhpWord Test Suite">
<directory>./tests/PhpWordTests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
<exclude>
<directory suffix=".php">./src/PhpWordTests/Shared/PCLZip</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./build/coverage" />
<log type="coverage-clover" target="./build/logs/clover.xml" />
</logging>
<?xml version="1.0"?>
<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">
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<directory suffix=".php">./src/PhpWord/Shared/PCLZip</directory>
</exclude>
<report>
<clover outputFile="./build/logs/clover.xml"/>
<html outputDirectory="./build/coverage"/>
</report>
</coverage>
<php>
<ini name="error_reporting" value="E_ALL"/>
</php>
<testsuites>
<testsuite name="PhpWord Test Suite">
<directory>./tests/PhpWordTests</directory>
</testsuite>
</testsuites>
<logging/>
</phpunit>
Loading

0 comments on commit 82ec8e2

Please sign in to comment.