-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Writer EPub3 : Added support (#2724)
* Feat: Added Epub3 writer support * Add: Tests * Fix: Code convention * Update: composer.lock file with latest requirement * Update: composer.lock file with latest requirement * Update: composer.lock file with latest requirement * Remove: composer.lock * Update: Code conventions for PHP 8.x & Update: .gitignore to include composer.lock (in support of PR #2722 : Autoload) * Add: Text & Image Element * Improvement of Epub3 elements code * Add: Unit tests for full EPub3 codebase * Fix: Code convention errors for EPub3 Unit tests * Fix: Added the suggestions * Revert: composer.json changes -> Now again included the gd & zip extension * Update: composer.json * Add: Generating Epub samples with adherence to the Epub 3 checking procedures * Fix: Null type error in php8.x runtime * Update: Changelog
- Loading branch information
Showing
39 changed files
with
1,695 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ _build | |
/build | ||
phpunit.xml | ||
composer.phar | ||
composer.lock | ||
vendor | ||
/report | ||
/build | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\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', | ||
'Nav' => 'nav.xhtml', | ||
'ContentXhtml' => 'content.xhtml', | ||
]; | ||
foreach (array_keys($this->parts) as $partName) { | ||
$partClass = static::class . '\\Part\\' . $partName; | ||
if (class_exists($partClass)) { | ||
/** @var WriterPartInterface $part */ | ||
$part = new $partClass($partName === 'Content' || $partName === 'ContentXhtml' ? $phpWord : null); | ||
$part->setParentWriter($this); | ||
$this->writerParts[strtolower($partName)] = $part; | ||
} | ||
} | ||
|
||
// 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 mimetype first without compression | ||
$zip->addFromString('mimetype', 'application/epub+zip'); | ||
$zip->addEmptyDir('META-INF'); | ||
|
||
// Add other files | ||
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 | ||
$zip->close(); | ||
|
||
// Cleanup temp file | ||
$this->cleanupTempFile(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?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\Element; | ||
|
||
use PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement as Word2007AbstractElement; | ||
|
||
/** | ||
* Abstract element writer. | ||
* | ||
* @since 0.11.0 | ||
*/ | ||
abstract class AbstractElement extends Word2007AbstractElement | ||
{ | ||
/** | ||
* Get class name of writer element based on read element. | ||
* | ||
* @param \PhpOffice\PhpWord\Element\AbstractElement $element | ||
*/ | ||
public static function getElementClass($element): string | ||
{ | ||
$elementClass = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($element)); | ||
$writerClass = 'PhpOffice\\PhpWord\\Writer\\EPub3\\Element\\' . $elementClass; | ||
if (!class_exists($writerClass)) { | ||
throw new \PhpOffice\PhpWord\Exception\Exception("Writer element class {$writerClass} not found."); | ||
} | ||
|
||
return $writerClass; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpWord\Writer\EPub3\Element; | ||
|
||
use PhpOffice\PhpWord\Element\Image as ImageElement; | ||
|
||
/** | ||
* Image element writer for EPub3. | ||
*/ | ||
class Image extends AbstractElement | ||
{ | ||
/** | ||
* Write element. | ||
*/ | ||
public function write(): void | ||
{ | ||
$xmlWriter = $this->getXmlWriter(); | ||
$xmlWriter->setIndent(false); | ||
$element = $this->getElement(); | ||
if (!$element instanceof ImageElement) { | ||
return; | ||
} | ||
$mediaIndex = $element->getMediaIndex(); | ||
$target = 'media/image' . $mediaIndex . '.' . $element->getImageExtension(); | ||
if (!$this->withoutP) { | ||
$xmlWriter->startElement('p'); | ||
} | ||
$xmlWriter->startElement('img'); | ||
$xmlWriter->writeAttribute('src', $target); | ||
$style = ''; | ||
if ($element->getStyle()->getWidth() !== null) { | ||
$style .= 'width:' . $element->getStyle()->getWidth() . 'px;'; | ||
} | ||
if ($element->getStyle()->getHeight() !== null) { | ||
$style .= 'height:' . $element->getStyle()->getHeight() . 'px;'; | ||
} | ||
if ($style !== '') { | ||
$xmlWriter->writeAttribute('style', $style); | ||
} | ||
$xmlWriter->endElement(); // img | ||
if (!$this->withoutP) { | ||
$xmlWriter->endElement(); // p | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpWord\Writer\EPub3\Element; | ||
|
||
/** | ||
* Text element writer for EPub3. | ||
*/ | ||
class Text extends AbstractElement | ||
{ | ||
/** | ||
* Write element. | ||
*/ | ||
public function write(): void | ||
{ | ||
$xmlWriter = $this->getXmlWriter(); | ||
$xmlWriter->setIndent(true); | ||
$xmlWriter->setIndentString(' '); | ||
$element = $this->getElement(); | ||
if (!$element instanceof \PhpOffice\PhpWord\Element\Text) { | ||
return; | ||
} | ||
|
||
$fontStyle = $element->getFontStyle(); | ||
$paragraphStyle = $element->getParagraphStyle(); | ||
|
||
if (!$this->withoutP) { | ||
$xmlWriter->startElement('p'); | ||
if (is_string($paragraphStyle) && $paragraphStyle !== '') { | ||
$xmlWriter->writeAttribute('class', $paragraphStyle); | ||
} | ||
} | ||
|
||
if (!empty($fontStyle)) { | ||
$xmlWriter->startElement('span'); | ||
if (is_string($fontStyle)) { | ||
$xmlWriter->writeAttribute('class', $fontStyle); | ||
} | ||
} | ||
|
||
$xmlWriter->text($element->getText()); | ||
|
||
if (!empty($fontStyle)) { | ||
$xmlWriter->endElement(); // span | ||
} | ||
|
||
if (!$this->withoutP) { | ||
$xmlWriter->endElement(); // p | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?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; | ||
|
||
use PhpOffice\PhpWord\Exception\Exception; | ||
|
||
/** | ||
* Factory class for EPub3 parts. | ||
*/ | ||
class Part | ||
{ | ||
/** | ||
* Get the fully qualified class name for a specific part type. | ||
* | ||
* @param string $type The type of part (Content, Manifest, Meta, Mimetype) | ||
* | ||
* @return string The fully qualified class name | ||
*/ | ||
public static function getPartClass(string $type): string | ||
{ | ||
$class = 'PhpOffice\\PhpWord\\Writer\\EPub3\\Part\\' . $type; | ||
|
||
if (!class_exists($class)) { | ||
throw new Exception("Invalid part type: {$type}"); | ||
} | ||
|
||
return $class; | ||
} | ||
} |
Oops, something went wrong.