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 - npm Package Compare versions

Comparing version 2.2.4 to 3.0.1

.github/workflows/lint.yml

6

dist/graphics.d.ts
/// <reference types="node" />
import gm from "gm";
import fs from "fs-extra";
import { WriteImageResponse } from "./types/writeImageResponse";
import fs from "fs";
import { BufferResponse, ToBase64Response, WriteImageResponse } from './types/convertResponse';
import { Options } from "./types/options";
import { ToBase64Response } from "./types/toBase64Response";
export declare class Graphics {

@@ -20,2 +19,3 @@ private quality;

toBase64(stream: fs.ReadStream, page?: number): Promise<ToBase64Response>;
toBuffer(stream: fs.ReadStream, page?: number): Promise<BufferResponse>;
writeImage(stream: fs.ReadStream, page?: number): Promise<WriteImageResponse>;

@@ -22,0 +22,0 @@ identify(filepath: string | fs.ReadStream, argument?: string): Promise<gm.ImageInfo | string>;

@@ -0,0 +0,0 @@ /// <reference types="node" />

@@ -1,1 +0,1 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("gm"),e=require("path"),i=require("fs-extra"),s=require("stream");function r(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var n=r(t),a=r(e),h=r(i);const o={quality:0,format:"png",width:768,height:512,density:72,savePath:"./",saveFilename:"untitled",compression:"jpeg"};class u{constructor(){this.quality=0,this.format="png",this.width=768,this.height=512,this.density=72,this.savePath="./",this.saveFilename="untitled",this.compression="jpeg",this.gm=n.default.subClass({imageMagick:!1})}generateValidFilename(t){return"number"==typeof t?`${this.savePath}/${this.saveFilename}.${t+1}.${this.format}`:`${this.savePath}/${this.saveFilename}.${this.format}`}gmBaseCommand(t,e){return this.gm(t,e).density(this.density,this.density).resize(this.width,this.height,"!").quality(this.quality).compress(this.compression)}toBase64(t,e){const i=`${t.path}[${e}]`;return new Promise(((s,r)=>{this.gmBaseCommand(t,i).stream(this.format,((t,i)=>{let n="";if(t)return r(t);i.on("data",(t=>{n+=t.toString("binary")})).on("end",(()=>{const t=Buffer.from(n,"binary").toString("base64");return s({base64:t,size:`${this.width}x${this.height}`,page:e+1})}))}))}))}writeImage(t,e){const i=this.generateValidFilename(e),s=`${t.path}[${e}]`;return new Promise(((r,n)=>{this.gmBaseCommand(t,s).write(i,(t=>t?n(t):r({name:a.default.basename(i),size:`${this.width}x${this.height}`,fileSize:h.default.statSync(i).size/1e3,path:i,page:e+1})))}))}identify(t,e){const i=this.gm(t);return new Promise(((t,s)=>{e?i.identify(e,((e,i)=>e?s(e):t(i.replace(/^[\w\W]*?1/,"1")))):i.identify(((e,i)=>e?s(e):t(i)))}))}setQuality(t){return this.quality=t,this}setFormat(t){return this.format=t,this}setSize(t,e){return this.width=t,this.height=e||t,this}setDensity(t){return this.density=t,this}setSavePath(t){return this.savePath=t,this}setSaveFilename(t){return this.saveFilename=t,this}setCompression(t){return this.compression=t,this}setGMClass(t){return"boolean"==typeof t?(this.gm=n.default.subClass({imageMagick:t}),this):"imagemagick"===t.toLocaleLowerCase()?(this.gm=n.default.subClass({imageMagick:!0}),this):(this.gm=n.default.subClass({appPath:t}),this)}getOptions(){return{quality:this.quality,format:this.format,width:this.width,height:this.height,density:this.density,savePath:this.savePath,saveFilename:this.saveFilename,compression:this.compression}}}function m(t){return new s.Readable({read(){this.push(t),this.push(null)}})}function f(t,e){if("buffer"===t)return m(e);if("path"===t)return i.createReadStream(e);if("base64"===t)return s=e,m(Buffer.from(s,"base64"));var s;throw new Error("Cannot recognize specified source")}function l(t,e,i,s){return new(i||(i=Promise))((function(r,n){function a(t){try{o(s.next(t))}catch(t){n(t)}}function h(t){try{o(s.throw(t))}catch(t){n(t)}}function o(t){var e;t.done?r(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(a,h)}o((s=s.apply(t,e||[])).next())}))}function c(t,e,i,s,r=!1){const n=f(e,i),a=s.map((e=>{if(e<1)throw new Error("Page number should be more than or equal 1");return r?t.toBase64(n,e-1):t.writeImage(n,e-1)}));return Promise.all(a)}function d(t,e,i,s=1,r=!1){return l(this,void 0,void 0,(function*(){const n=-1===s?yield function(t,e){return l(this,void 0,void 0,(function*(){return(yield t.identify(e,"%p ")).split(" ").map((t=>parseInt(t,10)))}))}(t,f(e,i)):Array.isArray(s)?s:[s],a=[];for(let s=0;s<n.length;s+=10)a.push(...yield c(t,e,i,n.slice(s,s+10),r));return a}))}function g(t,e,i=o){const s=new u;i=Object.assign(Object.assign({},o),i);const r=(i=1,r=!1)=>{if(i<1)throw new Error("Page number should be more than or equal 1");const n=f(t,e);return r?s.toBase64(n,i-1):s.writeImage(n,i-1)};return r.bulk=(i,r=!1)=>d(s,t,e,i,r),r.setOptions=()=>function(t,e){return void t.setQuality(e.quality).setFormat(e.format).setSize(e.width,e.height).setDensity(e.density).setSavePath(e.savePath).setSaveFilename(e.saveFilename).setCompression(e.compression)}(s,i),r.setGMClass=t=>{s.setGMClass(t)},r.setOptions(),r}exports.fromBase64=function(t,e=o){return g("base64",t,e)},exports.fromBuffer=function(t,e=o){return g("buffer",t,e)},exports.fromPath=function(t,e=o){return g("path",t,e)};
"use strict";var t=require("gm"),e=require("path"),s=require("fs"),i=require("stream");const r={quality:0,format:"png",width:768,height:512,density:72,savePath:"./",saveFilename:"untitled",compression:"jpeg"};function n(t,e,s,i){return new(s||(s=Promise))((function(r,n){function a(t){try{h(i.next(t))}catch(t){n(t)}}function o(t){try{h(i.throw(t))}catch(t){n(t)}}function h(t){var e;t.done?r(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(a,o)}h((i=i.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class a{constructor(){this.quality=0,this.format="png",this.width=768,this.height=512,this.density=72,this.savePath="./",this.saveFilename="untitled",this.compression="jpeg",this.gm=t.subClass({imageMagick:!1})}generateValidFilename(t){let s=e.join(this.savePath,this.saveFilename);return this.savePath.startsWith("./")&&(s=`./${s}`),"number"==typeof t&&(s=`${s}.${t+1}`),`${s}.${this.format}`}gmBaseCommand(t,e){return this.gm(t,e).density(this.density,this.density).resize(this.width,this.height,"!").quality(this.quality).compress(this.compression)}toBase64(t,e){return n(this,void 0,void 0,(function*(){const{buffer:s,size:i,page:r}=yield this.toBuffer(t,e);return{base64:s.toString("base64"),size:i,page:r}}))}toBuffer(t,e){const s=`${t.path}[${e}]`;return new Promise(((i,r)=>{this.gmBaseCommand(t,s).stream(this.format,((t,s)=>{const n=[];if(t)return r(t);s.on("data",(t=>{n.push(t)})).on("end",(()=>i({buffer:Buffer.concat(n),size:`${this.width}x${this.height}`,page:e+1})))}))}))}writeImage(t,i){const r=this.generateValidFilename(i),n=`${t.path}[${i}]`;return new Promise(((a,o)=>{this.gmBaseCommand(t,n).write(r,(t=>t?o(t):a({name:e.basename(r),size:`${this.width}x${this.height}`,fileSize:s.statSync(r).size/1e3,path:r,page:i+1})))}))}identify(t,e){const s=this.gm(t);return new Promise(((t,i)=>{e?s.identify(e,((e,s)=>e?i(e):t(s.replace(/^[\w\W]*?1/,"1")))):s.identify(((e,s)=>e?i(e):t(s)))}))}setQuality(t){return this.quality=t,this}setFormat(t){return this.format=t,this}setSize(t,e){return this.width=t,this.height=e||t,this}setDensity(t){return this.density=t,this}setSavePath(t){return this.savePath=t,this}setSaveFilename(t){return this.saveFilename=t,this}setCompression(t){return this.compression=t,this}setGMClass(e){return"boolean"==typeof e?(this.gm=t.subClass({imageMagick:e}),this):"imagemagick"===e.toLocaleLowerCase()?(this.gm=t.subClass({imageMagick:!0}),this):(this.gm=t.subClass({appPath:e}),this)}getOptions(){return{quality:this.quality,format:this.format,width:this.width,height:this.height,density:this.density,savePath:this.savePath,saveFilename:this.saveFilename,compression:this.compression}}}function o(t){return new i.Readable({read(){this.push(t),this.push(null)}})}function h(t,e){if("buffer"===t)return o(e);if("path"===t)return s.createReadStream(e);if("base64"===t)return i=e,o(Buffer.from(i,"base64"));var i;throw new Error("Cannot recognize specified source")}const u=t=>{var e;if(t&&"object"!=typeof t)throw new Error(`Invalid convertOptions type: ${t}`);return null!==(e=null==t?void 0:t.responseType)&&void 0!==e?e:"image"};function m(t,e,s=r){const i=new a;s=Object.assign(Object.assign({},r),s);const o=(t,e,s)=>{if(e<1)throw new Error("Page number should be more than or equal 1");const r=u(s);switch(r){case"base64":return i.toBase64(t,e-1);case"image":return i.writeImage(t,e-1);case"buffer":return i.toBuffer(t,e-1);default:throw new Error(`Invalid responseType: ${r}`)}},m=(t,e,s)=>Promise.all(e.map((e=>o(t,e,s)))),f=(s=1,i)=>{const r=h(t,e);return o(r,s,i)};return f.bulk=(s,r)=>n(this,void 0,void 0,(function*(){const a=-1===s?yield function(t,e){return n(this,void 0,void 0,(function*(){return(yield t.identify(e,"%p ")).split(" ").map((t=>parseInt(t,10)))}))}(i,h(t,e)):Array.isArray(s)?s:[s],o=[],u=h(t,e);for(let t=0;t<a.length;t+=10)o.push(...yield m(u,a.slice(t,t+10),r));return o})),f.setOptions=()=>function(t,e){return void t.setQuality(e.quality).setFormat(e.format).setSize(e.width,e.height).setDensity(e.density).setSavePath(e.savePath).setSaveFilename(e.saveFilename).setCompression(e.compression)}(i,s),f.setGMClass=t=>{i.setGMClass(t)},f.setOptions(),f}exports.fromBase64=function(t,e=r){return m("base64",t,e)},exports.fromBuffer=function(t,e=r){return m("buffer",t,e)},exports.fromPath=function(t,e=r){return m("path",t,e)};
/// <reference types="node" />
import { ReadStream } from "fs-extra";
import { ReadStream } from "fs";
export declare function base64ToStream(base64: string): ReadStream;
/// <reference types="node" />
/// <reference types="node" />
import { ReadStream } from "fs-extra";
import { ReadStream } from "fs";
export declare function bufferToStream(buffer: Buffer): ReadStream;
/// <reference types="node" />
/// <reference types="node" />
import { ReadStream } from "fs-extra";
import { ReadStream } from "fs";
export declare function convertToStream(source: string, file: string | Buffer): ReadStream;
import { Options } from "../types/options";
export declare const defaultOptions: Options;
{
"name": "pdf2pic",
"version": "2.2.4",
"description": "A utility for converting pdf to image and base64 format.",
"version": "3.0.1",
"description": "A utility for converting pdf to image formats. Supports different outputs: directly to file, base64 or buffer.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"dependencies": {
"fs-extra": "^10.0.0",
"gm": "^1.23.1"
"gm": "^1.25.0"
},
"devDependencies": {
"@rollup/plugin-alias": "^3.1.2",
"@types/chai": "^4.2.14",
"@types/fs-extra": "^9.0.6",
"@types/gm": "^1.18.9",
"@types/mocha": "^8.2.0",
"@types/node": "^15.0.0",
"@types/rimraf": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^4.14.2",
"@typescript-eslint/parser": "^4.14.2",
"chai": "^4.3.0",
"eslint": "^7.19.0",
"mocha": "^9.0.0",
"@rollup/plugin-alias": "^4.0.4",
"@rollup/plugin-terser": "^0.4.3",
"@rollup/plugin-typescript": "^11.1.2",
"@types/chai": "^4.3.5",
"@types/gm": "^1.25.1",
"@types/mocha": "^10.0.1",
"@types/node": "^16.18.39",
"@types/rimraf": "^4.0.5",
"@typescript-eslint/eslint-plugin": "^6.2.1",
"@typescript-eslint/parser": "^6.2.1",
"chai": "^4.3.7",
"eslint": "^8.46.0",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"rimraf": "^3.0.2",
"rollup": "^2.38.5",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.30.0",
"ts-node": "^10.0.0",
"tsconfig-paths": "^3.9.0",
"tslib": "^2.1.0",
"typescript": "^4.1.3"
"rimraf": "^5.0.1",
"rollup": "^3.27.2",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"tslib": "^2.6.1",
"typescript": "^5.1.6"
},
"scripts": {
"mocha": "mocha test/**/*.test.ts",
"test": "npm run lint && nyc --reporter=lcov npm run mocha",
"test:text": "npm run lint && nyc --reporter=text npm run mocha",
"test": "nyc --reporter=lcov npm run mocha",
"test:text": "nyc --reporter=text npm run mocha",
"test:watch": "npm run mocha -- --watch",
"rollup": "rollup -c",
"clean": "rimraf ./dist",
"lint": "eslint . --ext .ts",
"lint": "eslint --ext .ts .",
"build": "npm run test && npm run clean && npm run rollup"
},
"engines": {
"node": ">=14"
},
"repository": {

@@ -44,0 +46,0 @@ "type": "git",

@@ -1,2 +0,3 @@

# PDF2Pic
# pdf2pic
[![Donate][paypal-image]](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=938FMCPPQG4DQ&currency_code=USD&source=url)

@@ -11,28 +12,28 @@ ![Build Status][github-actions-url]

[![Known Vulnerabilities](https://snyk.io/test/npm/pdf2pic/badge.svg)](https://snyk.io/test/npm/pdf2pic)
[![Downloads Stats][npm-downloads]][npm-url]
A utility for converting pdf to image and base64 format.
[![Downloads Stats][npm-downloads]][npm-url]
A utility for converting pdf to image, base64 or buffer format.
> IMPORTANT NOTE: Please support this library by donating via [PayPal](https://www.paypal.com/paypalme/yakovmeister), your help is much appreciated. Contributors are also welcome!
## Prerequisite
* node >= 12.x
* graphicsmagick
* ghostscript
### Don't have graphicsmagick and ghostscript yet?
Follow [this](docs/gm-installation.md) guide to install the required dependencies.
## Installation
## Prerequisites
* node >= 14.x
* graphicsmagick
* ghostscript
### Don't have graphicsmagick and ghostscript yet?
Follow [this](docs/gm-installation.md) guide to install the required dependencies.
## Installation
```
npm install -S pdf2pic
npm install --save pdf2pic
```
## Usage
### Converting specific page of PDF from path, then saving as image file
## Usage
### Converting specific page of PDF from path, then saving as image file
```javascript

@@ -49,16 +50,16 @@ import { fromPath } from "pdf2pic";

};
const storeAsImage = fromPath("/path/to/pdf/sample.pdf", options);
const convert = fromPath("/path/to/pdf/sample.pdf", options);
const pageToConvertAsImage = 1;
storeAsImage(pageToConvertAsImage).then((resolve) => {
console.log("Page 1 is now converted as image");
convert(pageToConvertAsImage, { responseType: "image" })
.then((resolve) => {
console.log("Page 1 is now converted as image");
return resolve;
});
return resolve;
});
```
```
### Nuff talk, show me how:
More usage example can be found [here](https://github.com/yakovmeister/pdf2pic-examples).
More usage example can be found [here](https://github.com/yakovmeister/pdf2pic-examples).
## pdf2pic API

@@ -69,111 +70,109 @@

- [fromBase64(b64string, options)](#frombase64b64string-options)
### fromPath(filePath, options)
Initialize PDF to image conversion by supplying a file path
#### Functions
Convert a specific page of the PDF to Image/Base64 by supplying a file path
### 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
```javascript
fromPath(filePath, options)(page, isBase64)
fromPath(filePath, options)(page, convertOptions)
```
* filePath - pdf file's path
* options - see [options](#options).
* page - page number to convert to an image
* isBase64 - if true, `convert()` will return base64 output instead
---
Converts PDF to Image/Base64 by supplying a file path
* filePath - pdf file's path
* options - see [options](#options).
* page - page number to convert to an image
* convertOptions - see [convertOptions](#convertoptions).
---
Converts PDF to Image/Base64/Buffer by supplying a file path
```javascript
fromPath(filePath, options).bulk(pages, isBase64)
fromPath(filePath, options).bulk(pages, convertOptions)
```
* filePath - pdf file's path
* options - see [options](#options).
* pages - page numbers to convert to image
* set `pages` to `-1` to convert all pages
* filePath - pdf file's path
* options - see [options](#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`
* isBase64 - if true, `bulk()` will return an array of base64 output instead
* convertOptions - see [convertOptions](#convertoptions)
---
Set GraphicsMagick's subclass or path
Set GraphicsMagick's subclass or path
```javascript
fromPath(filePath, options).setGMClass(subClass)
```
fromPath(filePath, options).setGMClass(subClass)
```
NOTE: should be called before calling `convert()` or `bulk()`.
* filePath - pdf file's path
* options - see [options](#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
* filePath - pdf file's path
* options - see [options](#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 by supplying a buffer
### 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
```javascript
fromBuffer(buffer, options)(page, isBase64)
fromBuffer(buffer, options)(page, convertOptions)
```
Functions same as `fromPath(filePath, options)(page, isBase64)` only input is changed
Functions same as `fromPath(filePath, options)(page, convertOptions)` only input is changed
---
Converts PDF to Image/Base64 by supplying a buffer
Converts PDF to Image/Base64/Buffer by supplying a buffer:
```javascript
fromBuffer(buffer, options).bulk(pages, isBase64)
fromBuffer(buffer, options).bulk(pages, convertOptions)
```
Functions same as `fromPath(filePath, options).bulk(pages, isBase64)` only input is changed
Functions same as `fromPath(filePath, options).bulk(pages, convertOptions)` only input is changed
---
Set GraphicsMagick's subclass or path
Set GraphicsMagick's subclass or path:
```javascript
fromBuffer(buffer, options).setGMClass(subClass)
```
Functions same as `fromPath(filePath, options).setGMClass(subClass)` only input is changed
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 by supplying a base64 string
### 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:
```javascript
fromBase64(b64string, options)(page, isBase64)
fromBase64(b64string, options)(page, convertOptions)
```
Functions same as `fromPath(filePath, options)(page, isBase64)` only input is changed
Functions same as `fromPath(filePath, options)(page, convertOptions)` only input is changed.
---
Converts PDF to Image/Base64 by supplying a base64 string
Converts PDF to Image/Base64/Buffer by supplying a base64 string:
```javascript
fromBase64(b64string, options).bulk(pages, isBase64)
fromBase64(b64string, options).bulk(pages, convertOptions)
```
Functions same as `fromPath(filePath, options).bulk(pages, isBase64)` only input is changed
Functions same as `fromPath(filePath, options).bulk(pages, convertOptions)` only input is changed.
---
Set GraphicsMagick's subclass or path
Set GraphicsMagick's subclass or path:
```javascript
fromBase64(b64string, options).setGMClass(subClass)
```
fromBase64(b64string, options).setGMClass(subClass)
```
Functions same as `fromPath(filePath, options).setGMClass(subClass)` only input is changed.
---
### options
### options
Following are the options that can be passed on the pdf2pic api:

@@ -192,13 +191,21 @@

### convertOptions
| option | default value | description |
|--------------|---------------|-------------|
| 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')
* 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
* Create a new PR
## License
pdf2pic is [MIT licensed](LICENSE).
<!-- Markdown link & img dfn's -->

@@ -205,0 +212,0 @@ [npm-image]: https://img.shields.io/npm/v/pdf2pic.svg?style=flat-square

@@ -22,4 +22,5 @@ {

"exclude": [
"dist",
"node_modules"
]
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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