Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP stream #35

Closed
FedeMM opened this issue Feb 8, 2017 · 10 comments
Closed

HTTP stream #35

FedeMM opened this issue Feb 8, 2017 · 10 comments
Assignees
Milestone

Comments

@FedeMM
Copy link

FedeMM commented Feb 8, 2017

Hi

I've been using officegen module for a while and there you can create a HTTP stream and send the pptx xml in the response by just doing pptx.generate ( response );

Is there any way to do this or similar with PptxGenJS?.

Thanks

@gitbrent
Copy link
Owner

gitbrent commented Feb 8, 2017

Hi,

Thanks for trying out PptxGenJS and opening an issue.

There's currently no functionality to create HTTP streams, but i am curious about a solution using streams.

Is this for Node or web client, and how are the streams being used?

@gitbrent gitbrent self-assigned this Feb 8, 2017
@FedeMM
Copy link
Author

FedeMM commented Feb 9, 2017

Hi,

Thanks for your quick answer.

The streams are used to avoid creating the file in local, instead of that it goes within the response result from server to client and I finally downloaded in client side. (I haven't debugged how is that the pptx is generated and put into the response behind the scene, I guess it just keep it in memory as a buffer or blob).

@gabriele-mastrapasqua
Copy link

gabriele-mastrapasqua commented Feb 16, 2017

To save the powerpoint as an HTTP Stream instead of a file i've changed the function "doExportPresentation()" in the file pptxgen.js passing a callback that is called instead of "fs.writeFile" like this:

zip.generateAsync({type:'nodebuffer'}).then(function(content){ saveCallback(strExportName, content); });

In an express application I write the content as a http stream like this:

function saveCallback(filename, data) { // serve byte array as file attachment... res.writeHead(200, { 'Content-disposition': 'attachment;filename='+filename, 'Content-Length': data.length }); res.end(new Buffer(data, 'binary')); }

when I want to save a file i pass a callback like this:

function saveCallback(filename, data) { fs.writeFile(filename, data); }

Maybe a better method is to use a flag that indicate that we want the data instead of saving the file, merging with the pull https://github.com/gitbrent/PptxGenJS/commit/b2cd8b0a91f233e17e831edd0a45c25fa0983b3a
like this:

function doExportPresentation(callback, returnData)

and then when saving in nodejs in line 174:

zip.generateAsync({type:'nodebuffer'}).then(function(content){ if(returnData){ callback(strExportName, content); } else { fs.writeFile(strExportName, content, callback(strExportName)); } });

@gitbrent
Copy link
Owner

Hi @GabrielM0 ,

Thanks for the feedback. I'm continuing to investigate methods to further utilize the callback just added in v1.2.0

I'll see what i can do to integrate your suggestions.

@necouchman
Copy link

+1 on this. I'm looking to use the pptxgenjs library in an AWS Lambda application that I will expose via the AWS API Gateway. I believe there is some functionality in Lambda for saving/retrieving files, but the "serverless" architecture really lends itself to grabbing stuff and just pushing it out via the HTTP stream rather than having to save it somewhere and then download it.

@gitbrent
Copy link
Owner

Hi @necouchman ,

Thanks for the feedback.

Now that PptxGenJS can utilize callbacks, can you enable streaming using a callback like the example above? (The nodejs-demo.js file has an example of using callbacks)

function saveCallback(filename, data) {
  // serve byte array as file attachment...
  res.writeHead(200, { 'Content-disposition': 'attachment;filename='+filename, 'Content-Length': data.length });
  res.end(new Buffer(data, 'binary'));
}

@necouchman
Copy link

@gitbrent,
So, when I try to do this, I get the following error:
(node:24045) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'length' of undefined

Also, when I look the "data" object via console.log inside the callback method, it comes back "undefined." I'm using pptxgenjs 1.4.0.

@kirkjensen18
Copy link

@GabrielM0
Thank you!
I edited line 407 in pptxgen.js to remove fs.writeFile with callback:
zip.generateAsync({type:'nodebuffer'}).then(function(content){ callback(strExportName, content); });

Then, I can generate the http response in node:
pptx.save('presentation.pptx', function(filename, content) { res.writeHead(200, {'Content-Type': 'application/force-download','Content-disposition':'attachment; filename='+filename});
res.end(new Buffer(content, 'binary'));

@Salindakrish
Copy link

@FedeMM can you give me simple example of a officegen stream to front end , i created one but at front end it have been corrupted....

@gitbrent gitbrent added this to the v1.6.0 milestone Jul 18, 2017
@gitbrent
Copy link
Owner

Hi @FedeMM ,

Thanks to @GabrielM0 and @kirkjensen18 and other posts here, i've got http streaming working now!

The examples/nodejs-demo.js file has a working example of http streaming that can be tested locally.

  1. Call save using "http" as the filename:
pptx.save('http', steamCallback);
  1. Then stream from within the callback:
function steamCallback(data) {
    var strFilename = "Node-Presenation-Streamed.pptx";

    app.get('/', function(req, res) {
        res.writeHead(200, { 'Content-disposition':'attachment;filename='+strFilename, 'Content-Length':data.length });
        res.end(new Buffer(data, 'binary'));
    });

    app.listen(3000, function() {
        console.log('PptxGenJS Node Demo app listening on port 3000!');
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants