Comparing version 1.0.2 to 1.0.3-beta.0
"use strict"; | ||
module.exports = require('./lib/compiled/index.js') | ||
module.exports = require('./lib/index.js') |
{ | ||
"name": "pdf2pic", | ||
"version": "1.0.2", | ||
"version": "1.0.3-beta.0", | ||
"description": "Node module for converting PDF to image based on pdf2img by Fitra Adity", | ||
@@ -14,6 +14,8 @@ "main": "index.js", | ||
"clean": "rm -rf ./lib && mkdir ./lib", | ||
"clean:test": "rm -rf ./test/o && mkdir ./test/o", | ||
"clean:compile": "rm -rf ./lib/compiled", | ||
"compile:babel": "babel-compile -p es2017 -p es2015 src:lib/compiled", | ||
"compile:uglify": "uglifyjs ./lib/compiled/helper.js ./lib/compiled/index.js -o ./lib/main.min.js --compress sequences=false,toplevel=true --mangle --verbose", | ||
"compile": "npm run clean && npm run compile:babel", | ||
"test": "_mocha test" | ||
"compile:minify": "minify lib/compiled -d lib", | ||
"compile": "npm run clean && npm run compile:babel && npm run compile:minify && npm run clean:compile", | ||
"test": "npm run clean:test && _mocha test" | ||
}, | ||
@@ -33,3 +35,4 @@ "keywords": [ | ||
"fs-extra": "^4.0.2", | ||
"gm": "^1.23.0" | ||
"gm": "^1.23.0", | ||
"gm-base64": "^1.1.1" | ||
}, | ||
@@ -39,2 +42,3 @@ "devDependencies": { | ||
"babel-compile": "^2.0.0", | ||
"babel-minify": "^0.2.0", | ||
"babel-plugin-syntax-async-functions": "^6.1.4", | ||
@@ -41,0 +45,0 @@ "babel-plugin-transform-regenerator": "^6.1.4", |
# PDF2Pic | ||
[![Build Status](https://travis-ci.org/yakovmeister/pdf2image.svg?branch=1.0)](https://travis-ci.org/yakovmeister/pdf2image) | ||
[![Version](https://img.shields.io/badge/version-1.0.2-blue.svg)]() | ||
[![Version](https://img.shields.io/badge/version-1.0.3%20beta%200-red.svg)]() | ||
Node module for converting PDF to image based on pdf2img by Fitra Adity | ||
@@ -15,2 +15,3 @@ | ||
* gm | ||
* gm-base64 | ||
@@ -58,4 +59,7 @@ ## Installation | ||
## To do | ||
* base64 as output (beta) | ||
___ | ||
This module is based on Fitra Adity's Great work. |
require("babel-polyfill") | ||
require('es8-polyfill') | ||
require('gm-base64') | ||
@@ -78,2 +79,29 @@ import Promise from "bluebird" | ||
/** | ||
* GM command - toBase64 | ||
* @access private | ||
* @param {Stream} stream | ||
* @param {String} output | ||
* @param {String} filename | ||
* @param {Integer} page | ||
* @return {Promise} | ||
*/ | ||
Private(this).toBase64 = (stream, filename, page) => { | ||
return new Promise((resolve, reject) => { | ||
gm(stream, filename) | ||
.density(Private(this).density, Private(this).density) | ||
.resize(Private(this).size) | ||
.quality(Private(this).quality) | ||
.toBase64(Private(this).format, (error, base64) => { | ||
if(error) | ||
return reject(error) | ||
return resolve({ | ||
base64, | ||
page | ||
}) | ||
}) | ||
}) | ||
} | ||
} | ||
@@ -113,2 +141,51 @@ | ||
/** | ||
* Intialize pdftobase64 converter | ||
* @param {String} pdf_path path to file | ||
* @param {Page} page page number to be converted | ||
* @return {Object} image status | ||
*/ | ||
async convertToBase64(pdf_path, page = 1) { | ||
this.isValidPDF(pdf_path) | ||
this.fileExists(pdf_path) | ||
let output = path.basename(pdf_path, path.extname(path.basename(pdf_path))) | ||
// Set output dir | ||
if (this.get("savedir")) | ||
this.set("savedir", this.get("savedir") + path.sep) | ||
else | ||
this.set("savedir", output + path.sep) | ||
fs.mkdirsSync(this.get("savedir")) | ||
if(!this.get("savename")) | ||
this.set("savename", output) | ||
let pages = await this.getPageCount(pdf_path) | ||
if(page > pages) | ||
throw {error: "InvalidPageSelection", message: "Cannot convert non-existent page"} | ||
return await this.toBase64(pdf_path, page, true) | ||
} | ||
/** | ||
* Intialize pdftobase64 converter, well the bulk version | ||
* @param {String} pdf_path path to file | ||
* @param {Page} page page number to be converted (-1 for all pages) | ||
* @return {Object} image status | ||
*/ | ||
async convertToBase64Bulk(pdf_path, pages = -1) { | ||
let result = [] | ||
pages = pages === -1 ? await this.getPage(pdf_path) : (Array.isArray(pages) ? pages : [1]) | ||
await Promise.each(pages, async function(each) { | ||
result.push(await this.convertToBase64(pdf_path, each)) | ||
}.bind(this)) | ||
return result | ||
} | ||
/** | ||
* Intialize converter, well the bulk version | ||
@@ -158,3 +235,3 @@ * @param {String} pdf_path path to file | ||
*/ | ||
async toImage(pdf_path, page) { | ||
async toImage(pdf_path, page = 1) { | ||
let iStream = fs.createReadStream(pdf_path) | ||
@@ -168,2 +245,15 @@ let file = `${this.get("savedir")}${this.get("savename")}_${page}.${this.get("format")}` | ||
/** | ||
* Converts pdf to image | ||
* @param {String} pdf_path | ||
* @param {Integer} page | ||
* @return {Promise} | ||
*/ | ||
async toBase64(pdf_path, page = 1) { | ||
let iStream = fs.createReadStream(pdf_path) | ||
let filename = `${this.getFilePath(iStream)}[${page - 1}]` | ||
return await Private(this).toBase64(iStream,filename, page) | ||
} | ||
/** | ||
* Get file path | ||
@@ -170,0 +260,0 @@ * @param {Stream} stream |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
63
21745
4
12
9
272
2
1
+ Addedgm-base64@^1.1.1
+ Addedgm-base64@1.1.1(transitive)