Add support for streaming zip files in the xlsx and ods writers
This commit is contained in:
parent
1bcdf15533
commit
ccb49de301
|
@ -59,7 +59,7 @@ interface IWriter
|
||||||
/**
|
/**
|
||||||
* Save PhpSpreadsheet to file.
|
* Save PhpSpreadsheet to file.
|
||||||
*
|
*
|
||||||
* @param string $pFilename Name of the file to save
|
* @param resource|string $pFilename Name of the file to save
|
||||||
*
|
*
|
||||||
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
|
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -12,7 +12,9 @@ use PhpOffice\PhpSpreadsheet\Writer\Ods\Mimetype;
|
||||||
use PhpOffice\PhpSpreadsheet\Writer\Ods\Settings;
|
use PhpOffice\PhpSpreadsheet\Writer\Ods\Settings;
|
||||||
use PhpOffice\PhpSpreadsheet\Writer\Ods\Styles;
|
use PhpOffice\PhpSpreadsheet\Writer\Ods\Styles;
|
||||||
use PhpOffice\PhpSpreadsheet\Writer\Ods\Thumbnails;
|
use PhpOffice\PhpSpreadsheet\Writer\Ods\Thumbnails;
|
||||||
use ZipArchive;
|
use ZipStream\Exception\OverflowException;
|
||||||
|
use ZipStream\Option\Archive;
|
||||||
|
use ZipStream\ZipStream;
|
||||||
|
|
||||||
class Ods extends BaseWriter
|
class Ods extends BaseWriter
|
||||||
{
|
{
|
||||||
|
@ -30,6 +32,11 @@ class Ods extends BaseWriter
|
||||||
*/
|
*/
|
||||||
private $spreadSheet;
|
private $spreadSheet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool|resource
|
||||||
|
*/
|
||||||
|
private $fileHandle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Ods.
|
* Create a new Ods.
|
||||||
*
|
*
|
||||||
|
@ -73,7 +80,7 @@ class Ods extends BaseWriter
|
||||||
/**
|
/**
|
||||||
* Save PhpSpreadsheet to file.
|
* Save PhpSpreadsheet to file.
|
||||||
*
|
*
|
||||||
* @param string $pFilename
|
* @param resource|string $pFilename
|
||||||
*
|
*
|
||||||
* @throws WriterException
|
* @throws WriterException
|
||||||
*/
|
*/
|
||||||
|
@ -88,31 +95,41 @@ class Ods extends BaseWriter
|
||||||
|
|
||||||
// If $pFilename is php://output or php://stdout, make it a temporary file...
|
// If $pFilename is php://output or php://stdout, make it a temporary file...
|
||||||
$originalFilename = $pFilename;
|
$originalFilename = $pFilename;
|
||||||
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
|
if (is_resource($pFilename)) {
|
||||||
|
$this->fileHandle = $pFilename;
|
||||||
|
} elseif (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
|
||||||
|
// If $pFilename is php://output or php://stdout, make it a temporary file...
|
||||||
$pFilename = @tempnam(File::sysGetTempDir(), 'phpxltmp');
|
$pFilename = @tempnam(File::sysGetTempDir(), 'phpxltmp');
|
||||||
if ($pFilename == '') {
|
if ($pFilename == '') {
|
||||||
$pFilename = $originalFilename;
|
$pFilename = $originalFilename;
|
||||||
}
|
}
|
||||||
|
$this->fileHandle = fopen($pFilename, 'wb+');
|
||||||
|
} else {
|
||||||
|
$this->fileHandle = fopen($pFilename, 'wb+');
|
||||||
}
|
}
|
||||||
|
|
||||||
$zip = $this->createZip($pFilename);
|
$zip = $this->createZip();
|
||||||
|
|
||||||
$zip->addFromString('META-INF/manifest.xml', $this->getWriterPart('meta_inf')->writeManifest());
|
$zip->addFile('META-INF/manifest.xml', $this->getWriterPart('meta_inf')->writeManifest());
|
||||||
$zip->addFromString('Thumbnails/thumbnail.png', $this->getWriterPart('thumbnails')->writeThumbnail());
|
$zip->addFile('Thumbnails/thumbnail.png', $this->getWriterPart('thumbnails')->writeThumbnail());
|
||||||
$zip->addFromString('content.xml', $this->getWriterPart('content')->write());
|
$zip->addFile('content.xml', $this->getWriterPart('content')->write());
|
||||||
$zip->addFromString('meta.xml', $this->getWriterPart('meta')->write());
|
$zip->addFile('meta.xml', $this->getWriterPart('meta')->write());
|
||||||
$zip->addFromString('mimetype', $this->getWriterPart('mimetype')->write());
|
$zip->addFile('mimetype', $this->getWriterPart('mimetype')->write());
|
||||||
$zip->addFromString('settings.xml', $this->getWriterPart('settings')->write());
|
$zip->addFile('settings.xml', $this->getWriterPart('settings')->write());
|
||||||
$zip->addFromString('styles.xml', $this->getWriterPart('styles')->write());
|
$zip->addFile('styles.xml', $this->getWriterPart('styles')->write());
|
||||||
|
|
||||||
// Close file
|
// Close file
|
||||||
if ($zip->close() === false) {
|
try {
|
||||||
throw new WriterException("Could not close zip file $pFilename.");
|
$zip->finish();
|
||||||
|
} catch (OverflowException $e) {
|
||||||
|
throw new WriterException('Could not close resource.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rewind($this->fileHandle);
|
||||||
|
|
||||||
// If a temporary file was used, copy it to the correct file stream
|
// If a temporary file was used, copy it to the correct file stream
|
||||||
if ($originalFilename != $pFilename) {
|
if ($originalFilename != $pFilename) {
|
||||||
if (copy($pFilename, $originalFilename) === false) {
|
if (stream_copy_to_stream($this->fileHandle, fopen($originalFilename, 'wb+')) === false) {
|
||||||
throw new WriterException("Could not copy temporary zip file $pFilename to $originalFilename.");
|
throw new WriterException("Could not copy temporary zip file $pFilename to $originalFilename.");
|
||||||
}
|
}
|
||||||
@unlink($pFilename);
|
@unlink($pFilename);
|
||||||
|
@ -122,28 +139,23 @@ class Ods extends BaseWriter
|
||||||
/**
|
/**
|
||||||
* Create zip object.
|
* Create zip object.
|
||||||
*
|
*
|
||||||
* @param string $pFilename
|
|
||||||
*
|
|
||||||
* @throws WriterException
|
* @throws WriterException
|
||||||
*
|
*
|
||||||
* @return ZipArchive
|
* @return ZipStream
|
||||||
*/
|
*/
|
||||||
private function createZip($pFilename)
|
private function createZip()
|
||||||
{
|
{
|
||||||
// Create new ZIP file and open it for writing
|
|
||||||
$zip = new ZipArchive();
|
|
||||||
|
|
||||||
if (file_exists($pFilename)) {
|
|
||||||
unlink($pFilename);
|
|
||||||
}
|
|
||||||
// Try opening the ZIP file
|
// Try opening the ZIP file
|
||||||
if ($zip->open($pFilename, ZipArchive::OVERWRITE) !== true) {
|
if ($this->fileHandle === false) {
|
||||||
if ($zip->open($pFilename, ZipArchive::CREATE) !== true) {
|
throw new WriterException('Could not open resource for writing.');
|
||||||
throw new WriterException("Could not open $pFilename for writing.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $zip;
|
// Create new ZIP stream
|
||||||
|
$options = new Archive();
|
||||||
|
$options->setEnableZip64(false);
|
||||||
|
$options->setOutputStream($this->fileHandle);
|
||||||
|
|
||||||
|
return new ZipStream(null, $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -24,6 +24,9 @@ use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Theme;
|
||||||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Workbook;
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Workbook;
|
||||||
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet;
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet;
|
||||||
use ZipArchive;
|
use ZipArchive;
|
||||||
|
use ZipStream\Exception\OverflowException;
|
||||||
|
use ZipStream\Option\Archive;
|
||||||
|
use ZipStream\ZipStream;
|
||||||
|
|
||||||
class Xlsx extends BaseWriter
|
class Xlsx extends BaseWriter
|
||||||
{
|
{
|
||||||
|
@ -104,6 +107,11 @@ class Xlsx extends BaseWriter
|
||||||
*/
|
*/
|
||||||
private $drawingHashTable;
|
private $drawingHashTable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool|resource
|
||||||
|
*/
|
||||||
|
private $fileHandle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Xlsx Writer.
|
* Create a new Xlsx Writer.
|
||||||
*
|
*
|
||||||
|
@ -166,7 +174,7 @@ class Xlsx extends BaseWriter
|
||||||
/**
|
/**
|
||||||
* Save PhpSpreadsheet to file.
|
* Save PhpSpreadsheet to file.
|
||||||
*
|
*
|
||||||
* @param string $pFilename
|
* @param resource|string $pFilename
|
||||||
*
|
*
|
||||||
* @throws WriterException
|
* @throws WriterException
|
||||||
*/
|
*/
|
||||||
|
@ -176,13 +184,24 @@ class Xlsx extends BaseWriter
|
||||||
// garbage collect
|
// garbage collect
|
||||||
$this->spreadSheet->garbageCollect();
|
$this->spreadSheet->garbageCollect();
|
||||||
|
|
||||||
// If $pFilename is php://output or php://stdout, make it a temporary file...
|
|
||||||
$originalFilename = $pFilename;
|
$originalFilename = $pFilename;
|
||||||
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
|
|
||||||
|
if (is_resource($pFilename)) {
|
||||||
|
$this->fileHandle = $pFilename;
|
||||||
|
} elseif (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
|
||||||
|
// If $pFilename is php://output or php://stdout, make it a temporary file...
|
||||||
$pFilename = @tempnam(File::sysGetTempDir(), 'phpxltmp');
|
$pFilename = @tempnam(File::sysGetTempDir(), 'phpxltmp');
|
||||||
if ($pFilename == '') {
|
if ($pFilename == '') {
|
||||||
$pFilename = $originalFilename;
|
$pFilename = $originalFilename;
|
||||||
}
|
}
|
||||||
|
$this->fileHandle = fopen($pFilename, 'wb+');
|
||||||
|
} else {
|
||||||
|
$this->fileHandle = fopen($pFilename, 'wb+');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try opening the ZIP file
|
||||||
|
if ($this->fileHandle === false) {
|
||||||
|
throw new WriterException('Could not open resource for writing.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$saveDebugLog = Calculation::getInstance($this->spreadSheet)->getDebugLog()->getWriteDebugLog();
|
$saveDebugLog = Calculation::getInstance($this->spreadSheet)->getDebugLog()->getWriteDebugLog();
|
||||||
|
@ -207,83 +226,77 @@ class Xlsx extends BaseWriter
|
||||||
// Create drawing dictionary
|
// Create drawing dictionary
|
||||||
$this->drawingHashTable->addFromSource($this->getWriterPart('Drawing')->allDrawings($this->spreadSheet));
|
$this->drawingHashTable->addFromSource($this->getWriterPart('Drawing')->allDrawings($this->spreadSheet));
|
||||||
|
|
||||||
$zip = new ZipArchive();
|
$options = new Archive();
|
||||||
|
$options->setEnableZip64(false);
|
||||||
|
$options->setOutputStream($this->fileHandle);
|
||||||
|
|
||||||
if (file_exists($pFilename)) {
|
$zip = new ZipStream(null, $options);
|
||||||
unlink($pFilename);
|
|
||||||
}
|
|
||||||
// Try opening the ZIP file
|
|
||||||
if ($zip->open($pFilename, ZipArchive::OVERWRITE) !== true) {
|
|
||||||
if ($zip->open($pFilename, ZipArchive::CREATE) !== true) {
|
|
||||||
throw new WriterException('Could not open ' . $pFilename . ' for writing.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add [Content_Types].xml to ZIP file
|
// Add [Content_Types].xml to ZIP file
|
||||||
$zip->addFromString('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->spreadSheet, $this->includeCharts));
|
$zip->addFile('[Content_Types].xml', $this->getWriterPart('ContentTypes')->writeContentTypes($this->spreadSheet, $this->includeCharts));
|
||||||
|
|
||||||
//if hasMacros, add the vbaProject.bin file, Certificate file(if exists)
|
//if hasMacros, add the vbaProject.bin file, Certificate file(if exists)
|
||||||
if ($this->spreadSheet->hasMacros()) {
|
if ($this->spreadSheet->hasMacros()) {
|
||||||
$macrosCode = $this->spreadSheet->getMacrosCode();
|
$macrosCode = $this->spreadSheet->getMacrosCode();
|
||||||
if ($macrosCode !== null) {
|
if ($macrosCode !== null) {
|
||||||
// we have the code ?
|
// we have the code ?
|
||||||
$zip->addFromString('xl/vbaProject.bin', $macrosCode); //allways in 'xl', allways named vbaProject.bin
|
$zip->addFile('xl/vbaProject.bin', $macrosCode); //allways in 'xl', allways named vbaProject.bin
|
||||||
if ($this->spreadSheet->hasMacrosCertificate()) {
|
if ($this->spreadSheet->hasMacrosCertificate()) {
|
||||||
//signed macros ?
|
//signed macros ?
|
||||||
// Yes : add the certificate file and the related rels file
|
// Yes : add the certificate file and the related rels file
|
||||||
$zip->addFromString('xl/vbaProjectSignature.bin', $this->spreadSheet->getMacrosCertificate());
|
$zip->addFile('xl/vbaProjectSignature.bin', $this->spreadSheet->getMacrosCertificate());
|
||||||
$zip->addFromString('xl/_rels/vbaProject.bin.rels', $this->getWriterPart('RelsVBA')->writeVBARelationships($this->spreadSheet));
|
$zip->addFile('xl/_rels/vbaProject.bin.rels', $this->getWriterPart('RelsVBA')->writeVBARelationships($this->spreadSheet));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//a custom UI in this workbook ? add it ("base" xml and additional objects (pictures) and rels)
|
//a custom UI in this workbook ? add it ("base" xml and additional objects (pictures) and rels)
|
||||||
if ($this->spreadSheet->hasRibbon()) {
|
if ($this->spreadSheet->hasRibbon()) {
|
||||||
$tmpRibbonTarget = $this->spreadSheet->getRibbonXMLData('target');
|
$tmpRibbonTarget = $this->spreadSheet->getRibbonXMLData('target');
|
||||||
$zip->addFromString($tmpRibbonTarget, $this->spreadSheet->getRibbonXMLData('data'));
|
$zip->addFile($tmpRibbonTarget, $this->spreadSheet->getRibbonXMLData('data'));
|
||||||
if ($this->spreadSheet->hasRibbonBinObjects()) {
|
if ($this->spreadSheet->hasRibbonBinObjects()) {
|
||||||
$tmpRootPath = dirname($tmpRibbonTarget) . '/';
|
$tmpRootPath = dirname($tmpRibbonTarget) . '/';
|
||||||
$ribbonBinObjects = $this->spreadSheet->getRibbonBinObjects('data'); //the files to write
|
$ribbonBinObjects = $this->spreadSheet->getRibbonBinObjects('data'); //the files to write
|
||||||
foreach ($ribbonBinObjects as $aPath => $aContent) {
|
foreach ($ribbonBinObjects as $aPath => $aContent) {
|
||||||
$zip->addFromString($tmpRootPath . $aPath, $aContent);
|
$zip->addFile($tmpRootPath . $aPath, $aContent);
|
||||||
}
|
}
|
||||||
//the rels for files
|
//the rels for files
|
||||||
$zip->addFromString($tmpRootPath . '_rels/' . basename($tmpRibbonTarget) . '.rels', $this->getWriterPart('RelsRibbonObjects')->writeRibbonRelationships($this->spreadSheet));
|
$zip->addFile($tmpRootPath . '_rels/' . basename($tmpRibbonTarget) . '.rels', $this->getWriterPart('RelsRibbonObjects')->writeRibbonRelationships($this->spreadSheet));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add relationships to ZIP file
|
// Add relationships to ZIP file
|
||||||
$zip->addFromString('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->spreadSheet));
|
$zip->addFile('_rels/.rels', $this->getWriterPart('Rels')->writeRelationships($this->spreadSheet));
|
||||||
$zip->addFromString('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->spreadSheet));
|
$zip->addFile('xl/_rels/workbook.xml.rels', $this->getWriterPart('Rels')->writeWorkbookRelationships($this->spreadSheet));
|
||||||
|
|
||||||
// Add document properties to ZIP file
|
// Add document properties to ZIP file
|
||||||
$zip->addFromString('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->spreadSheet));
|
$zip->addFile('docProps/app.xml', $this->getWriterPart('DocProps')->writeDocPropsApp($this->spreadSheet));
|
||||||
$zip->addFromString('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->spreadSheet));
|
$zip->addFile('docProps/core.xml', $this->getWriterPart('DocProps')->writeDocPropsCore($this->spreadSheet));
|
||||||
$customPropertiesPart = $this->getWriterPart('DocProps')->writeDocPropsCustom($this->spreadSheet);
|
$customPropertiesPart = $this->getWriterPart('DocProps')->writeDocPropsCustom($this->spreadSheet);
|
||||||
if ($customPropertiesPart !== null) {
|
if ($customPropertiesPart !== null) {
|
||||||
$zip->addFromString('docProps/custom.xml', $customPropertiesPart);
|
$zip->addFile('docProps/custom.xml', $customPropertiesPart);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add theme to ZIP file
|
// Add theme to ZIP file
|
||||||
$zip->addFromString('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->spreadSheet));
|
$zip->addFile('xl/theme/theme1.xml', $this->getWriterPart('Theme')->writeTheme($this->spreadSheet));
|
||||||
|
|
||||||
// Add string table to ZIP file
|
// Add string table to ZIP file
|
||||||
$zip->addFromString('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->stringTable));
|
$zip->addFile('xl/sharedStrings.xml', $this->getWriterPart('StringTable')->writeStringTable($this->stringTable));
|
||||||
|
|
||||||
// Add styles to ZIP file
|
// Add styles to ZIP file
|
||||||
$zip->addFromString('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->spreadSheet));
|
$zip->addFile('xl/styles.xml', $this->getWriterPart('Style')->writeStyles($this->spreadSheet));
|
||||||
|
|
||||||
// Add workbook to ZIP file
|
// Add workbook to ZIP file
|
||||||
$zip->addFromString('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->spreadSheet, $this->preCalculateFormulas));
|
$zip->addFile('xl/workbook.xml', $this->getWriterPart('Workbook')->writeWorkbook($this->spreadSheet, $this->preCalculateFormulas));
|
||||||
|
|
||||||
$chartCount = 0;
|
$chartCount = 0;
|
||||||
// Add worksheets
|
// Add worksheets
|
||||||
for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) {
|
for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) {
|
||||||
$zip->addFromString('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->spreadSheet->getSheet($i), $this->stringTable, $this->includeCharts));
|
$zip->addFile('xl/worksheets/sheet' . ($i + 1) . '.xml', $this->getWriterPart('Worksheet')->writeWorksheet($this->spreadSheet->getSheet($i), $this->stringTable, $this->includeCharts));
|
||||||
if ($this->includeCharts) {
|
if ($this->includeCharts) {
|
||||||
$charts = $this->spreadSheet->getSheet($i)->getChartCollection();
|
$charts = $this->spreadSheet->getSheet($i)->getChartCollection();
|
||||||
if (count($charts) > 0) {
|
if (count($charts) > 0) {
|
||||||
foreach ($charts as $chart) {
|
foreach ($charts as $chart) {
|
||||||
$zip->addFromString('xl/charts/chart' . ($chartCount + 1) . '.xml', $this->getWriterPart('Chart')->writeChart($chart, $this->preCalculateFormulas));
|
$zip->addFile('xl/charts/chart' . ($chartCount + 1) . '.xml', $this->getWriterPart('Chart')->writeChart($chart, $this->preCalculateFormulas));
|
||||||
++$chartCount;
|
++$chartCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -294,19 +307,19 @@ class Xlsx extends BaseWriter
|
||||||
// Add worksheet relationships (drawings, ...)
|
// Add worksheet relationships (drawings, ...)
|
||||||
for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) {
|
for ($i = 0; $i < $this->spreadSheet->getSheetCount(); ++$i) {
|
||||||
// Add relationships
|
// Add relationships
|
||||||
$zip->addFromString('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->spreadSheet->getSheet($i), ($i + 1), $this->includeCharts));
|
$zip->addFile('xl/worksheets/_rels/sheet' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeWorksheetRelationships($this->spreadSheet->getSheet($i), ($i + 1), $this->includeCharts));
|
||||||
|
|
||||||
// Add unparsedLoadedData
|
// Add unparsedLoadedData
|
||||||
$sheetCodeName = $this->spreadSheet->getSheet($i)->getCodeName();
|
$sheetCodeName = $this->spreadSheet->getSheet($i)->getCodeName();
|
||||||
$unparsedLoadedData = $this->spreadSheet->getUnparsedLoadedData();
|
$unparsedLoadedData = $this->spreadSheet->getUnparsedLoadedData();
|
||||||
if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['ctrlProps'])) {
|
if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['ctrlProps'])) {
|
||||||
foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['ctrlProps'] as $ctrlProp) {
|
foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['ctrlProps'] as $ctrlProp) {
|
||||||
$zip->addFromString($ctrlProp['filePath'], $ctrlProp['content']);
|
$zip->addFile($ctrlProp['filePath'], $ctrlProp['content']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['printerSettings'])) {
|
if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['printerSettings'])) {
|
||||||
foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['printerSettings'] as $ctrlProp) {
|
foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['printerSettings'] as $ctrlProp) {
|
||||||
$zip->addFromString($ctrlProp['filePath'], $ctrlProp['content']);
|
$zip->addFile($ctrlProp['filePath'], $ctrlProp['content']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -319,13 +332,13 @@ class Xlsx extends BaseWriter
|
||||||
// Add drawing and image relationship parts
|
// Add drawing and image relationship parts
|
||||||
if (($drawingCount > 0) || ($chartCount > 0)) {
|
if (($drawingCount > 0) || ($chartCount > 0)) {
|
||||||
// Drawing relationships
|
// Drawing relationships
|
||||||
$zip->addFromString('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->spreadSheet->getSheet($i), $chartRef1, $this->includeCharts));
|
$zip->addFile('xl/drawings/_rels/drawing' . ($i + 1) . '.xml.rels', $this->getWriterPart('Rels')->writeDrawingRelationships($this->spreadSheet->getSheet($i), $chartRef1, $this->includeCharts));
|
||||||
|
|
||||||
// Drawings
|
// Drawings
|
||||||
$zip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->spreadSheet->getSheet($i), $this->includeCharts));
|
$zip->addFile('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->spreadSheet->getSheet($i), $this->includeCharts));
|
||||||
} elseif (isset($unparsedLoadedData['sheets'][$sheetCodeName]['drawingAlternateContents'])) {
|
} elseif (isset($unparsedLoadedData['sheets'][$sheetCodeName]['drawingAlternateContents'])) {
|
||||||
// Drawings
|
// Drawings
|
||||||
$zip->addFromString('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->spreadSheet->getSheet($i), $this->includeCharts));
|
$zip->addFile('xl/drawings/drawing' . ($i + 1) . '.xml', $this->getWriterPart('Drawing')->writeDrawings($this->spreadSheet->getSheet($i), $this->includeCharts));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add unparsed drawings
|
// Add unparsed drawings
|
||||||
|
@ -334,7 +347,7 @@ class Xlsx extends BaseWriter
|
||||||
$drawingFile = array_search($relId, $unparsedLoadedData['sheets'][$sheetCodeName]['drawingOriginalIds']);
|
$drawingFile = array_search($relId, $unparsedLoadedData['sheets'][$sheetCodeName]['drawingOriginalIds']);
|
||||||
if ($drawingFile !== false) {
|
if ($drawingFile !== false) {
|
||||||
$drawingFile = ltrim($drawingFile, '.');
|
$drawingFile = ltrim($drawingFile, '.');
|
||||||
$zip->addFromString('xl' . $drawingFile, $drawingXml);
|
$zip->addFile('xl' . $drawingFile, $drawingXml);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -342,30 +355,30 @@ class Xlsx extends BaseWriter
|
||||||
// Add comment relationship parts
|
// Add comment relationship parts
|
||||||
if (count($this->spreadSheet->getSheet($i)->getComments()) > 0) {
|
if (count($this->spreadSheet->getSheet($i)->getComments()) > 0) {
|
||||||
// VML Comments
|
// VML Comments
|
||||||
$zip->addFromString('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->spreadSheet->getSheet($i)));
|
$zip->addFile('xl/drawings/vmlDrawing' . ($i + 1) . '.vml', $this->getWriterPart('Comments')->writeVMLComments($this->spreadSheet->getSheet($i)));
|
||||||
|
|
||||||
// Comments
|
// Comments
|
||||||
$zip->addFromString('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->spreadSheet->getSheet($i)));
|
$zip->addFile('xl/comments' . ($i + 1) . '.xml', $this->getWriterPart('Comments')->writeComments($this->spreadSheet->getSheet($i)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add unparsed relationship parts
|
// Add unparsed relationship parts
|
||||||
if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['vmlDrawings'])) {
|
if (isset($unparsedLoadedData['sheets'][$sheetCodeName]['vmlDrawings'])) {
|
||||||
foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['vmlDrawings'] as $vmlDrawing) {
|
foreach ($unparsedLoadedData['sheets'][$sheetCodeName]['vmlDrawings'] as $vmlDrawing) {
|
||||||
$zip->addFromString($vmlDrawing['filePath'], $vmlDrawing['content']);
|
$zip->addFile($vmlDrawing['filePath'], $vmlDrawing['content']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add header/footer relationship parts
|
// Add header/footer relationship parts
|
||||||
if (count($this->spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) {
|
if (count($this->spreadSheet->getSheet($i)->getHeaderFooter()->getImages()) > 0) {
|
||||||
// VML Drawings
|
// VML Drawings
|
||||||
$zip->addFromString('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->spreadSheet->getSheet($i)));
|
$zip->addFile('xl/drawings/vmlDrawingHF' . ($i + 1) . '.vml', $this->getWriterPart('Drawing')->writeVMLHeaderFooterImages($this->spreadSheet->getSheet($i)));
|
||||||
|
|
||||||
// VML Drawing relationships
|
// VML Drawing relationships
|
||||||
$zip->addFromString('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->spreadSheet->getSheet($i)));
|
$zip->addFile('xl/drawings/_rels/vmlDrawingHF' . ($i + 1) . '.vml.rels', $this->getWriterPart('Rels')->writeHeaderFooterDrawingRelationships($this->spreadSheet->getSheet($i)));
|
||||||
|
|
||||||
// Media
|
// Media
|
||||||
foreach ($this->spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) {
|
foreach ($this->spreadSheet->getSheet($i)->getHeaderFooter()->getImages() as $image) {
|
||||||
$zip->addFromString('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath()));
|
$zip->addFile('xl/media/' . $image->getIndexedFilename(), file_get_contents($image->getPath()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -388,7 +401,7 @@ class Xlsx extends BaseWriter
|
||||||
$imageContents = file_get_contents($imagePath);
|
$imageContents = file_get_contents($imagePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
$zip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
|
$zip->addFile('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
|
||||||
} elseif ($this->getDrawingHashTable()->getByIndex($i) instanceof MemoryDrawing) {
|
} elseif ($this->getDrawingHashTable()->getByIndex($i) instanceof MemoryDrawing) {
|
||||||
ob_start();
|
ob_start();
|
||||||
call_user_func(
|
call_user_func(
|
||||||
|
@ -398,7 +411,7 @@ class Xlsx extends BaseWriter
|
||||||
$imageContents = ob_get_contents();
|
$imageContents = ob_get_contents();
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
|
|
||||||
$zip->addFromString('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
|
$zip->addFile('xl/media/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -406,13 +419,17 @@ class Xlsx extends BaseWriter
|
||||||
Calculation::getInstance($this->spreadSheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
|
Calculation::getInstance($this->spreadSheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
|
||||||
|
|
||||||
// Close file
|
// Close file
|
||||||
if ($zip->close() === false) {
|
try {
|
||||||
throw new WriterException("Could not close zip file $pFilename.");
|
$zip->finish();
|
||||||
|
} catch (OverflowException $e) {
|
||||||
|
throw new WriterException('Could not close resource.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rewind($this->fileHandle);
|
||||||
|
|
||||||
// If a temporary file was used, copy it to the correct file stream
|
// If a temporary file was used, copy it to the correct file stream
|
||||||
if ($originalFilename != $pFilename) {
|
if ($originalFilename != $pFilename) {
|
||||||
if (copy($pFilename, $originalFilename) === false) {
|
if (stream_copy_to_stream($this->fileHandle, fopen($originalFilename, 'wb+')) === false) {
|
||||||
throw new WriterException("Could not copy temporary zip file $pFilename to $originalFilename.");
|
throw new WriterException("Could not copy temporary zip file $pFilename to $originalFilename.");
|
||||||
}
|
}
|
||||||
@unlink($pFilename);
|
@unlink($pFilename);
|
||||||
|
|
Loading…
Reference in New Issue