Convert PDF pages to images using node js

·

3 min read

Prerequisites

  • node >= 14.x

  • graphicsmagick

  • ghostscript

For linux users, you can run the following commands on your terminal.

$ sudo apt-get update
$ sudo apt-get install ghostscript
$ sudo apt-get install graphicsmagick

Once everything is installed, the library should work as expected.

nstallation

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)

Thanks you!

Did you find this article valuable?

Support ramu k by becoming a sponsor. Any amount is appreciated!