有没有办法使用jsZIP将多个jsPDF输出保存到单个ZIP中?

问题描述 投票:0回答:1

我使用jsPDF生成多个PDF。有没有办法让我通过jsZIP将所有PDF保存为单个ZIP?

javascript jspdf jszip
1个回答
0
投票

对于来这里寻找更直接的答案的人来说,我有这个例子:

var zip = new JSZip();

angular.forEach ($scope.students, function (student) {
//The createFile() function returns a jsPDF
    var doc = createFile(student);
    if (typeof doc !== 'undefined') {
        try {
            zip.file(student.name + '.pdf', doc.output('blob'));
        }
        catch {
            console.error('Something went wrong!');
        }
    }
});

zip.generateAsync({type:'blob'}).then(function(content) {
    saveAs(content, 'reports.zip');
});

基本上,zip.file()使用(名称,数据)向zip添加新文件。您可以通过调用doc.output('blob')将jsPDF文档添加为blob。不要忘记在名称的末尾添加“.pdf”。

然后通过调用zip.generateAsync()函数生成一个zip文件,并在完成生成时保存它。

在这个例子中,我使用了jsPDFJSZipFileSaver.js


0
投票

这有帮助吗? https://gist.github.com/noelvo/4502eea719f83270c8e9

var zip = new JSZip();
var count = 0;
var zipFilename = "zipFilename.zip";
var urls = [
  'http://image-url-1',
  'http://image-url-2',
  'http://image-url-3'
];

urls.forEach(function(url){
  var filename = "filename";
  // loading a file and add it in a zip file
  JSZipUtils.getBinaryContent(url, function (err, data) {
     if(err) {
        throw err; // or handle the error
     }
     zip.file(filename, data, {binary:true});
     count++;
     if (count == urls.length) {
       var zipFile = zip.generate({type: "blob"});
       saveAs(zipFile, zipFilename);
     }
  });
});
© www.soinside.com 2019 - 2024. All rights reserved.