Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

pdftoimg-js

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pdftoimg-js

A javascript library that help to convert pdf to img in both platform nodejs and browser

latest
Source
npmnpm
Version
0.2.5
Version published
Weekly downloads
2.5K
4.55%
Maintainers
1
Weekly downloads
 
Created
Source

Version 2 Has Arrived! 🎉
This release is still in beta, so we welcome your feedback and feature requests.
If you encounter any bugs, please report them by opening an issue on our GitHub repository.

PDFtoIMG-JS

PDFtoIMG-JS is a powerful JavaScript library for converting PDF file/files into images (PNG or JPG). It supports both Node.js and browser environments, making it ideal for a wide range of use cases — including integration with popular frameworks such as React, Next.js, Vue, and more.

✨ Features

  • 📚 Supports single and multiple PDFs.
  • 🎯 Choose specific pages (all, firstPage, lastPage, page numbers, or ranges like 1..3).
  • 🎨 Output images as PNG or JPG formats.
  • 🔥 Compatible with React, Next.js, Vue, etc.
  • 🛠 Fine control over scale and output options.
  • 💻 Browser runtime automatically uses HTML5 Canvas rendering.
  • 🧹 CLI ready for quick batch processing in Node.js.

📦 Installation

npm install pdftoimg-js

🛠 Basic Usage

🚀 Example (Node.js Script)

import { pdfToImg } from "pdftoimg-js";

const images = await pdfToImg("example.pdf", {
  pages: "firstPage",
  imgType: "jpg",
  scale: 2,
  background: "white",
});

console.log(images); // => Base64 encoded JPG image

📝 Example (Browser)

import { pdfToImg } from "pdftoimg-js/browser";

const fileInput = document.getElementById("pdfInput");

fileInput.addEventListener("change", async (e) => {
  const file = e.target.files[0];
  const images = await pdfToImg(URL.createObjectURL(file), {
    maxWidth: 1000,
    maxHeight: 1000,
    // Set scaleForBrowserSupport: true to automatically scale to 4096x4096
    // Setting maxWidth and maxHeight will override scaleForBrowserSupport
    // scaleForBrowserSupport: true,
  });

  images.forEach((imgSrc) => {
    const img = document.createElement("img");
    img.src = imgSrc;
    document.body.appendChild(img);
  });
});

📖 API Reference

pdfToImg(src, options?)

Convert PDF(s) to images.

ParameterTypeDescription
srcstring, URL, Uint8Array, or Array of EachPDF file(s) input source.
optionsPartial<Options>(Optional) Conversion settings.

Returns:

  • If pages is a single page (firstPage, lastPage, or a number) ➔ Single image.
  • Otherwise ➔ Array of images.

Options (Options Interface)

interface Options {
  imgType?: "png" | "jpg"; // Default: "png"
  scale?: number; // Default: 1.5
  background?: string | CanvasGradient | CanvasPattern; // Default: "rgb(255,255,255)"
  intent?: "display" | "print" | "any"; // Default: "display"
  pages?: PagesType; // "all" | "firstPage" | "lastPage" | number | number[] | { startPage, endPage }
  documentOptions?: DocumentInitParameters; // (Optional) More PDF.js config.
  maxWidth?: number | null; // Default: null
  maxHeight?: number | null; // Default: null
  scaleForBrowserSupport?: boolean; // Default: false
}

🖽 Browser Support

  • Auto-detects browser environment.
  • Uses Canvas API for rendering pages.
  • Sets worker dynamically from CDN:
    pdfjsLib.GlobalWorkerOptions.workerSrc =
      "//cdnjs.cloudflare.com/ajax/libs/pdf.js/4.8.69/pdf.worker.min.mjs";
    
  • Returns base64 DataURL for each page.

👡 CLI Usage (Node.js Only)

Command-line support to batch convert PDFs easily!

pdftoimg -i <input> [-o <output>] [-t <imgType>] [-s <scale>] [-p <pages>] [-n <template>] [-ps <password>] [-b <background>] [-in <intent>] [-mw <maxWidth>] [-mh <maxHeight>] [-sb <scaleForBrowserSupport>]

CLI Options

OptionTypeDescription
-i, --inputstring(Required) Input PDF path.
-o, --outstringOutput directory (default: current directory).
-t, --imgTypestringpng or jpg (default: png).
-s, --scalenumberScale factor (default: 1.5).
-b, --backgroundstringBackground color (e.g., 'white', 'rgba(255,255,255,0.5)', '#ffffff').
-in, --intentstringRendering intent: 'display', 'print', or 'any' (default: 'display').
-p, --pagesstring"all", "firstPage", "lastPage", page numbers, or ranges like 1..3.
-n, --namestringFilename template {i}, {p}, {ext}, {f} available.
-ps, --passwordstringPassword for the PDF file if encrypted.
-mw, --maxWidthnumberMaximum width for the rendered canvas.
-mh, --maxHeightnumberMaximum height for the rendered canvas.
-sb, --scaleForBrowserSupportbooleanScale for browser support.

⚡ Example CLI Commands

Convert first page to PNG:

pdftoimg -i ./example.pdf -p firstPage

Convert pages 1 to 3 to JPG:

pdftoimg -i ./example.pdf -t jpg -p 1..3

Save in a custom folder:

pdftoimg -i ./example.pdf -o ./output

Convert with custom background and print intent:

pdftoimg -i ./example.pdf -b "white" -in print

Convert with transparent background:

pdftoimg -i ./example.pdf -b "rgba(255,255,255,0)"

Convert with custom max width and height:

pdftoimg -i ./example.pdf -mw 1000 -mh 1000

Convert with scale for browser support:

pdftoimg -i ./example.pdf -sb true

Contribution

Contributions are welcome! Feel free to check out the Contributing Guide before making a pull request.

Keywords

pdftoimg-js

FAQs

Package last updated on 24 Jul 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts