Comparing version 2.2.0 to 2.2.1
@@ -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 n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(t),a=n(e),o=n(i);const h={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=r.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,n)=>{this.gmBaseCommand(t,i).stream(this.format,((t,i)=>{let r="";if(t)return n(t);i.on("data",(t=>{r+=t.toString("binary")})).on("end",(()=>{const t=Buffer.from(r,"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(((n,r)=>{this.gmBaseCommand(t,s).write(i,(t=>t?r(t):n({name:a.default.basename(i),size:`${this.width}x${this.height}`,fileSize:o.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=r.default.subClass({imageMagick:t}),this):"imagemagick"===t.toLocaleLowerCase()?(this.gm=r.default.subClass({imageMagick:!0}),this):(this.gm=r.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(n,r){function a(t){try{h(s.next(t))}catch(t){r(t)}}function o(t){try{h(s.throw(t))}catch(t){r(t)}}function h(t){var e;t.done?n(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(a,o)}h((s=s.apply(t,e||[])).next())}))}function c(t,e,i,s,n=!1){const r=f(e,i),a=s.map((e=>{if(e<1)throw new Error("Page number should be more than or equal 1");return n?t.toBase64(r,e-1):t.writeImage(r,e-1)}));return Promise.all(a)}function d(t,e,i,s=1,n=!1){return l(this,void 0,void 0,(function*(){const r=-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<r.length;s+=10)a.concat(...yield c(t,e,i,r.slice(s,s+10),n));return a}))}function g(t,e,i=h){const s=new u;i=Object.assign(Object.assign({},h),i);const n=(i=1,n=!1)=>{if(i<1)throw new Error("Page number should be more than or equal 1");const r=f(t,e);return n?s.toBase64(r,i-1):s.writeImage(r,i-1)};return n.bulk=(i,n=!1)=>d(s,t,e,i,n),n.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),n.setGMClass=t=>{s.setGMClass(t)},n.setOptions(),n}exports.fromBase64=function(t,e=h){return g("base64",t,e)},exports.fromBuffer=function(t,e=h){return g("buffer",t,e)},exports.fromPath=function(t,e=h){return g("path",t,e)}; | ||
"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)}; |
{ | ||
"name": "pdf2pic", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "A utility for converting pdf to image and base64 format.", | ||
@@ -11,5 +11,2 @@ "main": "dist/index.js", | ||
}, | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"devDependencies": { | ||
@@ -44,3 +41,3 @@ "@rollup/plugin-alias": "^3.1.2", | ||
"clean": "rimraf ./dist", | ||
"lint": "./node_modules/.bin/eslint . --ext .ts", | ||
"lint": "eslint . --ext .ts", | ||
"build": "npm run test && npm run clean && npm run rollup" | ||
@@ -52,2 +49,5 @@ }, | ||
}, | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"keywords": [ | ||
@@ -54,0 +54,0 @@ "pdf-to-image", |
# PDF2Pic | ||
[![Donate][paypal-image]](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=938FMCPPQG4DQ¤cy_code=USD&source=url) | ||
[![Build Status][travis-image]][travis-url] | ||
[![CodeFactor](https://www.codefactor.io/repository/github/yakovmeister/pdf2image/badge/next)](https://www.codefactor.io/repository/github/yakovmeister/pdf2image/overview/next) | ||
![Build Status][github-actions-url] | ||
[![CodeFactor](https://www.codefactor.io/repository/github/yakovmeister/pdf2image/badge)](https://www.codefactor.io/repository/github/yakovmeister/pdf2image) | ||
[![Maintainability](https://api.codeclimate.com/v1/badges/6d7bfbae9057998bda99/maintainability)](https://codeclimate.com/github/yakovmeister/pdf2image/maintainability) | ||
@@ -62,3 +62,7 @@ [![Test Coverage](https://api.codeclimate.com/v1/badges/6d7bfbae9057998bda99/test_coverage)](https://codeclimate.com/github/yakovmeister/pdf2image/test_coverage) | ||
## pdf2pic API | ||
## pdf2pic API | ||
- [fromPath(filePath, options)](#frompathfilepath-options) | ||
- [fromBuffer(buffer, options)](#frombufferbuffer-options) | ||
- [fromBase64(b64string, options)](#frombase64b64string-options) | ||
@@ -71,10 +75,10 @@ ### fromPath(filePath, options) | ||
Converts specific page of the PDF to Image/Base64 by supplying a file path | ||
Convert a specific page of the PDF to Image/Base64 by supplying a file path | ||
```javascript | ||
fromPath(filePath, options).convert(pageNumber, isBase64) | ||
fromPath(filePath, options)(page, isBase64) | ||
``` | ||
* filePath - pdf file's path | ||
* options - see [options](#options). | ||
* pageNumber - page number to be converted to image | ||
* page - page number to convert to an image | ||
* isBase64 - if true, `convert()` will return base64 output instead | ||
@@ -86,9 +90,9 @@ | ||
```javascript | ||
fromPath(filePath, options).bulk(pageNumber, isBase64) | ||
fromPath(filePath, options).bulk(pages, isBase64) | ||
``` | ||
* filePath - pdf file's path | ||
* options - see [options](#options). | ||
* pageNumber - page number/s to be converted to images | ||
* set `pageNumber` to `-1` to select all pages | ||
* `pageNumber` also accepts an array indicating the page number e.g. `[1,2,3]` | ||
* 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` | ||
@@ -118,8 +122,8 @@ * isBase64 - if true, `bulk()` will return an array of base64 output instead | ||
Converts specific page of the PDF to Image/Base64 by supplying a buffer | ||
Convert a specific page of the PDF to Image/Base64 by supplying a buffer | ||
```javascript | ||
fromBuffer(buffer, options)(pageNumber, isBase64) | ||
fromBuffer(buffer, options)(page, isBase64) | ||
``` | ||
Functions same as `fromPath(filePath, options)(pageNumber, isBase64)` only input is changed | ||
Functions same as `fromPath(filePath, options)(page, isBase64)` only input is changed | ||
@@ -130,6 +134,6 @@ --- | ||
```javascript | ||
fromBuffer(buffer, options).bulk(pageNumber, isBase64) | ||
fromBuffer(buffer, options).bulk(pages, isBase64) | ||
``` | ||
Functions same as `fromPath(filePath, options).bulk(pageNumber, isBase64)` only input is changed | ||
Functions same as `fromPath(filePath, options).bulk(pages, isBase64)` only input is changed | ||
@@ -151,8 +155,8 @@ --- | ||
Converts specific page of the PDF to Image/Base64 by supplying a base64 string | ||
Convert a specific page of the PDF to Image/Base64 by supplying a base64 string | ||
```javascript | ||
fromBase64(b64string, options)(pageNumber, isBase64) | ||
fromBase64(b64string, options)(page, isBase64) | ||
``` | ||
Functions same as `fromPath(filePath, options)(pageNumber, isBase64)` only input is changed | ||
Functions same as `fromPath(filePath, options)(page, isBase64)` only input is changed | ||
@@ -163,6 +167,6 @@ --- | ||
```javascript | ||
fromBase64(b64string, options).bulk(pageNumber, isBase64) | ||
fromBase64(b64string, options).bulk(pages, isBase64) | ||
``` | ||
Functions same as `fromPath(filePath, options).bulk(pageNumber, isBase64)` only input is changed | ||
Functions same as `fromPath(filePath, options).bulk(pages, isBase64)` only input is changed | ||
@@ -175,3 +179,3 @@ --- | ||
Functions same as `fromPath(filePath, options).setGMClass(subClass)` only input is changed | ||
Functions same as `fromPath(filePath, options).setGMClass(subClass)` only input is changed. | ||
@@ -181,10 +185,14 @@ --- | ||
Following are the options that can be passed on the pdf2pic api: | ||
* quality - set output's image quality | ||
* format - set output's file format | ||
* width - set output's width | ||
* height - set output's height | ||
* density - controls output's dpi (i am not so sure) | ||
* savePath - set output's save path | ||
* saveFilename - set output's file name | ||
* compression - set output's compression method | ||
| option | default value | description | | ||
|--------------|--------------- |------------------------------| | ||
| quality | `0` | Image compression level. Value depends on `format`, usually from `0` to `100` ([more info](http://www.graphicsmagick.org/GraphicsMagick.html#details-quality)) | | ||
| format | `'png'` | Formatted image characteristics / image format ([image characteristics](http://www.graphicsmagick.org/GraphicsMagick.html#details-format), [image format](http://www.graphicsmagick.org/formats.html)) | | ||
| width | `768` | Output width | | ||
| height | `512` | Output height | | ||
| density | `72` | Output DPI (dots per inch) ([more info](http://www.graphicsmagick.org/GraphicsMagick.html#details-density)) | | ||
| savePath | `'./'` | Path where to save the output | | ||
| saveFilename | `'untitled'` | Output filename | | ||
| compression | `'jpeg'` | Compression method ([more info](http://www.graphicsmagick.org/GraphicsMagick.html#details-compress)) | | ||
@@ -205,4 +213,3 @@ ## Contributing | ||
[npm-downloads]: https://img.shields.io/npm/dm/pdf2pic.svg?style=flat-square | ||
[travis-image]: https://travis-ci.org/yakovmeister/pdf2image.svg?branch=next | ||
[travis-url]: https://travis-ci.org/yakovmeister/pdf2image | ||
[paypal-image]: https://img.shields.io/badge/Donate-PayPal-green.svg | ||
[github-actions-url]: https://github.com/yakovmeister/pdf2image/actions/workflows/test.yml/badge.svg?branch=master | ||
[paypal-image]: https://img.shields.io/badge/Donate-PayPal-green.svg |
19954
20
206