Skip to content

Instantly share code, notes, and snippets.

@ArmandoAssuncao
Created September 27, 2018 15:11
Show Gist options
  • Save ArmandoAssuncao/6dbe9ad91f8b5380bb8438bd0fe1fee9 to your computer and use it in GitHub Desktop.
Save ArmandoAssuncao/6dbe9ad91f8b5380bb8438bd0fe1fee9 to your computer and use it in GitHub Desktop.
import JSZip from 'jszip';
import JSZipUtils from 'jszip-utils';
import saveAs from 'file-saver';
function urlToPromise(url) {
return new Promise(function(resolve, reject) {
JSZipUtils.getBinaryContent(url, function (err, data) {
if(err) {
reject(err);
} else {
resolve(data);
}
});
});
}
function saveFilesToZip() {
const zip = new JSZip();
const folder_name = 'folder_name'
zip.folder(folder_name);
const url = 'http://www.url_file.extension'
const pdf_name = 'pdf_name.extension'
zip.file(`${folder_name}/${pdf_name}.pdf`, urlToPromise(url), {binary:true});
zip.generateAsync({type:"blob"})
.then(function(content) {
zip_name = 'zip_name.zip'
saveAs(content, zip_name);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment