Skip to content

Commit c80dc43

Browse files
OlisaevAGOlisaevAG
OlisaevAG
authored and
OlisaevAG
committed
Merge branch 'master' into properCSSInlineBorder1670
2 parents c5a1b56 + d5d5aea commit c80dc43

Some content is hidden

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

55 files changed

+2335
-40
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ _build
1212
/build
1313
phpunit.xml
1414
composer.phar
15+
composer.lock
1516
vendor
1617
/report
1718
/build

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@
108108
"require": {
109109
"php": "^7.1|^8.0",
110110
"ext-dom": "*",
111-
"ext-gd": "*",
111+
"ext-gd": "*",
112+
"ext-zip": "*",
112113
"ext-json": "*",
113114
"ext-xml": "*",
114-
"ext-zip": "*",
115115
"phpoffice/math": "^0.2"
116116
},
117117
"require-dev": {

docs/changes/1.x/1.4.0.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
- Added support for PHP 8.4 by [@Progi1984](https://github.com/Progi1984) in [#2660](https://github.com/PHPOffice/PHPWord/pull/2660)
1313
- Autoload : Allow to use PHPWord without Composer fixing [#2543](https://github.com/PHPOffice/PHPWord/issues/2543), [#2552](https://github.com/PHPOffice/PHPWord/issues/2552), [#2716](https://github.com/PHPOffice/PHPWord/issues/2716), [#2717](https://github.com/PHPOffice/PHPWord/issues/2717) in [#2722](https://github.com/PHPOffice/PHPWord/pull/2722)
1414
- Add Default font color for Word by [@Collie-IT](https://github.com/Collie-IT) in [#2700](https://github.com/PHPOffice/PHPWord/pull/2700)
15-
- Writer HTML: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey)
15+
- Writer HTML: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2731](https://github.com/PHPOffice/PHPWord/pull/2731)
16+
- Writer ODText: Support Default font color by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2735](https://github.com/PHPOffice/PHPWord/pull/2735)
1617
- Add basic ruby text (phonetic guide) support for Word2007 and HTML Reader/Writer, RTF Writer, basic support for ODT writing by [@Deadpikle](https://github.com/Deadpikle) in [#2727](https://github.com/PHPOffice/PHPWord/pull/2727)
18+
- Reader HTML: Support font styles for h1/h6 by [@Progi1984](https://github.com/Progi1984) fixing [#2619](https://github.com/PHPOffice/PHPWord/issues/2619) in [#2737](https://github.com/PHPOffice/PHPWord/pull/2737)
19+
- Writer EPub3: Basic support by [@Sambit003](https://github.com/Sambit003) fixing [#55](https://github.com/PHPOffice/PHPWord/issues/55) in [#2724](https://github.com/PHPOffice/PHPWord/pull/2724)
20+
- Writer2007: Added support for background and border color transparency in Text Box element [@chudy20007](https://github.com/Chudy20007) in [#2555](https://github.com/PHPOffice/PHPWord/pull/2555)
1721

1822
### Bug fixes
1923

@@ -24,15 +28,25 @@
2428
- Reader Word2007: Respect paragraph indent units by [@tugmaks](https://github.com/tugmaks) & [@Progi1984](https://github.com/Progi1984) fixing [#507](https://github.com/PHPOffice/PHPWord/issues/507) in [#2726](https://github.com/PHPOffice/PHPWord/pull/2726)
2529
- Reader Word2007: Support Header elements within Title elements by [@SpraxDev](https://github.com/SpraxDev) fixing [#2616](https://github.com/PHPOffice/PHPWord/issues/2616), [#2426](https://github.com/PHPOffice/PHPWord/issues/2426) in [#2674](https://github.com/PHPOffice/PHPWord/pull/2674)
2630
- Reader HTML: Support for inherit value for property line-height by [@Progi1984](https://github.com/Progi1984) fixing [#2683](https://github.com/PHPOffice/PHPWord/issues/2683) in [#2733](https://github.com/PHPOffice/PHPWord/pull/2733)
31+
- Writer HTML: Fixed null string for Text Elements by [@armagedon007](https://github.com/armagedon007) and [@Progi1984](https://github.com/Progi1984) in [#2738](https://github.com/PHPOffice/PHPWord/pull/2738)
32+
- Template Processor: Fix 0 considered as empty string by [@cavasinf](https://github.com/cavasinf), [@SnipsMine](https://github.com/SnipsMine) and [@Progi1984](https://github.com/Progi1984) fixing [#2572](https://github.com/PHPOffice/PHPWord/issues/2572), [#2703](https://github.com/PHPOffice/PHPWord/issues/2703) in [#2748](https://github.com/PHPOffice/PHPWord/pull/2748)
2733

2834
### Miscellaneous
2935

3036
- Bump dompdf/dompdf from 2.0.4 to 3.0.0 by [@dependabot](https://github.com/dependabot) fixing [#2621](https://github.com/PHPOffice/PHPWord/issues/2621) in [#2666](https://github.com/PHPOffice/PHPWord/pull/2666)
3137
- Add test case to make sure vMerge defaults to 'continue' by [@SpraxDev](https://github.com/SpraxDev) in [#2677](https://github.com/PHPOffice/PHPWord/pull/2677)
38+
- Adding the possibility to use iterate search and replace with setValues by [@moghwan](https://github.com/moghwan) in [#2632](https://github.com/PHPOffice/PHPWord/pull/2640)
39+
- Add test cases that test the ODTText and Word2007 reader using the corresponding writer, increasing test coverage by [@MichaelPFrey](https://github.com/MichaelPFrey) in [#2745](https://github.com/PHPOffice/PHPWord/pull/2745)
3240

3341
### Deprecations
3442
- Deprecate `PhpOffice\PhpWord\Style\Paragraph::getIndent()` : Use `PhpOffice\PhpWord\Style\Paragraph::getIndentLeft()`
3543
- Deprecate `PhpOffice\PhpWord\Style\Paragraph::setHanging()` : Use `PhpOffice\PhpWord\Style\Paragraph::setIndentHanging()`
3644
- Deprecate `PhpOffice\PhpWord\Style\Paragraph::setIndent()` : Use `PhpOffice\PhpWord\Style\Paragraph::setIndentLeft()`
3745

3846
### BC Breaks
47+
48+
### Notes
49+
- Writer ODText previously used to set 'style:use-window-font-color' to 'true', now it is set to 'false'. (see [#2735](https://github.com/PHPOffice/PHPWord/pull/2735))
50+
The effect of this attribute is "implementation dependent" (if implemented at all).
51+
Setting it to false allows setting a default font color and improves interoperabilt,
52+
but may break certain specific use cases.

docs/install.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ require_once 'path/to/PHPWord/src/PhpWord/Autoloader.php';
4545

4646
The preferred method is the Composer one.
4747

48+
### Configuration
49+
50+
In order to configure you can create phpword.ini file and load configuration by calling Settings::loadConfig
51+
52+
``` php
53+
<?php
54+
55+
Settings::loadConfig();
56+
57+
```
58+
59+
You can also specify the config file location. (Do not use phpword.ini file in vendor folder)
60+
61+
``` php
62+
<?php
63+
64+
Settings::loadConfig(__DIR__ . '/../../phpword.ini');
65+
66+
```
67+
4868
## Samples
4969

5070
After installation, you can browse and use the samples that we've provided, either by command line or using browser. If you can access your PhpWord library folder using browser, point your browser to the `samples` folder, e.g. `http://localhost/PhpWord/samples/`.

samples/Sample_Header.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
}
3232

3333
// Set writers
34-
$writers = ['Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf', 'HTML' => 'html', 'PDF' => 'pdf'];
34+
$writers = ['Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf', 'HTML' => 'html', 'PDF' => 'pdf', 'EPub3' => 'epub'];
3535

3636
// Set PDF renderer
3737
if (null === Settings::getPdfRendererPath()) {

src/PhpWord/Element/Link.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,8 @@ public function getSource()
9999

100100
/**
101101
* Get link text.
102-
*
103-
* @return string
104102
*/
105-
public function getText()
103+
public function getText(): string
106104
{
107105
return $this->text;
108106
}

src/PhpWord/Element/Text.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,8 @@ public function setText($text)
147147

148148
/**
149149
* Get Text content.
150-
*
151-
* @return ?string
152150
*/
153-
public function getText()
151+
public function getText(): ?string
154152
{
155153
return $this->text;
156154
}

src/PhpWord/IOFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class IOFactory
3636
*/
3737
public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
3838
{
39-
if ($name !== 'WriterInterface' && !in_array($name, ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF'], true)) {
39+
if ($name !== 'WriterInterface' && !in_array($name, ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF', 'EPub3'], true)) {
4040
throw new Exception("\"{$name}\" is not a valid writer.");
4141
}
4242

src/PhpWord/Shared/Html.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,16 @@ protected static function parseNode($node, $element, $styles = [], $data = []):
211211

212212
// Node mapping table
213213
$nodes = [
214-
// $method $node $element $styles $data $argument1 $argument2
215-
'p' => ['Paragraph', $node, $element, $styles, null, null, null],
216-
'h1' => ['Heading', null, $element, $styles, null, 'Heading1', null],
217-
'h2' => ['Heading', null, $element, $styles, null, 'Heading2', null],
218-
'h3' => ['Heading', null, $element, $styles, null, 'Heading3', null],
219-
'h4' => ['Heading', null, $element, $styles, null, 'Heading4', null],
220-
'h5' => ['Heading', null, $element, $styles, null, 'Heading5', null],
221-
'h6' => ['Heading', null, $element, $styles, null, 'Heading6', null],
222-
'#text' => ['Text', $node, $element, $styles, null, null, null],
223-
'strong' => ['Property', null, null, $styles, null, 'bold', true],
214+
// $method $node $element $styles $data $argument1 $argument2
215+
'p' => ['Paragraph', $node, $element, $styles, null, null, null],
216+
'h1' => ['Heading', $node, $element, $styles, null, 'Heading1', null],
217+
'h2' => ['Heading', $node, $element, $styles, null, 'Heading2', null],
218+
'h3' => ['Heading', $node, $element, $styles, null, 'Heading3', null],
219+
'h4' => ['Heading', $node, $element, $styles, null, 'Heading4', null],
220+
'h5' => ['Heading', $node, $element, $styles, null, 'Heading5', null],
221+
'h6' => ['Heading', $node, $element, $styles, null, 'Heading6', null],
222+
'#text' => ['Text', $node, $element, $styles, null, null, null],
223+
'strong' => ['Property', null, null, $styles, null, 'bold', true],
224224
'b' => ['Property', null, null, $styles, null, 'bold', true],
225225
'em' => ['Property', null, null, $styles, null, 'italic', true],
226226
'i' => ['Property', null, null, $styles, null, 'italic', true],
@@ -345,21 +345,18 @@ protected static function parseInput($node, $element, &$styles): void
345345
/**
346346
* Parse heading node.
347347
*
348-
* @param AbstractContainer $element
349-
* @param array &$styles
350348
* @param string $argument1 Name of heading style
351349
*
352-
* @return TextRun
353-
*
354350
* @todo Think of a clever way of defining header styles, now it is only based on the assumption, that
355351
* Heading1 - Heading6 are already defined somewhere
356352
*/
357-
protected static function parseHeading($element, &$styles, $argument1)
353+
protected static function parseHeading(DOMNode $node, AbstractContainer $element, array &$styles, string $argument1): TextRun
358354
{
359-
$styles['paragraph'] = $argument1;
360-
$newElement = $element->addTextRun($styles['paragraph']);
355+
$style = new Paragraph();
356+
$style->setStyleName($argument1);
357+
$style->setStyleByArray(self::parseInlineStyle($node, $styles['paragraph']));
361358

362-
return $newElement;
359+
return $element->addTextRun($style);
363360
}
364361

365362
/**

src/PhpWord/Shared/ZipArchive.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,15 @@ public function pclzipLocateName($filename)
423423

424424
return ($listIndex > -1) ? $listIndex : false;
425425
}
426+
427+
/**
428+
* Add an empty directory to the zip archive (emulate \ZipArchive).
429+
*
430+
* @param string $dirname Directory name to add to the zip archive
431+
*/
432+
public function addEmptyDir(string $dirname): bool
433+
{
434+
// Create a directory entry by adding an empty file with trailing slash
435+
return $this->addFromString(rtrim($dirname, '/') . '/', '');
436+
}
426437
}

src/PhpWord/Style/Font.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Font extends AbstractStyle
109109
/**
110110
* Font color.
111111
*
112-
* @var string
112+
* @var null|string
113113
*/
114114
private $color;
115115

@@ -426,10 +426,8 @@ public function setSize($value = null)
426426

427427
/**
428428
* Get font color.
429-
*
430-
* @return string
431429
*/
432-
public function getColor()
430+
public function getColor(): ?string
433431
{
434432
return $this->color;
435433
}

src/PhpWord/TemplateProcessor.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ protected static function ensureMacroCompleted($macro)
269269
*/
270270
protected static function ensureUtf8Encoded($subject)
271271
{
272-
return $subject ? Text::toUTF8($subject) : '';
272+
return (null !== $subject) ? Text::toUTF8($subject) : '';
273273
}
274274

275275
/**
@@ -362,10 +362,10 @@ public function setValue($search, $replace, $limit = self::MAXIMUM_REPLACEMENTS_
362362
/**
363363
* Set values from a one-dimensional array of "variable => value"-pairs.
364364
*/
365-
public function setValues(array $values): void
365+
public function setValues(array $values, int $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT): void
366366
{
367367
foreach ($values as $macro => $replace) {
368-
$this->setValue($macro, $replace);
368+
$this->setValue($macro, $replace, $limit);
369369
}
370370
}
371371

src/PhpWord/Writer/EPub3.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHPWord - A pure PHP library for reading and writing
5+
* word processing documents.
6+
*
7+
* PHPWord is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPWord
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
namespace PhpOffice\PhpWord\Writer;
20+
21+
use PhpOffice\PhpWord\PhpWord;
22+
use PhpOffice\PhpWord\Writer\EPub3\Part\AbstractPart;
23+
24+
/**
25+
* EPub3 writer.
26+
*/
27+
class EPub3 extends AbstractWriter implements WriterInterface
28+
{
29+
/**
30+
* Create new EPub3 writer.
31+
*/
32+
public function __construct(?PhpWord $phpWord = null)
33+
{
34+
// Assign PhpWord
35+
$this->setPhpWord($phpWord);
36+
37+
// Create parts
38+
$this->parts = [
39+
'Mimetype' => 'mimetype',
40+
'Content' => 'content.opf',
41+
'Toc' => 'toc.ncx',
42+
'Styles' => 'styles.css',
43+
'Manifest' => 'META-INF/container.xml',
44+
'Nav' => 'nav.xhtml',
45+
'ContentXhtml' => 'content.xhtml',
46+
];
47+
foreach (array_keys($this->parts) as $partName) {
48+
$partClass = static::class . '\\Part\\' . $partName;
49+
if (class_exists($partClass)) {
50+
/** @var WriterPartInterface $part */
51+
$part = new $partClass($partName === 'Content' || $partName === 'ContentXhtml' ? $phpWord : null);
52+
$part->setParentWriter($this);
53+
$this->writerParts[strtolower($partName)] = $part;
54+
}
55+
}
56+
57+
// Set package paths
58+
$this->mediaPaths = ['image' => 'Images/', 'object' => 'Objects/'];
59+
}
60+
61+
/**
62+
* Save PhpWord to file.
63+
*/
64+
public function save(string $filename): void
65+
{
66+
$filename = $this->getTempFile($filename);
67+
$zip = $this->getZipArchive($filename);
68+
69+
// Add mimetype first without compression
70+
$zip->addFromString('mimetype', 'application/epub+zip');
71+
$zip->addEmptyDir('META-INF');
72+
73+
// Add other files
74+
foreach ($this->parts as $partName => $fileName) {
75+
if ($fileName === '') {
76+
continue;
77+
}
78+
$part = $this->getWriterPart($partName);
79+
if (!$part instanceof AbstractPart) {
80+
continue;
81+
}
82+
$zip->addFromString($fileName, $part->write());
83+
}
84+
85+
// Close zip archive
86+
$zip->close();
87+
88+
// Cleanup temp file
89+
$this->cleanupTempFile();
90+
}
91+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHPWord - A pure PHP library for reading and writing
5+
* word processing documents.
6+
*
7+
* PHPWord is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPWord
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
namespace PhpOffice\PhpWord\Writer\EPub3\Element;
20+
21+
use PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement as Word2007AbstractElement;
22+
23+
/**
24+
* Abstract element writer.
25+
*
26+
* @since 0.11.0
27+
*/
28+
abstract class AbstractElement extends Word2007AbstractElement
29+
{
30+
/**
31+
* Get class name of writer element based on read element.
32+
*
33+
* @param \PhpOffice\PhpWord\Element\AbstractElement $element
34+
*/
35+
public static function getElementClass($element): string
36+
{
37+
$elementClass = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($element));
38+
$writerClass = 'PhpOffice\\PhpWord\\Writer\\EPub3\\Element\\' . $elementClass;
39+
if (!class_exists($writerClass)) {
40+
throw new \PhpOffice\PhpWord\Exception\Exception("Writer element class {$writerClass} not found.");
41+
}
42+
43+
return $writerClass;
44+
}
45+
}

0 commit comments

Comments
 (0)