-
Notifications
You must be signed in to change notification settings - Fork 8
/
PDF2Image.js
76 lines (68 loc) · 2.71 KB
/
PDF2Image.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const fs = require('mz/fs');
const path = require('path');
const _ = require('lodash');
const withTmpDir = require('with-tmp-dir-promise').WithTempDir;
// const exec = require('mz/child_process').execFile;
const {requireNativeExecutableSync} = require('require-native-executable');
const { execFile } = require('child_process');
requireNativeExecutableSync('gm');
async function pdfToImage (pdfBuf, imgtype = 'jpg', dpi = 200) {
return withTmpDir(async (tmpdir) => {
const srcpath = path.join(tmpdir, 'in.pdf');
const outpath = path.join(tmpdir, `out-%04d.${imgtype}`);
// Write input files
await fs.writeFile(srcpath, pdfBuf);
// Convert
var cmd = `gm convert +adjoin -trim -format ${imgtype} -density ${dpi} ${srcpath} -quality 95 ${outpath}`;
cmd = cmd.split(' ');
// execute the command reliable async using a promise
await new Promise((resolve, reject) => {
execFile(cmd[0], cmd.slice(1), execThenFunct(resolve, reject));
});
// Read all the files
const files = await fs.readdir(tmpdir);
// console.log('files', files);
const imgs = files.filter(file => _.endsWith(file, `.${imgtype}`)).sort();
const imgPaths = imgs.map(img => path.join(tmpdir, img));
const imgPromises = imgPaths.map(img => fs.readFile(img));
return Promise.all(imgPromises);
}, { unsafeCleanup: true, prefix: 'pdftoolz-pdf2img-' });
}
async function imageToPdf (buffer, opts) {
return withTmpDir(async (tmpdir) => {
const srcpath = path.join(tmpdir, 'imageToPdfInPutImage');
const outpath = path.join(tmpdir, `out-%04d.pdf`);
// Write input files
await fs.writeFile(srcpath, buffer);
// Convert
var cmd = `gm convert ${srcpath} ${outpath}`;
cmd = cmd.split(' ');
// execute the command reliable async using a promise
await new Promise((resolve, reject) => {
execFile(cmd[0], cmd.slice(1), {timeout: 2000}, execThenFunct(resolve, reject));
});
// Read all the files
return fs.readFile(outpath);
}, { unsafeCleanup: true, prefix: 'pdftoolz-pdf2img-' });
}
/** execThenFunct
* help to wrapp a promise around execFile
* otherwise execFile returns only the childprocess, before it has finished
*/
function execThenFunct (resolve, reject) {
return function (err, data) {
let returnValue;
if (err) {
returnValue = 'ERROR:\n' + err;
reject(returnValue);
} else {
returnValue = data.toString();
resolve(returnValue);
}
console.log('cmd return value: ' + returnValue);
};
}
module.exports = {
imageToPdf,
pdfToImage
};