Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Added Epub3 writer support #2724

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ _build
/build
phpunit.xml
composer.phar
composer.lock
vendor
/report
/build
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@
"require": {
"php": "^7.1|^8.0",
"ext-dom": "*",
"ext-gd": "*",
"ext-json": "*",
"ext-xml": "*",
"ext-zip": "*",
"phpoffice/math": "^0.2"
},
"require-dev": {
"ext-zip": "*",
"ext-gd": "*",
"ext-libxml": "*",
"dompdf/dompdf": "^2.0 || ^3.0",
"friendsofphp/php-cs-fixer": "^3.3",
Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/IOFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class IOFactory
*/
public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
{
if ($name !== 'WriterInterface' && !in_array($name, ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF'], true)) {
if ($name !== 'WriterInterface' && !in_array($name, ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF', 'EPub3'], true)) {
throw new Exception("\"{$name}\" is not a valid writer.");
}

Expand Down
91 changes: 91 additions & 0 deletions src/PhpWord/Writer/EPub3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Writer;

use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Writer\EPub3\Part\AbstractPart;

/**
* EPub3 writer.
*/
class EPub3 extends AbstractWriter implements WriterInterface
{
/**
* Create new EPub3 writer.
*/
public function __construct(?PhpWord $phpWord = null)
{
// Assign PhpWord
$this->setPhpWord($phpWord);

// Create parts
$this->parts = [
'Mimetype' => 'mimetype',
'Content' => 'content.opf',
'Toc' => 'toc.ncx',
'Styles' => 'styles.css',
'Manifest' => 'META-INF/container.xml',
];
foreach (array_keys($this->parts) as $partName) {
$partClass = static::class . '\\Part\\' . $partName;
if (class_exists($partClass)) {
/** @var AbstractPart $partObject Type hint */
$partObject = new $partClass();
$partObject->setParentWriter($this);
$this->writerParts[strtolower($partName)] = $partObject;
}
}

// Set package paths
$this->mediaPaths = ['image' => 'Images/', 'object' => 'Objects/'];
}

/**
* Save PhpWord to file.
*/
public function save(string $filename): void
{
$filename = $this->getTempFile($filename);
$zip = $this->getZipArchive($filename);

// Add section media files
$sectionMedia = Media::getElements('section');
if (!empty($sectionMedia)) {
$this->addFilesToPackage($zip, $sectionMedia);
}

// Write parts
foreach ($this->parts as $partName => $fileName) {
if ($fileName === '') {
continue;
}
$part = $this->getWriterPart($partName);
if (!$part instanceof AbstractPart) {
continue;
}

$zip->addFromString($fileName, $part->write());
}

// Close zip archive and cleanup temp file
$zip->close();
$this->cleanupTempFile();
}
}
63 changes: 63 additions & 0 deletions src/PhpWord/Writer/EPub3/Part/AbstractPart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Writer\EPub3\Part;

use PhpOffice\PhpWord\Writer\AbstractWriter;

/**
* Abstract class for EPub3 parts.
*/
abstract class AbstractPart
{
/**
* Parent writer.
*
* @var AbstractWriter
*/
protected $parentWriter;

/**
* Set parent writer.
*
* @return self
*/
public function setParentWriter(AbstractWriter $writer)
{
$this->parentWriter = $writer;

return $this;
}

/**
* Get parent writer.
*
* @return AbstractWriter
*/
public function getParentWriter()
{
return $this->parentWriter;
}

/**
* Write part content.
*
* @return string
*/
abstract public function write();
}
49 changes: 49 additions & 0 deletions src/PhpWord/Writer/EPub3/Part/Content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Writer\EPub3\Part;

/**
* Class for EPub3 content part.
*/
class Content extends AbstractPart
{
/**
* Write part content.
*
* @return string
*/
public function write()
{
$content = '<?xml version="1.0" encoding="UTF-8"?>';
$content .= '<package xmlns="http://www.idpf.org/2007/opf" version="3.0">';
$content .= '<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">';
$content .= '<dc:title>Sample EPub3 Document</dc:title>';
$content .= '<dc:language>en</dc:language>';
$content .= '</metadata>';
$content .= '<manifest>';
$content .= '<item id="content" href="content.xhtml" media-type="application/xhtml+xml"/>';
$content .= '</manifest>';
$content .= '<spine>';
$content .= '<itemref idref="content"/>';
$content .= '</spine>';
$content .= '</package>';

return $content;
}
}
42 changes: 42 additions & 0 deletions src/PhpWord/Writer/EPub3/Part/Manifest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Writer\EPub3\Part;

/**
* Class for EPub3 manifest part.
*/
class Manifest extends AbstractPart
{
/**
* Write part content.
*
* @return string
*/
public function write()
{
$content = '<?xml version="1.0" encoding="UTF-8"?>';
$content .= '<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">';
$content .= '<rootfiles>';
$content .= '<rootfile full-path="content.opf" media-type="application/oebps-package+xml"/>';
$content .= '</rootfiles>';
$content .= '</container>';

return $content;
}
}
43 changes: 43 additions & 0 deletions src/PhpWord/Writer/EPub3/Part/Meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Writer\EPub3\Part;

/**
* Class for EPub3 metadata part.
*/
class Meta extends AbstractPart
{
/**
* Write part content.
*
* @return string
*/
public function write()
{
$content = '<?xml version="1.0" encoding="UTF-8"?>';
$content .= '<metadata xmlns="http://www.idpf.org/2007/opf">';
$content .= '<dc:title>Sample EPub3 Document</dc:title>';
$content .= '<dc:language>en</dc:language>';
$content .= '<dc:identifier id="bookid">urn:uuid:12345</dc:identifier>';
$content .= '<meta property="dcterms:modified">2023-01-01T00:00:00Z</meta>';
$content .= '</metadata>';

return $content;
}
}
35 changes: 35 additions & 0 deletions src/PhpWord/Writer/EPub3/Part/Mimetype.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Writer\EPub3\Part;

/**
* Class for EPub3 mimetype part.
*/
class Mimetype extends AbstractPart
{
/**
* Write part content.
*
* @return string
*/
public function write()
{
return 'application/epub+zip';
}
}
Loading
Loading