Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pdf2pic

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pdf2pic

A utility for converting pdf to image and base64 format.

  • 2.0.0-alpha.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
84K
increased by44.88%
Maintainers
1
Weekly downloads
 
Created
Source

PDF2Pic

Donate Build Status CodeFactor Maintainability Test Coverage install size NPM Version License Known Vulnerabilities Downloads Stats

A utility for converting pdf to image and base64 format.

IMPORTANT NOTE: Please support this library by donating via PayPal, your help is much appreciated. Contributors are also welcome!

ANOTHER IMPORTANT NOTE: please use version 1.x for now as 2.x is still in early development stage

Prerequisite

  • node >= 12.x
  • graphicsmagick
  • ghostscript

Don't have graphicsmagick and ghostscript yet?

Follow this guide to install the required dependencies.

Installation

npm install -S pdf2pic

Usage

Converting specific page of PDF from path, then saving as image file

import { fromPath } from "pdf2pic";

const options = {
  density: 100,
  savename: "untitled",
  savedir: "./images",
  format: "png",
  width: 600,
  height: 600
};
const storeAsImage = fromPath("/path/to/pdf/sample.pdf", options);
const pageToConvertAsImage = 1;

storeAsImage(pageToConvertAsImage).then((resolve) => {
  console.log("Page 1 is now converted as image");

  return resolve;
});

Converting specific page of PDF from path, then saving as base64 string of image

import { fromPath } from "pdf2pic";

const options = {
  density: 100,
  savename: "untitled",
  savedir: "./images",
  format: "png",
  width: 600,
  height: 600
};
const convertPdf2Base64Image = fromPath("/path/to/pdf/sample.pdf", options);
const pageToConvertAsImage = 1;

// adding true as second parameter will transform response to base64 string
convertPdf2Base64Image(pageToConvertAsImage, true).then((resolve) => {
  console.log("Page 1 is now converted as base64string");

  console.log({
    base64string: resolve.base64
  })

  return resolve;
});

Bulk conversion

import { fromPath } from "pdf2pic";

const options = {
  density: 100,
  savename: "untitled",
  savedir: "./images",
  format: "png",
  width: 600,
  height: 600
};
const convert = fromPath("/path/to/pdf/sample.pdf", options);
const pageToConvertAsImage = [4, 5]; // let's convert page 4 and 5

convert.bulk(pageToConvertAsImage).then((resolve) => {
  console.log("Page 4 and 5 is now converted as image");

  return resolve;
});

PDF Stream input, saved as image file

import { fromBuffer } from "pdf2pic";
import { readFileSync } from "fs";

const options = {
  density: 100,
  savename: "untitled",
  savedir: "./images",
  format: "png",
  width: 600,
  height: 600
};

const file = readFileSync("/path/to/pdf/sample.pdf", "base64");
const buff = Buffer.from(file, "base64");

const convert = fromBuffer(buff, options);
const pageToConvertAsImage = 1;

convert(pageToConvertAsImage).then((resolve) => {
  console.log("Page 1 is now converted as image");

  return resolve;
});

PDF base64 input, saved as image file

import { fromBase64 } from "pdf2pic";
import { readFileSync } from "fs";

const options = {
  density: 100,
  savename: "untitled",
  savedir: "./images",
  format: "png",
  width: 600,
  height: 600
};

const file = readFileSync("/path/to/pdf/sample.pdf", "base64");

const convert = fromBase64(file, options);
const pageToConvertAsImage = 1;

convert(pageToConvertAsImage).then((resolve) => {
  console.log("Page 1 is now converted as image");

  return resolve;
});

TO BE CONTINUED!

Keywords

FAQs

Package last updated on 13 Sep 2020

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc