2012-05-06 23:25:37 +02:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-03-26 16:33:20 +07:00
|
|
|
* PHPWord
|
2012-05-06 23:25:37 +02:00
|
|
|
*
|
2014-03-27 23:55:06 +07:00
|
|
|
* @link https://github.com/PHPOffice/PHPWord
|
|
|
|
|
* @copyright 2014 PHPWord
|
|
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
2012-05-06 23:25:37 +02:00
|
|
|
*/
|
|
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
namespace PhpOffice\PhpWord\Writer;
|
|
|
|
|
|
2014-03-23 10:32:08 +04:00
|
|
|
use PhpOffice\PhpWord\Exceptions\Exception;
|
2014-03-22 10:06:08 +04:00
|
|
|
use PhpOffice\PhpWord\PhpWord;
|
|
|
|
|
use PhpOffice\PhpWord\HashTable;
|
2014-03-28 19:41:33 +07:00
|
|
|
use PhpOffice\PhpWord\Settings;
|
2014-03-22 10:06:08 +04:00
|
|
|
use PhpOffice\PhpWord\Writer\ODText\Content;
|
|
|
|
|
use PhpOffice\PhpWord\Writer\ODText\Manifest;
|
|
|
|
|
use PhpOffice\PhpWord\Writer\ODText\Meta;
|
|
|
|
|
use PhpOffice\PhpWord\Writer\ODText\Mimetype;
|
|
|
|
|
use PhpOffice\PhpWord\Writer\ODText\Styles;
|
|
|
|
|
|
2014-03-24 00:26:10 +07:00
|
|
|
/**
|
|
|
|
|
* ODText writer
|
|
|
|
|
*/
|
2014-03-30 16:31:01 +07:00
|
|
|
class ODText extends Writer implements IWriter
|
2012-05-06 23:25:37 +02:00
|
|
|
{
|
2013-12-11 14:45:44 -05:00
|
|
|
/**
|
|
|
|
|
* Private unique PHPWord_Worksheet_BaseDrawing HashTable
|
|
|
|
|
*
|
2014-03-30 18:51:25 +07:00
|
|
|
* @var HashTable
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
2014-03-30 16:31:01 +07:00
|
|
|
private $drawingHashTable;
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Create new ODText writer
|
2014-03-30 18:51:25 +07:00
|
|
|
* @param PhpWord $phpWord
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
2014-03-22 10:06:08 +04:00
|
|
|
public function __construct(PhpWord $phpWord = null)
|
2013-12-11 14:45:44 -05:00
|
|
|
{
|
2014-03-22 10:06:08 +04:00
|
|
|
// Assign PhpWord
|
|
|
|
|
$this->setPhpWord($phpWord);
|
2013-12-11 14:45:44 -05:00
|
|
|
|
2014-03-30 16:31:01 +07:00
|
|
|
// Set writer parts
|
|
|
|
|
$this->writerParts['content'] = new Content();
|
|
|
|
|
$this->writerParts['manifest'] = new Manifest();
|
|
|
|
|
$this->writerParts['meta'] = new Meta();
|
|
|
|
|
$this->writerParts['mimetype'] = new Mimetype();
|
|
|
|
|
$this->writerParts['styles'] = new Styles();
|
|
|
|
|
foreach ($this->writerParts as $writer) {
|
2013-12-11 14:45:44 -05:00
|
|
|
$writer->setParentWriter($this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set HashTable variables
|
2014-03-30 16:31:01 +07:00
|
|
|
$this->drawingHashTable = new HashTable();
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-22 10:06:08 +04:00
|
|
|
* Save PhpWord to file
|
2013-12-11 14:45:44 -05:00
|
|
|
*
|
2014-03-24 00:26:10 +07:00
|
|
|
* @param string $pFilename
|
2014-03-30 18:51:25 +07:00
|
|
|
* @throws Exception
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
|
|
|
|
public function save($pFilename = null)
|
|
|
|
|
{
|
2014-03-30 16:31:01 +07:00
|
|
|
if (!is_null($this->phpWord)) {
|
|
|
|
|
$pFilename = $this->getTempFile($pFilename);
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
// Create new ZIP file and open it for writing
|
2014-03-28 19:41:33 +07:00
|
|
|
$zipClass = Settings::getZipClass();
|
|
|
|
|
$objZip = new $zipClass();
|
|
|
|
|
|
|
|
|
|
// Retrieve OVERWRITE and CREATE constants from the instantiated zip class
|
|
|
|
|
// This method of accessing constant values from a dynamic class should work with all appropriate versions of PHP
|
|
|
|
|
$ro = new \ReflectionObject($objZip);
|
|
|
|
|
$zipOverWrite = $ro->getConstant('OVERWRITE');
|
|
|
|
|
$zipCreate = $ro->getConstant('CREATE');
|
|
|
|
|
|
|
|
|
|
// Remove any existing file
|
|
|
|
|
if (file_exists($pFilename)) {
|
|
|
|
|
unlink($pFilename);
|
|
|
|
|
}
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
// Try opening the ZIP file
|
2014-03-28 19:41:33 +07:00
|
|
|
if ($objZip->open($pFilename, $zipOverWrite) !== true) {
|
|
|
|
|
if ($objZip->open($pFilename, $zipCreate) !== true) {
|
2013-12-11 14:45:44 -05:00
|
|
|
throw new Exception("Could not open " . $pFilename . " for writing.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add mimetype to ZIP file
|
2014-03-22 16:13:43 +04:00
|
|
|
//@todo Not in \ZipArchive::CM_STORE mode
|
2014-03-30 16:31:01 +07:00
|
|
|
$objZip->addFromString('mimetype', $this->getWriterPart('mimetype')->writeMimetype($this->phpWord));
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
// Add content.xml to ZIP file
|
2014-03-30 16:31:01 +07:00
|
|
|
$objZip->addFromString('content.xml', $this->getWriterPart('content')->writeContent($this->phpWord));
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
// Add meta.xml to ZIP file
|
2014-03-30 16:31:01 +07:00
|
|
|
$objZip->addFromString('meta.xml', $this->getWriterPart('meta')->writeMeta($this->phpWord));
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
// Add styles.xml to ZIP file
|
2014-03-30 16:31:01 +07:00
|
|
|
$objZip->addFromString('styles.xml', $this->getWriterPart('styles')->writeStyles($this->phpWord));
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
// Add META-INF/manifest.xml
|
2014-03-30 16:31:01 +07:00
|
|
|
$objZip->addFromString('META-INF/manifest.xml', $this->getWriterPart('manifest')->writeManifest($this->phpWord));
|
2013-12-11 14:45:44 -05:00
|
|
|
|
2014-03-14 20:56:27 +07:00
|
|
|
// Add media. Has not used yet. Legacy from PHPExcel.
|
|
|
|
|
// @codeCoverageIgnoreStart
|
2013-12-11 14:45:44 -05:00
|
|
|
for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
|
|
|
|
|
if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_Drawing) {
|
|
|
|
|
$imageContents = null;
|
|
|
|
|
$imagePath = $this->getDrawingHashTable()->getByIndex($i)->getPath();
|
|
|
|
|
|
|
|
|
|
if (strpos($imagePath, 'zip://') !== false) {
|
|
|
|
|
$imagePath = substr($imagePath, 6);
|
|
|
|
|
$imagePathSplitted = explode('#', $imagePath);
|
|
|
|
|
|
2014-03-28 19:41:33 +07:00
|
|
|
$zipClass = Settings::getZipClass();
|
|
|
|
|
$imageZip = new $zipClass();
|
2013-12-11 14:45:44 -05:00
|
|
|
$imageZip->open($imagePathSplitted[0]);
|
|
|
|
|
$imageContents = $imageZip->getFromName($imagePathSplitted[1]);
|
|
|
|
|
$imageZip->close();
|
|
|
|
|
unset($imageZip);
|
|
|
|
|
} else {
|
|
|
|
|
$imageContents = file_get_contents($imagePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$objZip->addFromString('Pictures/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
|
2014-03-11 21:44:54 +07:00
|
|
|
} elseif ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_MemoryDrawing) {
|
2013-12-11 14:45:44 -05:00
|
|
|
ob_start();
|
|
|
|
|
call_user_func(
|
|
|
|
|
$this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(),
|
|
|
|
|
$this->getDrawingHashTable()->getByIndex($i)->getImageResource()
|
|
|
|
|
);
|
|
|
|
|
$imageContents = ob_get_contents();
|
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
|
|
$objZip->addFromString('Pictures/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-15 13:25:42 +07:00
|
|
|
// @codeCoverageIgnoreEnd
|
2013-12-11 14:45:44 -05:00
|
|
|
|
|
|
|
|
// Close file
|
|
|
|
|
if ($objZip->close() === false) {
|
|
|
|
|
throw new Exception("Could not close zip file $pFilename.");
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-30 16:31:01 +07:00
|
|
|
$this->cleanupTempFile();
|
2013-12-11 14:45:44 -05:00
|
|
|
} else {
|
2014-03-23 10:32:08 +04:00
|
|
|
throw new Exception("PhpWord object unassigned.");
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get PHPWord_Worksheet_BaseDrawing HashTable
|
|
|
|
|
*
|
2014-03-30 18:51:25 +07:00
|
|
|
* @return HashTable
|
2013-12-11 14:45:44 -05:00
|
|
|
*/
|
|
|
|
|
public function getDrawingHashTable()
|
|
|
|
|
{
|
2014-03-30 16:31:01 +07:00
|
|
|
return $this->drawingHashTable;
|
2013-12-11 14:45:44 -05:00
|
|
|
}
|
2014-03-11 19:26:56 +07:00
|
|
|
}
|