2

I'm using the PptxGenJS library, and one of the functions I'm using it for leads to around 8 powerpoint files being downloaded at once. It works, but it creates a mess, and so putting them into a single zip file would greatly improve the experience.

The issue is that I can't find a way to do this, as there seems to be nothing in PptxGenJS that will let you manipulate a file once it's been created, it just sends it straight as a download.

Is there someway to intercept those downloads, add them to a zip file, and then send that zip to the user?

Jesse Bell
  • 23
  • 3

2 Answers2

0

Pass in 'jszip' as the first argument of save, a callback, and a jszip file type, like this:

var p1 = new PptxGenJS();
p1.addNewSlide().addText('Presentation 1');
var p1file = p1.save('jszip', function(file1) {
    var p2 = new PptxGenJS();
    p2.addNewSlide().addText('Presentation 2');
    p2.save('jszip', function(file2) {
        var zip = new JSZip();
        zip.file('pres1.pptx', file1);
        zip.file('pres2.pptx', file2);

        zip.generateAsync({type: 'blob'}).then(function(content) {
            saveAs(content, 'multipres.zip'); // requires filesaver.js
        });
    }, 'blob');
}, 'blob');

If you have more than 2 presentations to file, you'll probably want to use async instead of nested callbacks.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • Hey thanks, looks like I was wrong to assume that the advanced saving features were only available with NodeJS. – Jesse Bell Jan 11 '18 at 06:29
0

For anyone else with a similar question, this is what I ended up with:

var p1 = new PptxGenJS();
var p2 = new PptxGenJS();
var p3 = new PptxGenJS();
var p4 = new PptxGenJS();

p1.addNewSlide().addText('Annual report');
p2.addNewSlide().addText('Monthly report');
p3.addNewSlide().addText('Weekly report');
p4.addNewSlide().addText('Daily report');

pptxArr = [[p1,"Annual report"],[p2,"Monthly report"],[p3,"Weekly report"],[p4,"Daily report"]];

CreateZip(pptxArr);

function CreateZip(pptxArr)
{
    var zip = new JSZip();

    pptxArr.forEach(function(pptx, index)
    {
        if (index < pptxArr.length-1)
        {   
            pptx[0].save('jszip', function(pres) {
                zip.file(pptx[1]+'.pptx', pres)
            }, 'blob');
        }
        else
        {
            var file = pptx[0].save('jszip', function(pres) {
                zip.file(pptx[1]+'.pptx', pres)

                zip.generateAsync({type: 'blob'}).then(function(content) {
                    saveAs(content, 'multipres.zip');
                });
            }, 'blob');
        }
    });
}
Jesse Bell
  • 23
  • 3