Merge pull request #1292 from basbl/write-to-stream-support

Support writing to resource handles in all IWriter implementations
This commit is contained in:
Adrien Crivelli 2020-05-16 20:25:53 +09:00 committed by GitHub
commit 8967696095
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 542 additions and 180 deletions

View File

@ -55,7 +55,8 @@
"ext-zlib": "*", "ext-zlib": "*",
"markbaker/complex": "^1.4", "markbaker/complex": "^1.4",
"markbaker/matrix": "^1.2", "markbaker/matrix": "^1.2",
"psr/simple-cache": "^1.0" "psr/simple-cache": "^1.0",
"maennchen/zipstream-php": "^2.0"
}, },
"require-dev": { "require-dev": {
"dompdf/dompdf": "^0.8.5", "dompdf/dompdf": "^0.8.5",

344
composer.lock generated
View File

@ -4,8 +4,69 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "9c60146d8c78c13d2610a2cec23339a2", "content-hash": "ab06908c3ff8187971def16c578f1ced",
"packages": [ "packages": [
{
"name": "maennchen/zipstream-php",
"version": "2.0.0",
"source": {
"type": "git",
"url": "https://github.com/maennchen/ZipStream-PHP.git",
"reference": "9ceee828f9620b2e5c075e551ec7ed8a7035ac95"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/maennchen/ZipStream-PHP/zipball/9ceee828f9620b2e5c075e551ec7ed8a7035ac95",
"reference": "9ceee828f9620b2e5c075e551ec7ed8a7035ac95",
"shasum": ""
},
"require": {
"ext-mbstring": "*",
"myclabs/php-enum": "^1.5",
"php": ">= 7.1",
"psr/http-message": "^1.0"
},
"require-dev": {
"ext-zip": "*",
"guzzlehttp/guzzle": ">= 6.3",
"mikey179/vfsstream": "^1.6",
"phpunit/phpunit": ">= 7.5"
},
"type": "library",
"autoload": {
"psr-4": {
"ZipStream\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Paul Duncan",
"email": "pabs@pablotron.org"
},
{
"name": "Jonatan Männchen",
"email": "jonatan@maennchen.ch"
},
{
"name": "Jesse Donat",
"email": "donatj@gmail.com"
},
{
"name": "András Kolesár",
"email": "kolesar@kolesar.hu"
}
],
"description": "ZipStream is a library for dynamically streaming dynamic zip files from PHP without writing to the disk at all on the server.",
"keywords": [
"stream",
"zip"
],
"time": "2020-02-23T01:48:39+00:00"
},
{ {
"name": "markbaker/complex", "name": "markbaker/complex",
"version": "1.4.8", "version": "1.4.8",
@ -170,6 +231,102 @@
], ],
"time": "2019-10-06T11:29:25+00:00" "time": "2019-10-06T11:29:25+00:00"
}, },
{
"name": "myclabs/php-enum",
"version": "1.7.6",
"source": {
"type": "git",
"url": "https://github.com/myclabs/php-enum.git",
"reference": "5f36467c7a87e20fbdc51e524fd8f9d1de80187c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/myclabs/php-enum/zipball/5f36467c7a87e20fbdc51e524fd8f9d1de80187c",
"reference": "5f36467c7a87e20fbdc51e524fd8f9d1de80187c",
"shasum": ""
},
"require": {
"ext-json": "*",
"php": ">=7.1"
},
"require-dev": {
"phpunit/phpunit": "^7",
"squizlabs/php_codesniffer": "1.*",
"vimeo/psalm": "^3.8"
},
"type": "library",
"autoload": {
"psr-4": {
"MyCLabs\\Enum\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP Enum contributors",
"homepage": "https://github.com/myclabs/php-enum/graphs/contributors"
}
],
"description": "PHP Enum implementation",
"homepage": "http://github.com/myclabs/php-enum",
"keywords": [
"enum"
],
"time": "2020-02-14T08:15:52+00:00"
},
{
"name": "psr/http-message",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-message.git",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP messages",
"homepage": "https://github.com/php-fig/http-message",
"keywords": [
"http",
"http-message",
"psr",
"psr-7",
"request",
"response"
],
"time": "2016-08-06T14:39:51+00:00"
},
{ {
"name": "psr/simple-cache", "name": "psr/simple-cache",
"version": "1.0.1", "version": "1.0.1",
@ -323,6 +480,12 @@
"Xdebug", "Xdebug",
"performance" "performance"
], ],
"funding": [
{
"url": "https://packagist.com",
"type": "custom"
}
],
"time": "2020-03-01T12:26:26+00:00" "time": "2020-03-01T12:26:26+00:00"
}, },
{ {
@ -667,6 +830,12 @@
} }
], ],
"description": "A tool to automatically fix PHP code style", "description": "A tool to automatically fix PHP code style",
"funding": [
{
"url": "https://github.com/keradus",
"type": "github"
}
],
"time": "2020-04-15T18:51:10+00:00" "time": "2020-04-15T18:51:10+00:00"
}, },
{ {
@ -1703,6 +1872,16 @@
"testing", "testing",
"xunit" "xunit"
], ],
"funding": [
{
"url": "https://phpunit.de/donate.html",
"type": "custom"
},
{
"url": "https://github.com/sebastianbergmann",
"type": "github"
}
],
"time": "2020-04-23T04:39:42+00:00" "time": "2020-04-23T04:39:42+00:00"
}, },
{ {
@ -2566,6 +2745,12 @@
"fpdi", "fpdi",
"pdf" "pdf"
], ],
"funding": [
{
"url": "https://tidelift.com/funding/github/packagist/setasign/fpdi",
"type": "tidelift"
}
],
"time": "2020-03-23T15:53:59+00:00" "time": "2020-03-23T15:53:59+00:00"
}, },
{ {
@ -2693,6 +2878,20 @@
], ],
"description": "Symfony Console Component", "description": "Symfony Console Component",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-03-30T11:42:42+00:00" "time": "2020-03-30T11:42:42+00:00"
}, },
{ {
@ -2763,6 +2962,20 @@
], ],
"description": "Symfony EventDispatcher Component", "description": "Symfony EventDispatcher Component",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-03-27T16:56:45+00:00" "time": "2020-03-27T16:56:45+00:00"
}, },
{ {
@ -2871,6 +3084,20 @@
], ],
"description": "Symfony Filesystem Component", "description": "Symfony Filesystem Component",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-03-27T16:56:45+00:00" "time": "2020-03-27T16:56:45+00:00"
}, },
{ {
@ -2920,6 +3147,20 @@
], ],
"description": "Symfony Finder Component", "description": "Symfony Finder Component",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-03-27T16:56:45+00:00" "time": "2020-03-27T16:56:45+00:00"
}, },
{ {
@ -2974,6 +3215,20 @@
"configuration", "configuration",
"options" "options"
], ],
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-03-27T16:56:45+00:00" "time": "2020-03-27T16:56:45+00:00"
}, },
{ {
@ -3032,6 +3287,20 @@
"polyfill", "polyfill",
"portable" "portable"
], ],
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-02-27T09:26:54+00:00" "time": "2020-02-27T09:26:54+00:00"
}, },
{ {
@ -3091,6 +3360,20 @@
"portable", "portable",
"shim" "shim"
], ],
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-03-09T19:04:49+00:00" "time": "2020-03-09T19:04:49+00:00"
}, },
{ {
@ -3205,6 +3488,20 @@
"portable", "portable",
"shim" "shim"
], ],
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-02-27T09:26:54+00:00" "time": "2020-02-27T09:26:54+00:00"
}, },
{ {
@ -3263,6 +3560,20 @@
"portable", "portable",
"shim" "shim"
], ],
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-02-27T09:26:54+00:00" "time": "2020-02-27T09:26:54+00:00"
}, },
{ {
@ -3312,6 +3623,20 @@
], ],
"description": "Symfony Process Component", "description": "Symfony Process Component",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-03-27T16:56:45+00:00" "time": "2020-03-27T16:56:45+00:00"
}, },
{ {
@ -3420,6 +3745,20 @@
], ],
"description": "Symfony Stopwatch Component", "description": "Symfony Stopwatch Component",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-03-27T16:56:45+00:00" "time": "2020-03-27T16:56:45+00:00"
}, },
{ {
@ -3594,5 +3933,6 @@
"ext-zip": "*", "ext-zip": "*",
"ext-zlib": "*" "ext-zlib": "*"
}, },
"platform-dev": [] "platform-dev": [],
"plugin-api-version": "1.1.0"
} }

