Skip to content

Commit f914c95

Browse files
committed
Feat: Added Epub3 writer support
1 parent f24aabc commit f914c95

File tree

13 files changed

+519
-6
lines changed

13 files changed

+519
-6
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@
6767
"ext-dom": "*",
6868
"ext-json": "*",
6969
"ext-xml": "*",
70-
"phpoffice/math": "^0.2"
70+
"phpoffice/math": "^0.2",
71+
"ext-zip": "*",
72+
"ext-fileinfo": "*",
73+
"ext-mbstring": "*"
7174
},
7275
"require-dev": {
73-
"ext-zip": "*",
7476
"ext-gd": "*",
7577
"ext-libxml": "*",
7678
"dompdf/dompdf": "^2.0 || ^3.0",

composer.lock

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

src/PhpWord/IOFactory.php

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
*
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\ePub3\Part;
19+
20+
use PhpOffice\PhpWord\Writer\AbstractWriter;
21+
22+
/**
23+
* Abstract class for ePub3 parts.
24+
*/
25+
abstract class AbstractPart
26+
{
27+
/**
28+
* Parent writer.
29+
*
30+
* @var \PhpOffice\PhpWord\Writer\AbstractWriter
31+
*/
32+
protected $parentWriter;
33+
34+
/**
35+
* Set parent writer.
36+
*
37+
* @param \PhpOffice\PhpWord\Writer\AbstractWriter $writer
38+
*
39+
* @return self
40+
*/
41+
public function setParentWriter(AbstractWriter $writer)
42+
{
43+
$this->parentWriter = $writer;
44+
45+
return $this;
46+
}
47+
48+
/**
49+
* Get parent writer.
50+
*
51+
* @return \PhpOffice\PhpWord\Writer\AbstractWriter
52+
*/
53+
public function getParentWriter()
54+
{
55+
return $this->parentWriter;
56+
}
57+
58+
/**
59+
* Write part content.
60+
*
61+
* @return string
62+
*/
63+
abstract public function write();
64+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
*
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\ePub3\Part;
19+
20+
/**
21+
* Class for ePub3 content part.
22+
*/
23+
class Content extends AbstractPart
24+
{
25+
/**
26+
* Write part content.
27+
*
28+
* @return string
29+
*/
30+
public function write()
31+
{
32+
$content = '<?xml version="1.0" encoding="UTF-8"?>';
33+
$content .= '<package xmlns="http://www.idpf.org/2007/opf" version="3.0">';
34+
$content .= '<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">';
35+
$content .= '<dc:title>Sample ePub3 Document</dc:title>';
36+
$content .= '<dc:language>en</dc:language>';
37+
$content .= '</metadata>';
38+
$content .= '<manifest>';
39+
$content .= '<item id="content" href="content.xhtml" media-type="application/xhtml+xml"/>';
40+
$content .= '</manifest>';
41+
$content .= '<spine>';
42+
$content .= '<itemref idref="content"/>';
43+
$content .= '</spine>';
44+
$content .= '</package>';
45+
46+
return $content;
47+
}
48+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
*
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\ePub3\Part;
19+
20+
/**
21+
* Class for ePub3 manifest part.
22+
*/
23+
class Manifest extends AbstractPart
24+
{
25+
/**
26+
* Write part content.
27+
*
28+
* @return string
29+
*/
30+
public function write()
31+
{
32+
$content = '<?xml version="1.0" encoding="UTF-8"?>';
33+
$content .= '<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">';
34+
$content .= '<rootfiles>';
35+
$content .= '<rootfile full-path="content.opf" media-type="application/oebps-package+xml"/>';
36+
$content .= '</rootfiles>';
37+
$content .= '</container>';
38+
39+
return $content;
40+
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
*
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\ePub3\Part;
19+
20+
/**
21+
* Class for ePub3 metadata part.
22+
*/
23+
class Meta extends AbstractPart
24+
{
25+
/**
26+
* Write part content.
27+
*
28+
* @return string
29+
*/
30+
public function write()
31+
{
32+
$content = '<?xml version="1.0" encoding="UTF-8"?>';
33+
$content .= '<metadata xmlns="http://www.idpf.org/2007/opf">';
34+
$content .= '<dc:title>Sample ePub3 Document</dc:title>';
35+
$content .= '<dc:language>en</dc:language>';
36+
$content .= '<dc:identifier id="bookid">urn:uuid:12345</dc:identifier>';
37+
$content .= '<meta property="dcterms:modified">2023-01-01T00:00:00Z</meta>';
38+
$content .= '</metadata>';
39+
40+
return $content;
41+
}
42+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
*
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\ePub3\Part;
19+
20+
/**
21+
* Class for ePub3 mimetype part.
22+
*/
23+
class Mimetype extends AbstractPart
24+
{
25+
/**
26+
* Write part content.
27+
*
28+
* @return string
29+
*/
30+
public function write()
31+
{
32+
return 'application/epub+zip';
33+
}
34+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
*
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\EPub3\Style;
19+
20+
use PhpOffice\PhpWord\Writer\AbstractWriter;
21+
22+
/**
23+
* Abstract class for ePub3 styles.
24+
*/
25+
abstract class AbstractStyle
26+
{
27+
/**
28+
* Parent writer.
29+
*
30+
* @var \PhpOffice\PhpWord\Writer\AbstractWriter
31+
*/
32+
protected $parentWriter;
33+
34+
/**
35+
* Set parent writer.
36+
*
37+
* @param \PhpOffice\PhpWord\Writer\AbstractWriter $writer
38+
*
39+
* @return self
40+
*/
41+
public function setParentWriter(AbstractWriter $writer)
42+
{
43+
$this->parentWriter = $writer;
44+
45+
return $this;
46+
}
47+
48+
/**
49+
* Get parent writer.
50+
*
51+
* @return \PhpOffice\PhpWord\Writer\AbstractWriter
52+
*/
53+
public function getParentWriter()
54+
{
55+
return $this->parentWriter;
56+
}
57+
58+
/**
59+
* Write style content.
60+
*
61+
* @return string
62+
*/
63+
abstract public function write();
64+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
*
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\ePub3\Style;
19+
20+
/**
21+
* Class for ePub3 font styles.
22+
*/
23+
class Font extends AbstractStyle
24+
{
25+
/**
26+
* Write style content.
27+
*
28+
* @return string
29+
*/
30+
public function write()
31+
{
32+
$content = 'body {';
33+
$content .= 'font-family: "Times New Roman", Times, serif;';
34+
$content .= 'font-size: 12pt;';
35+
$content .= 'color: #000000;';
36+
$content .= '}';
37+
38+
return $content;
39+
}
40+
}

0 commit comments

Comments
 (0)