javascriptpptxgenjs

How to divert a number of downloads into a zip file?


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?


Solution

  • 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.