1.1.0

Getting Started

Last updated


This page covers installing pdf2image and running your first PDF-to-image conversion in the browser.

Prerequisites


ItemRequirement
RuntimeA modern browser with ES2022 private fields (#field), canvas, and Promise
NetworkAccess to cdnjs.cloudflare.com (pdf.js 3.4.120, JSZip 3.7.1) and cdn.jsdelivr.net (stylesheet)
InputThe PDF as an ArrayBuffer, usually from File.arrayBuffer()

pdf2image runs in the browser only; Node.js has no document or window and cannot run it directly.

Installation


npm


  1. npm i @pardnchiu/pdf2image

CDN (UMD)


  1. <script src="https://cdn.jsdelivr.net/npm/@pardnchiu/pdf2image@1.1.2/dist/pdf2image.js"></script>

The UMD build registers the global window.pdf2image on load.

CDN (ES Module)


  1. import { pdf2image } from "https://cdn.jsdelivr.net/npm/@pardnchiu/pdf2image@1.1.2/dist/pdf2image.esm.js";

The ESM build is the same code as the UMD build, with one extra line that exports window.pdf2image.

From Source


  1. git clone https://github.com/pardnchiu/pdf2image.git
  2. cd pdf2image
  3. npm install
  4. npm run minify

ScriptOutput
npm run minifyCompresses src/pdf2image.js with terser into dist/pdf2image.js and dist/pdf2image.esm.js (the ESM file gets the extra export line appended)
npm run minify-wWatches src/*.js with chokidar and reruns minify on change
npm run sassWatches src/ and compiles src/pdf2image.scss into dist/pdf2image.css

The built script still injects the stylesheet from cdn.jsdelivr.net/npm/@pardnchiu/pdf2image@latest/dist/pdf2image.css, so a locally compiled dist/pdf2image.css only takes effect when you link it yourself.

First Conversion


The example below converts /assets/sample.pdf (3 pages) on load; you can also pick your own PDF with the file input.
  1. <input type="file" accept="application/pdf" id="pick">
  2. <div id="pages"></div>
  3. <script>
  4. async function run(buffer) {
  5. const converter = new pdf2image({ file: buffer, type: "png", scale: 0.5 });
  6. try {
  7. const images = await converter.convert();
  8. document.getElementById("pages").replaceChildren(
  9. ...images.map(src => Object.assign(document.createElement("img"), { src, width: 120 }))
  10. );
  11. console.log("pages:", images.length);
  12. } catch (err) {
  13. console.log("failed:", String(err));
  14. }
  15. }
  16. document.getElementById("pick").addEventListener("change", async e => {
  17. const file = e.target.files[0];
  18. if (file) run(await file.arrayBuffer());
  19. });
  20. addEventListener("load", async () => {
  21. run(await (await fetch("/assets/sample.pdf")).arrayBuffer());
  22. });
  23. </script>
Live preview · @pardnchiu/pdf2image@1.1.2

Three things to note:
  • file must be an ArrayBuffer, not a File object
  • Call convert() after the load event, because pdf.js is injected asynchronously when the package loads (see Core Concepts)
  • The rejection value can be a string or an Error; String(err) prints both (see Conversion)

Next Steps


中文