View File

@ -22,7 +22,6 @@ namespace PhpOffice\PhpSpreadsheet\Shared\OLE\PPS;
// //
use PhpOffice\PhpSpreadsheet\Shared\OLE; use PhpOffice\PhpSpreadsheet\Shared\OLE;
use PhpOffice\PhpSpreadsheet\Shared\OLE\PPS; use PhpOffice\PhpSpreadsheet\Shared\OLE\PPS;
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
/** /**
* Class for creating Root PPS's for OLE containers. * Class for creating Root PPS's for OLE containers.
@ -33,23 +32,11 @@ use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
*/ */
class Root extends PPS class Root extends PPS
{ {
/**
* Directory for temporary files.
*
* @var string
*/
protected $tempDirectory;
/** /**
* @var resource * @var resource
*/ */
private $fileHandle; private $fileHandle;
/**
* @var string
*/
private $tempFilename;
/** /**
* @var int * @var int
*/ */
@ -67,8 +54,6 @@ class Root extends PPS
*/ */
public function __construct($time_1st, $time_2nd, $raChild) public function __construct($time_1st, $time_2nd, $raChild)
{ {
$this->tempDirectory = \PhpOffice\PhpSpreadsheet\Shared\File::sysGetTempDir();
parent::__construct(null, OLE::ascToUcs('Root Entry'), OLE::OLE_PPS_TYPE_ROOT, null, null, null, $time_1st, $time_2nd, null, $raChild); parent::__construct(null, OLE::ascToUcs('Root Entry'), OLE::OLE_PPS_TYPE_ROOT, null, null, null, $time_1st, $time_2nd, null, $raChild);
} }
@ -79,14 +64,14 @@ class Root extends PPS
* If a resource pointer to a stream created by fopen() is passed * If a resource pointer to a stream created by fopen() is passed
* it will be used, but you have to close such stream by yourself. * it will be used, but you have to close such stream by yourself.
* *
* @param resource|string $filename the name of the file or stream where to save the OLE container * @param resource $fileHandle the name of the file or stream where to save the OLE container
*
* @throws WriterException
* *
* @return bool true on success * @return bool true on success
*/ */
public function save($filename) public function save($fileHandle)
{ {
$this->fileHandle = $fileHandle;
// Initial Setting for saving // Initial Setting for saving
$this->bigBlockSize = pow( $this->bigBlockSize = pow(
2, 2,
@ -97,23 +82,6 @@ class Root extends PPS
(isset($this->smallBlockSize)) ? self::adjust2($this->smallBlockSize) : 6 (isset($this->smallBlockSize)) ? self::adjust2($this->smallBlockSize) : 6
); );
if (is_resource($filename)) {
$this->fileHandle = $filename;
} elseif ($filename == '-' || $filename == '') {
if ($this->tempDirectory === null) {
$this->tempDirectory = \PhpOffice\PhpSpreadsheet\Shared\File::sysGetTempDir();
}
$this->tempFilename = tempnam($this->tempDirectory, 'OLE_PPS_Root');
$this->fileHandle = fopen($this->tempFilename, 'w+b');
if ($this->fileHandle == false) {
throw new WriterException("Can't create temporary file.");
}
} else {
$this->fileHandle = fopen($filename, 'wb');
}
if ($this->fileHandle == false) {
throw new WriterException("Can't open $filename. It may be in use or protected.");
}
// Make an array of PPS's (for Save) // Make an array of PPS's (for Save)
$aList = []; $aList = [];
PPS::_savePpsSetPnt($aList, [$this]); PPS::_savePpsSetPnt($aList, [$this]);
@ -132,10 +100,6 @@ class Root extends PPS
// Write Big Block Depot and BDList and Adding Header informations // Write Big Block Depot and BDList and Adding Header informations
$this->_saveBbd($iSBDcnt, $iBBcnt, $iPPScnt); $this->_saveBbd($iSBDcnt, $iBBcnt, $iPPScnt);
if (!is_resource($filename)) {
fclose($this->fileHandle);
}
return true; return true;
} }

View File

@ -35,6 +35,16 @@ abstract class BaseWriter implements IWriter
*/ */
private $diskCachingDirectory = './'; private $diskCachingDirectory = './';
/**
* @var resource
*/
protected $fileHandle;
/**
* @var bool
*/
private $shouldCloseFile;
public function getIncludeCharts() public function getIncludeCharts()
{ {
return $this->includeCharts; return $this->includeCharts;
@ -83,4 +93,39 @@ abstract class BaseWriter implements IWriter
{ {
return $this->diskCachingDirectory; return $this->diskCachingDirectory;
} }
/**
* Open file handle.
*
* @param resource|string $filename
*/
public function openFileHandle($filename): void
{
if (is_resource($filename)) {
$this->fileHandle = $filename;
$this->shouldCloseFile = false;
return;
}
$fileHandle = fopen($filename, 'wb+');
if ($fileHandle === false) {
throw new Exception('Could not open file ' . $filename . ' for writing.');
}
$this->fileHandle = $fileHandle;
$this->shouldCloseFile = true;
}
/**
* Close file handle only we opened it ourselves.
*/
protected function maybeCloseFileHandle(): void
{
if ($this->shouldCloseFile) {
if (!fclose($this->fileHandle)) {
throw new Exception('Could not close file after writing.');
}
}
}
} }

View File

@ -4,6 +4,7 @@ namespace PhpOffice\PhpSpreadsheet\Writer;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
class Csv extends BaseWriter class Csv extends BaseWriter
{ {
@ -77,7 +78,7 @@ class Csv extends BaseWriter
/** /**
* Save PhpSpreadsheet to file. * Save PhpSpreadsheet to file.
* *
* @param string $pFilename * @param resource|string $pFilename
* *
* @throws Exception * @throws Exception
*/ */
@ -92,9 +93,14 @@ class Csv extends BaseWriter
Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE); Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE);
// Open file // Open file
$fileHandle = fopen($pFilename, 'wb+'); if (is_resource($pFilename)) {
$fileHandle = $pFilename;
} else {
$fileHandle = fopen($pFilename, 'wb+');
}
if ($fileHandle === false) { if ($fileHandle === false) {
throw new Exception("Could not open file $pFilename for writing."); throw new WriterException("Could not open file $pFilename for writing.");
} }
if ($this->excelCompatibility) { if ($this->excelCompatibility) {
@ -125,9 +131,6 @@ class Csv extends BaseWriter
$this->writeLine($fileHandle, $cellsArray[0]); $this->writeLine($fileHandle, $cellsArray[0]);
} }
// Close file
fclose($fileHandle);
Calculation::setArrayReturnType($saveArrayReturnType); Calculation::setArrayReturnType($saveArrayReturnType);
Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog); Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
} }

