What is pdf2pic?
The pdf2pic npm package is a tool that allows you to convert PDF documents into images. It is particularly useful for generating image previews of PDF files, which can be used in web applications, document management systems, or any other scenario where visual representation of PDF content is needed.
What are pdf2pic's main functionalities?
Convert PDF to Image
This feature allows you to convert a specific page of a PDF document into an image. The code sample demonstrates how to convert the first page of a PDF file located at './test.pdf' into a PNG image with specified dimensions and save it to a designated path.
const { fromPath } = require('pdf2pic');
const options = {
density: 100,
saveFilename: "test",
savePath: "./images",
format: "png",
width: 600,
height: 600
};
const storeAsImage = fromPath("./test.pdf", options);
storeAsImage(1).then((resolve) => {
console.log("Page 1 is now converted as image");
return resolve;
});
Batch Convert PDF to Images
This feature allows you to convert all pages of a PDF document into images in a batch process. The code sample shows how to convert all pages of a PDF file into PNG images and save them to a specified directory.
const { fromPath } = require('pdf2pic');
const options = {
density: 100,
saveFilename: "test",
savePath: "./images",
format: "png",
width: 600,
height: 600
};
const storeAsImage = fromPath("./test.pdf", options);
storeAsImage.bulk(-1).then((resolve) => {
console.log("All pages are now converted as images");
return resolve;
});
Other packages similar to pdf2pic
pdf-poppler
pdf-poppler is a Node.js wrapper for the Poppler PDF rendering library. It provides similar functionality to pdf2pic by allowing conversion of PDF pages to images. However, pdf-poppler relies on the Poppler library, which may require additional setup and dependencies compared to the more straightforward approach of pdf2pic.
pdf-image
pdf-image is another package that converts PDF files to images using the PDF.js library. It offers similar capabilities to pdf2pic but may have different performance characteristics and dependencies. pdf-image is often used in environments where PDF.js is already in use or preferred.
pdf2pic

A utility for converting pdf to image, base64 or buffer format.
IMPORTANT NOTE: Please support this library by donating via PayPal, your help is much appreciated. Contributors are also welcome!
Prerequisites
- node >= 14.x
- graphicsmagick
- ghostscript
Don't have graphicsmagick and ghostscript yet?
Follow this guide to install the required dependencies.
Installation
npm install --save pdf2pic
Usage
Converting specific page of PDF from path, then saving as image file
import { fromPath } from "pdf2pic";
const options = {
density: 100,
saveFilename: "untitled",
savePath: "./images",
format: "png",
width: 600,
height: 600
};
const convert = fromPath("/path/to/pdf/sample.pdf", options);
const pageToConvertAsImage = 1;
convert(pageToConvertAsImage, { responseType: "image" })
.then((resolve) => {
console.log("Page 1 is now converted as image");
return resolve;
});
Nuff talk, show me how:
More usage example can be found here.
pdf2pic API
fromPath(filePath, options)
Initialize PDF to image conversion by supplying a file path
Functions
Convert a specific page of the PDF to Image/Base64/Buffer by supplying a file path
fromPath(filePath, options)(page, convertOptions)
- filePath - pdf file's path
- options - see options.
- page - page number to convert to an image
- convertOptions - see convertOptions.
Converts PDF to Image/Base64/Buffer by supplying a file path
fromPath(filePath, options).bulk(pages, convertOptions)
- filePath - pdf file's path
- options - see options.
- pages - page numbers to convert to image
- set
pages
to -1
to convert all pages
pages
also accepts an array indicating the page number e.g. [1,2,3]
- also accepts number e.g.
1
- convertOptions - see convertOptions
Set GraphicsMagick's subclass or path
fromPath(filePath, options).setGMClass(subClass)
NOTE: should be called before calling convert()
or bulk()
.
- filePath - pdf file's path
- options - see options.
- subClass - path to gm binary or set to true to use imagemagick
- set
subClass
to true to use imagemagick
- supply a valid path as
subClass
to locate gm path specified
fromBuffer(buffer, options)
Initialize PDF to image conversion by supplying a PDF buffer
Functions
Convert a specific page of the PDF to Image/Base64/Buffer by supplying a buffer
fromBuffer(buffer, options)(page, convertOptions)
Functions same as fromPath(filePath, options)(page, convertOptions)
only input is changed
Converts PDF to Image/Base64/Buffer by supplying a buffer:
fromBuffer(buffer, options).bulk(pages, convertOptions)
Functions same as fromPath(filePath, options).bulk(pages, convertOptions)
only input is changed
Set GraphicsMagick's subclass or path:
fromBuffer(buffer, options).setGMClass(subClass)
Functions same as fromPath(filePath, options).setGMClass(subClass)
only input is changed
fromBase64(b64string, options)
Initialize PDF to image conversion by supplying a PDF base64 string.
Functions
Convert a specific page of the PDF to Image/Base64/Buffer by supplying a base64 string:
fromBase64(b64string, options)(page, convertOptions)
Functions same as fromPath(filePath, options)(page, convertOptions)
only input is changed.
Converts PDF to Image/Base64/Buffer by supplying a base64 string:
fromBase64(b64string, options).bulk(pages, convertOptions)
Functions same as fromPath(filePath, options).bulk(pages, convertOptions)
only input is changed.
Set GraphicsMagick's subclass or path:
fromBase64(b64string, options).setGMClass(subClass)
Functions same as fromPath(filePath, options).setGMClass(subClass)
only input is changed.
options
Following are the options that can be passed on the pdf2pic api:
quality | 0 | Image compression level. Value depends on format , usually from 0 to 100 (more info) |
format | 'png' | Formatted image characteristics / image format (image characteristics, image format) |
width | 768 | Output width |
height | 512 | Output height |
preserveAspectRatio | false | Maintains the aspect ratio of the image. When set to true and both width and height are specified, they are interpreted as the minimum width and minimum height, respectively. If set to true with only the width specified, the height will be automatically determined while preserving the aspect ratio, and vice versa. |
density | 72 | Output DPI (dots per inch) (more info) |
savePath | './' | Path where to save the output |
saveFilename | 'untitled' | Output filename |
compression | 'jpeg' | Compression method (more info) |
convertOptions
responseType | image | Response type of the output. Accepts: image , base64 or buffer |
The parameter can also be a boolean, if true
then the response type will be base64
and if false
then the response type will be image
.
This is deprecated and will be removed in the next major version.
Contributing
- Fork it (https://github.com/yakovmeister/pdf2image/fork)
- Create your feature branch (git checkout -b feature/make-maintainer-cry)
- Commit your changes (git commit -am 'feature: make maintainer cry by running git rm -rf')
- Push to the branch (git push origin feature/make-maintainer-cry)
- Create a new PR
License
pdf2pic is MIT licensed.