我使用 jsPDF 生成多个 PDF。
有没有办法让我通过 jsZIP 将所有 PDF 保存为单个 ZIP?
最佳答案
对于任何来这里寻找这个问题的更直接答案的人,我有这个例子:
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 文件,并在生成完成后保存它。
在这个例子中,我使用了 jsPDF , JSZip和 FileSaver.js
关于javascript - 有没有办法使用 jsZIP 将多个 jsPDF 输出保存到单个 ZIP 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55649688/