View File

@ -146,7 +146,7 @@ class Html extends BaseWriter
/** /**
* Save Spreadsheet to file. * Save Spreadsheet to file.
* *
* @param string $pFilename * @param resource|string $pFilename
* *
* @throws WriterException * @throws WriterException
*/ */
@ -164,27 +164,23 @@ class Html extends BaseWriter
$this->buildCSS(!$this->useInlineCss); $this->buildCSS(!$this->useInlineCss);
// Open file // Open file
$fileHandle = fopen($pFilename, 'wb+'); $this->openFileHandle($pFilename);
if ($fileHandle === false) {
throw new WriterException("Could not open file $pFilename for writing.");
}
// Write headers // Write headers
fwrite($fileHandle, $this->generateHTMLHeader(!$this->useInlineCss)); fwrite($this->fileHandle, $this->generateHTMLHeader(!$this->useInlineCss));
// Write navigation (tabs) // Write navigation (tabs)
if ((!$this->isPdf) && ($this->generateSheetNavigationBlock)) { if ((!$this->isPdf) && ($this->generateSheetNavigationBlock)) {
fwrite($fileHandle, $this->generateNavigation()); fwrite($this->fileHandle, $this->generateNavigation());
} }
// Write data // Write data
fwrite($fileHandle, $this->generateSheetData()); fwrite($this->fileHandle, $this->generateSheetData());
// Write footer // Write footer
fwrite($fileHandle, $this->generateHTMLFooter()); fwrite($this->fileHandle, $this->generateHTMLFooter());
// Close file $this->maybeCloseFileHandle();
fclose($fileHandle);
Calculation::setArrayReturnType($saveArrayReturnType); Calculation::setArrayReturnType($saveArrayReturnType);
Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog); Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);

View File

@ -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
*/ */

View File

@ -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
{ {
@ -73,7 +75,7 @@ class Ods extends BaseWriter
/** /**
* Save PhpSpreadsheet to file. * Save PhpSpreadsheet to file.
* *
* @param string $pFilename * @param resource|string $pFilename
* *
* @throws WriterException * @throws WriterException
*/ */
@ -86,64 +88,48 @@ class Ods 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... $this->openFileHandle($pFilename);
$originalFilename = $pFilename;
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
$pFilename = @tempnam(File::sysGetTempDir(), 'phpxltmp');
if ($pFilename == '') {
$pFilename = $originalFilename;
}
}
$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.');
} }
// If a temporary file was used, copy it to the correct file stream $this->maybeCloseFileHandle();
if ($originalFilename != $pFilename) {
if (copy($pFilename, $originalFilename) === false) {
throw new WriterException("Could not copy temporary zip file $pFilename to $originalFilename.");
}
@unlink($pFilename);
}
} }
/** /**
* 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 (!is_resource($this->fileHandle)) {
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);
} }
/** /**

View File

@ -255,28 +255,22 @@ abstract class Pdf extends Html
Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE); Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE);
// Open file // Open file
$fileHandle = fopen($pFilename, 'w'); $this->openFileHandle($pFilename);
if ($fileHandle === false) {
throw new WriterException("Could not open file $pFilename for writing.");
}
// Set PDF // Set PDF
$this->isPdf = true; $this->isPdf = true;
// Build CSS // Build CSS
$this->buildCSS(true); $this->buildCSS(true);
return $fileHandle; return $this->fileHandle;
} }
/** /**
* Save PhpSpreadsheet to PDF file, post-save. * Save PhpSpreadsheet to PDF file, post-save.
*
* @param resource $fileHandle
*/ */
protected function restoreStateAfterSave($fileHandle) protected function restoreStateAfterSave(): void
{ {
// Close file $this->maybeCloseFileHandle();
fclose($fileHandle);
Calculation::setArrayReturnType($this->saveArrayReturnType); Calculation::setArrayReturnType($this->saveArrayReturnType);
} }

View File

@ -73,6 +73,6 @@ class Dompdf extends Pdf
// Write to file // Write to file
fwrite($fileHandle, $pdf->output()); fwrite($fileHandle, $pdf->output());
parent::restoreStateAfterSave($fileHandle); parent::restoreStateAfterSave();
} }
} }

View File

@ -65,7 +65,7 @@ class Mpdf extends Pdf
} }
// Create PDF // Create PDF
$config = ['tempDir' => $this->tempDir]; $config = ['tempDir' => $this->tempDir . '/mpdf'];
$pdf = $this->createExternalWriterInstance($config); $pdf = $this->createExternalWriterInstance($config);
$ortmp = $orientation; $ortmp = $orientation;
$pdf->_setPageSize(strtoupper($paperSize), $ortmp); $pdf->_setPageSize(strtoupper($paperSize), $ortmp);
@ -95,7 +95,7 @@ class Mpdf extends Pdf
// Write to file // Write to file
fwrite($fileHandle, $pdf->Output('', 'S')); fwrite($fileHandle, $pdf->Output('', 'S'));
parent::restoreStateAfterSave($fileHandle); parent::restoreStateAfterSave();
} }
/** /**

View File

@ -93,6 +93,6 @@ class Tcpdf extends Pdf
// Write to file // Write to file
fwrite($fileHandle, $pdf->output($pFilename, 'S')); fwrite($fileHandle, $pdf->output($pFilename, 'S'));
parent::restoreStateAfterSave($fileHandle); parent::restoreStateAfterSave();
} }
} }

View File

@ -115,7 +115,7 @@ class Xls extends BaseWriter
/** /**
* Save Spreadsheet to file. * Save Spreadsheet to file.
* *
* @param string $pFilename * @param resource|string $pFilename
* *
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/ */
@ -221,7 +221,9 @@ class Xls extends BaseWriter
$root = new Root(time(), time(), $arrRootData); $root = new Root(time(), time(), $arrRootData);
// save the OLE file // save the OLE file
$root->save($pFilename); $this->openFileHandle($pFilename);
$root->save($this->fileHandle);
$this->maybeCloseFileHandle();
Functions::setReturnDateType($saveDateReturnType); Functions::setReturnDateType($saveDateReturnType);
Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog); Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);

