Comparing version 3.0.3 to 3.1.0
@@ -11,2 +11,3 @@ /// <reference types="node" /> | ||
private height; | ||
private preserveAspectRatio; | ||
private density; | ||
@@ -26,2 +27,3 @@ private savePath; | ||
setSize(width: number, height?: number): Graphics; | ||
setPreserveAspectRatio(preserveAspectRatio: boolean): Graphics; | ||
setDensity(density: number): Graphics; | ||
@@ -28,0 +30,0 @@ setSavePath(savePath: string): Graphics; |
@@ -1,1 +0,1 @@ | ||
"use strict";var e=require("gm"),t=require("path"),i=require("fs"),s=require("stream");const r={quality:0,format:"png",width:768,height:512,density:72,savePath:"./",saveFilename:"untitled",compression:"jpeg"};function n(e,t,i,s){return new(i||(i=Promise))((function(r,n){function a(e){try{h(s.next(e))}catch(e){n(e)}}function o(e){try{h(s.throw(e))}catch(e){n(e)}}function h(e){var t;e.done?r(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(a,o)}h((s=s.apply(e,t||[])).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=e.subClass({imageMagick:!1})}generateValidFilename(e){let i=t.join(this.savePath,this.saveFilename);return this.savePath.startsWith("./")&&(i=`./${i}`),"number"==typeof e&&(i=`${i}.${e+1}`),`${i}.${this.format}`}gmBaseCommand(e,t){return this.gm(e,t).density(this.density,this.density).resize(this.width,this.height,"!").quality(this.quality).compress(this.compression)}toBase64(e,t){return n(this,void 0,void 0,(function*(){const{buffer:i,size:s,page:r}=yield this.toBuffer(e,t);return{base64:i.toString("base64"),size:s,page:r}}))}toBuffer(e,t){const i=`${e.path}[${t}]`;return new Promise(((s,r)=>{this.gmBaseCommand(e,i).stream(this.format,((e,i)=>{const n=[];if(e)return r(e);i.on("data",(e=>{n.push(e)})).on("end",(()=>s({buffer:Buffer.concat(n),size:`${this.width}x${this.height}`,page:t+1})))}))}))}writeImage(e,s){const r=this.generateValidFilename(s),n=`${e.path}[${s}]`;return new Promise(((a,o)=>{this.gmBaseCommand(e,n).write(r,(e=>e?o(e):a({name:t.basename(r),size:`${this.width}x${this.height}`,fileSize:i.statSync(r).size/1e3,path:r,page:s+1})))}))}identify(e,t){const i=this.gm(e);return new Promise(((e,s)=>{t?i.identify(t,((t,i)=>t?s(t):e(i.replace(/^[\w\W]*?1/,"1")))):i.identify(((t,i)=>t?s(t):e(i)))}))}setQuality(e){return this.quality=e,this}setFormat(e){return this.format=e,this}setSize(e,t){return this.width=e,this.height=t||e,this}setDensity(e){return this.density=e,this}setSavePath(e){return this.savePath=e,this}setSaveFilename(e){return this.saveFilename=e,this}setCompression(e){return this.compression=e,this}setGMClass(t){return"boolean"==typeof t?(this.gm=e.subClass({imageMagick:t}),this):"imagemagick"===t.toLocaleLowerCase()?(this.gm=e.subClass({imageMagick:!0}),this):(this.gm=e.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 o(e){return new s.Readable({read(){this.push(e),this.push(null)}})}function h(e,t){if("buffer"===e)return o(t);if("path"===e)return i.createReadStream(t);if("base64"===e)return s=t,o(Buffer.from(s,"base64"));var s;throw new Error("Cannot recognize specified source")}const u=e=>{var t;if(e&&"object"!=typeof e)throw new Error(`Invalid convertOptions type: ${e}`);return null!==(t=null==e?void 0:e.responseType)&&void 0!==t?t:"image"};function f(e,t,s=r){const f=new a;s=Object.assign(Object.assign({},r),s);const m=(e,t,i)=>{if(t<1)throw new Error("Page number should be more than or equal 1");const s=u(i);switch(s){case"base64":return f.toBase64(e,t-1);case"image":return f.writeImage(e,t-1);case"buffer":return f.toBuffer(e,t-1);default:throw new Error(`Invalid responseType: ${s}`)}},c=(e,t,i)=>Promise.all(t.map((t=>m(e,t,i)))),l=(i=1,s)=>{const r=h(e,t);return m(r,i,s)};return l.bulk=(s,r)=>n(this,void 0,void 0,(function*(){const a=yield function(e,t){return n(this,void 0,void 0,(function*(){if("buffer"===e)return t;if("path"===e)return yield i.promises.readFile(t);if("base64"===e)return Buffer.from(t,"base64");throw new Error("Cannot recognize specified source")}))}(e,t),h=-1===s?yield function(e,t){return n(this,void 0,void 0,(function*(){return(yield e.identify(t,"%p ")).split(" ").map((e=>parseInt(e,10)))}))}(f,o(a)):Array.isArray(s)?s:[s],u=[];for(let e=0;e<h.length;e+=10)u.push(...yield c(o(a),h.slice(e,e+10),r));return u})),l.setOptions=()=>function(e,t){return void e.setQuality(t.quality).setFormat(t.format).setSize(t.width,t.height).setDensity(t.density).setSavePath(t.savePath).setSaveFilename(t.saveFilename).setCompression(t.compression)}(f,s),l.setGMClass=e=>{f.setGMClass(e)},l.setOptions(),l}exports.fromBase64=function(e,t=r){return f("base64",e,t)},exports.fromBuffer=function(e,t=r){return f("buffer",e,t)},exports.fromPath=function(e,t=r){return f("path",e,t)}; | ||
"use strict";var e=require("gm"),t=require("path"),s=require("fs"),i=require("stream");const r={quality:0,format:"png",width:768,height:512,density:72,preserveAspectRatio:!1,savePath:"./",saveFilename:"untitled",compression:"jpeg"};function n(e,t,s,i){return new(s||(s=Promise))((function(r,n){function a(e){try{h(i.next(e))}catch(e){n(e)}}function o(e){try{h(i.throw(e))}catch(e){n(e)}}function h(e){var t;e.done?r(e.value):(t=e.value,t instanceof s?t:new s((function(e){e(t)}))).then(a,o)}h((i=i.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class a{constructor(){this.quality=0,this.format="png",this.width=768,this.height=512,this.preserveAspectRatio=!1,this.density=72,this.savePath="./",this.saveFilename="untitled",this.compression="jpeg",this.gm=e.subClass({imageMagick:!1})}generateValidFilename(e){let s=t.join(this.savePath,this.saveFilename);return this.savePath.startsWith("./")&&(s=`./${s}`),"number"==typeof e&&(s=`${s}.${e+1}`),`${s}.${this.format}`}gmBaseCommand(e,t){return this.gm(e,t).density(this.density,this.density).resize(this.width,this.height,this.preserveAspectRatio?"^":"!").quality(this.quality).compress(this.compression)}toBase64(e,t){return n(this,void 0,void 0,(function*(){const{buffer:s,size:i,page:r}=yield this.toBuffer(e,t);return{base64:s.toString("base64"),size:i,page:r}}))}toBuffer(e,t){const s=`${e.path}[${t}]`;return new Promise(((i,r)=>{this.gmBaseCommand(e,s).stream(this.format,((e,s)=>{const n=[];if(e)return r(e);s.on("data",(e=>{n.push(e)})).on("end",(()=>i({buffer:Buffer.concat(n),size:`${this.width}x${this.height}`,page:t+1})))}))}))}writeImage(e,i){const r=this.generateValidFilename(i),n=`${e.path}[${i}]`;return new Promise(((a,o)=>{this.gmBaseCommand(e,n).write(r,(e=>e?o(e):a({name:t.basename(r),size:`${this.width}x${this.height}`,fileSize:s.statSync(r).size/1e3,path:r,page:i+1})))}))}identify(e,t){const s=this.gm(e);return new Promise(((e,i)=>{t?s.identify(t,((t,s)=>t?i(t):e(s.replace(/^[\w\W]*?1/,"1")))):s.identify(((t,s)=>t?i(t):e(s)))}))}setQuality(e){return this.quality=e,this}setFormat(e){return this.format=e,this}setSize(e,t){return this.width=e,this.height=this.preserveAspectRatio||t?t:e,this}setPreserveAspectRatio(e){return this.preserveAspectRatio=e,this}setDensity(e){return this.density=e,this}setSavePath(e){return this.savePath=e,this}setSaveFilename(e){return this.saveFilename=e,this}setCompression(e){return this.compression=e,this}setGMClass(t){return"boolean"==typeof t?(this.gm=e.subClass({imageMagick:t}),this):"imagemagick"===t.toLocaleLowerCase()?(this.gm=e.subClass({imageMagick:!0}),this):(this.gm=e.subClass({appPath:t}),this)}getOptions(){return{quality:this.quality,format:this.format,width:this.width,height:this.height,preserveAspectRatio:this.preserveAspectRatio,density:this.density,savePath:this.savePath,saveFilename:this.saveFilename,compression:this.compression}}}function o(e){return new i.Readable({read(){this.push(e),this.push(null)}})}function h(e,t){if("buffer"===e)return o(t);if("path"===e)return s.createReadStream(t);if("base64"===e)return i=t,o(Buffer.from(i,"base64"));var i;throw new Error("Cannot recognize specified source")}const u=e=>{var t;if(e&&"object"!=typeof e)throw new Error(`Invalid convertOptions type: ${e}`);return null!==(t=null==e?void 0:e.responseType)&&void 0!==t?t:"image"};function c(e,t,i=r){const c=new a;i=Object.assign(Object.assign({},r),i);const f=(e,t,s)=>{if(t<1)throw new Error("Page number should be more than or equal 1");const i=u(s);switch(i){case"base64":return c.toBase64(e,t-1);case"image":return c.writeImage(e,t-1);case"buffer":return c.toBuffer(e,t-1);default:throw new Error(`Invalid responseType: ${i}`)}},p=(e,t,s)=>Promise.all(t.map((t=>f(e,t,s)))),m=(s=1,i)=>{const r=h(e,t);return f(r,s,i)};return m.bulk=(i,r)=>n(this,void 0,void 0,(function*(){const a=yield function(e,t){return n(this,void 0,void 0,(function*(){if("buffer"===e)return t;if("path"===e)return yield s.promises.readFile(t);if("base64"===e)return Buffer.from(t,"base64");throw new Error("Cannot recognize specified source")}))}(e,t),h=-1===i?yield function(e,t){return n(this,void 0,void 0,(function*(){return(yield e.identify(t,"%p ")).split(" ").map((e=>parseInt(e,10)))}))}(c,o(a)):Array.isArray(i)?i:[i],u=[];for(let e=0;e<h.length;e+=10)u.push(...yield p(o(a),h.slice(e,e+10),r));return u})),m.setOptions=()=>function(e,t){return void e.setQuality(t.quality).setFormat(t.format).setPreserveAspectRatio(t.preserveAspectRatio).setSize(t.width,t.height).setDensity(t.density).setSavePath(t.savePath).setSaveFilename(t.saveFilename).setCompression(t.compression)}(c,i),m.setGMClass=e=>{c.setGMClass(e)},m.setOptions(),m}exports.fromBase64=function(e,t=r){return c("base64",e,t)},exports.fromBuffer=function(e,t=r){return c("buffer",e,t)},exports.fromPath=function(e,t=r){return c("path",e,t)}; |
@@ -6,2 +6,3 @@ export type Options = { | ||
height?: number; | ||
preserveAspectRatio?: boolean; | ||
density?: number; | ||
@@ -8,0 +9,0 @@ savePath?: string; |
{ | ||
"name": "pdf2pic", | ||
"version": "3.0.3", | ||
"version": "3.1.0", | ||
"description": "A utility for converting pdf to image formats. Supports different outputs: directly to file, base64 or buffer.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -177,12 +177,13 @@ # pdf2pic | ||
| 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)) | | ||
| 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 | | ||
| 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](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)) | | ||
@@ -189,0 +190,0 @@ ### convertOptions |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26613
203
214