question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to download multiple pdfs in a zip?

See original GitHub issue

Hi! Thanks for creating this awesome library I’m struggling with replacing JSZip with your library I need to download several files in an array like this

exampleFiles = [
    {
        name: "1.pdf",
        url: "/storage/files/1.pdf'
    },
    {
        name: "hello.pdf",
        url: "/storage/files/hello.pdf'
    },
    {
        name: "abcd.pdf",
        url: "/storage/files/abcd.pdf'
    },
]

// this function is a layer of abstraction to download files
// it uses JSZip to fetch the remote files and download them 
// in a zip archive. More important, it works well!
downloadFilesInZip(files)

Can you help me implement this using your library? I’ve tried to do it following the code provided in the README, but it seems like the fetch api gets the binary content of the pdf and it is saved as plain text; therefore I can’t display the file after downloading the zip

Thanks in advance

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Touffycommented, Nov 9, 2021

Hello @myeongwooni. Yes, you can yield objects with name, lastModified and input properties instead of just the Response. For example, if you want sequential filenames and you know all the files are plain text :

let number = 1
async function* downloadGenerator(urls) {
  for(const url of urls) yield { input: await fetch(url), name: `file${number++}.txt` }
}

Of course you might need more logic there to compute appropriate filenames. You have access to the whole Response for that. It’s how client-zip does it by default when you just pass it a Response: it looks at the response headers (specifically the Content-Disposition header if present ; if not, it will use the last part of the URL’s pathname).

1reaction
Touffycommented, Oct 12, 2020

Hello. You can’t specify “url” in the input for downloadZip and it’s not something I want to support. The reason for that is because fetching resources can be quite complicated (what if you need a JWT to access the PDFs, for example ?) and this library doesn’t want to be complicated.

But, it’s really quite easy to fetch the resources and generate a correct input for downloadZip. The quick-and-slightly-inefficient way is this :

const responses = await Promise.all([
  fetch('/storage/files/1.pdf'),
  fetch('/storage/files/hello.pdf'),
  fetch('/storage/files/abcd.pdf')
])

const zippedBlob = await downloadZip(responses).blob()

You don’t even need to provide a “name” for your files because client-zip will extract the file name from the URL (or the Content-Disposition HTTP header) when you provide a Response object as input.

It’s slightly inefficient because the browser will start all the HTTP requests for the PDFs immediately. If you have a lot of PDFs to download, it would be better to download them in sequence using an async generator :

const urls = [
  '/storage/files/1.pdf',
  '/storage/files/hello.pdf',
  '/storage/files/abcd.pdf'
])

async function* downloadGenerator(urls) {
  for(const url of urls) yield await fetch(url)
}

const zippedBlob = await downloadZip(downloadGenerator(urls)).blob()

The difference there is that each download is started only when the previous file is completely copied to the ZIP archive.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Zipping multiple PDFs and downloading the zip file?
If someone had the same issue, this is how I resolved it: var filename = url.replace(/.*\//g, ""); zip.file(filename, data, { binary: true, ...
Read more >
Extracting Zip Files and Printing Multiple PDF Files - YouTube
This is a quick video on how to extract . zip files in Windows, as well as how to print multiple PDF files...
Read more >
Download multiple PDF Report as Zip using ZipAchive in ASP ...
So I currently have all my PDF records being put in a zip fileEach record that is selected is put into a single...
Read more >
Download multiple PDFs as a zip archive [#2672576] - Drupal
Downloading PDF's via an action plugin which gives us VBO integration is possible since [#2668482] but that downloads multiple entities onto ...
Read more >
How to convert ZIP to PDF files or combine into one PDF?
And PDF Converter makes it simple and quick to combine such documents into one PDF file, in a click. All you need to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found