View File

@ -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
{ {
@ -166,7 +169,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,14 +179,7 @@ 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... $this->openFileHandle($pFilename);
$originalFilename = $pFilename;
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
$pFilename = @tempnam(File::sysGetTempDir(), 'phpxltmp');
if ($pFilename == '') {
$pFilename = $originalFilename;
}
}
$saveDebugLog = Calculation::getInstance($this->spreadSheet)->getDebugLog()->getWriteDebugLog(); $saveDebugLog = Calculation::getInstance($this->spreadSheet)->getDebugLog()->getWriteDebugLog();
Calculation::getInstance($this->spreadSheet)->getDebugLog()->setWriteDebugLog(false); Calculation::getInstance($this->spreadSheet)->getDebugLog()->setWriteDebugLog(false);
@ -207,83 +203,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 +284,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 +309,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 +324,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 +332,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 +378,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 +388,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,17 +396,13 @@ 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.');
} }
// If a temporary file was used, copy it to the correct file stream $this->maybeCloseFileHandle();
if ($originalFilename != $pFilename) {
if (copy($pFilename, $originalFilename) === false) {
throw new WriterException("Could not copy temporary zip file $pFilename to $originalFilename.");
}
@unlink($pFilename);
}
} else { } else {
throw new WriterException('PhpSpreadsheet object unassigned.'); throw new WriterException('PhpSpreadsheet object unassigned.');
} }

View File

@ -0,0 +1,45 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Functional;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
class StreamTest extends TestCase
{
public function providerFormats(): array
{
return [
['Xls'],
['Xlsx'],
['Ods'],
['Csv'],
['Html'],
['Tcpdf'],
['Dompdf'],
['Mpdf'],
];
}
/**
* @dataProvider providerFormats
*
* @param string $format
*/
public function testAllWritersCanWriteToStream(string $format): void
{
$spreadsheet = new Spreadsheet();
$spreadsheet->getActiveSheet()->setCellValue('A1', 'foo');
$writer = IOFactory::createWriter($spreadsheet, $format);
$stream = fopen('php://memory', 'wb+');
self::assertSame(0, fstat($stream)['size']);
$writer->save($stream);
self::assertIsResource($stream, 'should not close the stream for further usage out of PhpSpreadsheet');
self::assertGreaterThan(0, fstat($stream)['size'], 'something should have been written to the stream');
self::assertGreaterThan(0, ftell($stream), 'should not be rewinded, because not all streams support it');
}
}