GithubHelp home page GithubHelp logo

Comments (12)

PowerKiKi avatar PowerKiKi commented on July 18, 2024 2

My take on this would be to modify the signature of PhpOffice\PhpSpreadsheet\Writer\IWriter::save() to always return the file descriptor of the written file. Then it's up to you to do whatever you need with that file descriptor. The save() method already supports writing to php://output (via temp files for some writers), so we only need to add support for php://memory, which should not be too difficult hopefully.

This would be for convenience only and would not bring any gain of performance, because we would still be creating temp files on disk.

So usages would be something like:

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);

$writer->save('/some/path/to/file.xlsx'); // standard, save to disk
$writer->save('php://output');            // to download the file in the browser
$handle = $writer->save('php://memory');  // to get a file descriptor to the stream in memory

Would this solve your use case ?

from phpspreadsheet.

PowerKiKi avatar PowerKiKi commented on July 18, 2024 2

I mentioned in another issue that an important blocking factor is that ZipArchive does not work with resources. So to implement this properly would mean to replace ZipArchive with something else. It could be a non-trivial work, but it could be worth the effort if it means we can then avoid touching the disk entirely... PR welcome...

from phpspreadsheet.

jbelien avatar jbelien commented on July 18, 2024 2

I never used it (I'll have a look) but seems that ZipStream-PHP could be a solution to replace ZipArchive.

from phpspreadsheet.

Moln avatar Moln commented on July 18, 2024 2

If the file is large. It's best not to use file_get_contents().

@jbelien

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use GuzzleHttp\Psr7\Response;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class ExportXlsxHandler implements RequestHandlerInterface {

    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $file = tempnam(File::sysGetTempDir(), 'phpxltmp');
        $writer = new Xlsx($spreadsheet);
        $writer->save($file);

        return new class($file, $filename) extends Response {
            private $file;

            public function __construct($file, $filename)
            {
                $this->file = $file;
                parent::__construct(
                    200,
                    [
                        'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                        'Content-Disposition' => "attachment;filename=\"{$filename}.xlsx\"",
                        'Cache-Control' => 'max-age=0',
                    ],
                    fopen($file, 'r+')
                );
            }

            public function __destruct()
            {
                @unlink($this->file);
            }
        };
    }
}

from phpspreadsheet.

jbelien avatar jbelien commented on July 18, 2024 1

Hello everyone,

Same issue as mentioned by @lucascorbeaux : I want to be able to use PSR-7 Response object.
It would be really awesome to implement php://memory in save() function like mentioned by @PowerKiKi :

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$handle = $writer->save('php://memory'); // to get a file descriptor to the stream in memory

Meanwhile, if anyone else needs it, here is how I did it using Zend Framework :

    $file = 'phpxltmp.xlsx';

    $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
    $writer->save($file);

    $body = new \Zend\Diactoros\Stream('php://temp', 'w+');
    $body->write(file_get_contents($file));

    unlink($file);

    $response = new \Zend\Diactoros\Response($body));

from phpspreadsheet.

PowerKiKi avatar PowerKiKi commented on July 18, 2024

I am not familiar with the details of PSR-7 yet, and can't find much related to "resource" in the spec. Would you care to detail what you had in mind ? Would it be an implementation of Psr\Http\Message\ResponseInterface ? or usage of that interface ?

from phpspreadsheet.

lenore-corbeaux avatar lenore-corbeaux commented on July 18, 2024

Hi, and sorry I probably wasn't clear. The resource isn't at all part of PSR-7 specs, I was talking about the PHP resource type : http://php.net/manual/en/language.types.resource.php

In this case, it will be a stream resource. What could be a great add to PhpSpreadsheet is to provide a getResource method that returns the stream resource of the current document. If document is provided by stream, it's still possible to use a php://memory stream, for example.

Behing able to retrieve this resource will be really useful especially for PSR-7 integration (PSR-7 Responses need a PHP resource to generate a response body).

from phpspreadsheet.

lenore-corbeaux avatar lenore-corbeaux commented on July 18, 2024

Hi, and thanks for your reply. It will totally solve my case.

from phpspreadsheet.

PowerKiKi avatar PowerKiKi commented on July 18, 2024

If you want to create a PR for that, it would be best to modify all writer, and unit test the new feature.

Otherwise I am not sure when we're gonna have time for it, since we are more busy with refactoring things, rather than adding new features for the time being.

from phpspreadsheet.

lenore-corbeaux avatar lenore-corbeaux commented on July 18, 2024

from phpspreadsheet.

stale avatar stale commented on July 18, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.

from phpspreadsheet.

piotrantosik avatar piotrantosik commented on July 18, 2024

Hi, any progress on this?

My mistake - it is implemented in https://github.com/PHPOffice/PhpSpreadsheet/blob/master/src/PhpSpreadsheet/Writer/Xlsx.php#L181

from phpspreadsheet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.