ZIP Download
This page explains how download() bundles conversion results into a ZIP and triggers a browser download, and how filename templates work.
Flow
- When
fileisnull/undefined, it resolvesundefinedimmediately without converting or downloading - When there are no results yet, it first runs
await this.convert(); existing results are reused without re-rendering - Appends the progress overlay to
<body>and adds every image to JSZip as base64 - Builds the ZIP with
generateAsync({ type: "blob", streamFiles: true }), syncing compression progress to the overlay - Creates an
<a download>pointing atURL.createObjectURL(blob), then after 500ms resolves, clicks it, and removes the link and the overlay
<button id="save">Download sample.zip</button>
<script>
document.getElementById("save").addEventListener("click", async e => {
e.target.disabled = true;
const buffer = await (await fetch("/assets/sample.pdf")).arrayBuffer();
const converter = new pdf2image({ file: buffer, filename: "sample.pdf yyyy-MM-DD", type: "png", scale: 0.5 });
try {
await converter.download();
console.log("downloaded", converter.images.length, "images");
} catch (err) {
console.log("failed:", String(err));
}
e.target.disabled = false;
});
new pdf2image({}).download().then(result => console.log("no file ->", String(result)));
</script>
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:
| Token | Meaning | Example |
|---|---|---|
yyyy |
Four-digit year | 2026 |
MM |
Month, zero-padded | 10 |
DD |
Day, zero-padded | 04 |
hh |
Hour (24-hour), zero-padded | 13 |
mm |
Minute, zero-padded | 05 |
Output Names
| Item | Rule | With filename: "report.pdf yyyy-MM-DD", type: "png" |
|---|---|---|
| ZIP | {name}.zip |
report 2026-10-04.zip |
| Image | {name} {index}.{type}, index starts at 0 |
report 2026-10-04 0.png, report 2026-10-04 1.png |
Notes:
- The image extension comes straight from
type; withtype: "jpg"the extension is.jpgbut the content is PNG (see Conversion) - Without
filename, the ZIP is named.zipand images get names that start with a space, such as0.jpg - Image indexes follow the result array order, which is not guaranteed to match page order
Failure Behavior
| Case | Result |
|---|---|
| The automatic conversion fails | Rejects with convert()'s rejection value; no compression overlay appears |
| JSZip fails to generate | Rejects with an empty array [] and logs the original error to the console |
The Object URL created by download() is never passed to revokeObjectURL, so repeated downloads on one page stay in memory until the page closes.