-
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.
- Loading branch information
Showing
50 changed files
with
1,880 additions
and
31 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
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.