-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Writer EPub3 : Added support #2724
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f914c95
Feat: Added Epub3 writer support
Sambit003 8f85515
Add: Tests
Sambit003 5f63301
Fix: Code convention
Sambit003 701acf3
Merge branch 'master' into feat/add-epub3-writer
Sambit003 75daa5a
Update: composer.lock file with latest requirement
Sambit003 4918282
Update: composer.lock file with latest requirement
Sambit003 f429579
Update: composer.lock file with latest requirement
Sambit003 514c210
Remove: composer.lock
Sambit003 71b625a
Update: Code conventions for PHP 8.x & Update: .gitignore to include …
Sambit003 aa19421
Add: Text & Image Element
Sambit003 9929f8f
Improvement of Epub3 elements code
Sambit003 6b58664
Add: Unit tests for full EPub3 codebase
Sambit003 52d1f18
Fix: Code convention errors for EPub3 Unit tests
Sambit003 b9374b0
Fix: Added the suggestions
Sambit003 d08c8fc
Revert: composer.json changes -> Now again included the gd & zip exte…
Sambit003 5063a36
Update: composer.json
Sambit003 ae7e4fd
Add: Generating Epub samples with adherence to the Epub 3 checking pr…
Sambit003 d804406
Fix: Null type error in php8.x runtime
Sambit003 f8f0c83
Update: Changelog
Sambit003 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.