1.1.0

ZIP Download

Last updated


This page explains how download() bundles conversion results into a ZIP and triggers a browser download, and how filename templates work.

Flow


  1. When file is null / undefined, it resolves undefined immediately without converting or downloading
  2. When there are no results yet, it first runs await this.convert(); existing results are reused without re-rendering
  3. Appends the progress overlay to <body> and adds every image to JSZip as base64
  4. Builds the ZIP with generateAsync({ type: "blob", streamFiles: true }), syncing compression progress to the overlay
  5. Creates an <a download> pointing at URL.createObjectURL(blob), then after 500ms resolves, clicks it, and removes the link and the overlay

  1. <button id="save">Download sample.zip</button>
  2. <script>
  3. document.getElementById("save").addEventListener("click", async e => {
  4. e.target.disabled = true;
  5. const buffer = await (await fetch("/assets/sample.pdf")).arrayBuffer();
  6. const converter = new pdf2image({ file: buffer, filename: "sample.pdf yyyy-MM-DD", type: "png", scale: 0.5 });
  7. try {
  8. await converter.download();
  9. console.log("downloaded", converter.images.length, "images");
  10. } catch (err) {
  11. console.log("failed:", String(err));
  12. }
  13. e.target.disabled = false;
  14. });
  15. new pdf2image({}).download().then(result => console.log("no file ->", String(result)));
  16. </script>
Live preview · @pardnchiu/pdf2image@1.1.2

Filename Template


At construction filename is trimmed and its first .pdf is removed (case-sensitive, anywhere in the string). At download time these date tokens are replaced with local time:
TokenMeaningExample
yyyyFour-digit year2026
MMMonth, zero-padded10
DDDay, zero-padded04
hhHour (24-hour), zero-padded13
mmMinute, zero-padded05

Output Names


ItemRuleWith filename: "report.pdf yyyy-MM-DD", type: "png"
ZIP{name}.zipreport 2026-10-04.zip
Image{name} {index}.{type}, index starts at 0report 2026-10-04 0.png, report 2026-10-04 1.png

Notes:
  • The image extension comes straight from type; with type: "jpg" the extension is .jpg but the content is PNG (see Conversion)
  • Without filename, the ZIP is named .zip and images get names that start with a space, such as 0.jpg
  • Image indexes follow the result array order, which is not guaranteed to match page order

Failure Behavior


CaseResult
The automatic conversion failsRejects with convert()'s rejection value; no compression overlay appears
JSZip fails to generateRejects with an empty array [] and logs the original error to the console; the compression overlay is not removed and stays on the page


中文