-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathindex.js
132 lines (106 loc) · 3.45 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'use strict';
const fs = require('fs');
const os = require('os');
const tmp = require('tmp');
const child = require('child_process');
const Promise = require('bluebird');
const PassThrough = require('stream').PassThrough;
const shellescape = require('shell-escape');
const execFile = Promise.promisify(child.execFile);
const exec = Promise.promisify(child.exec);
const readFile = Promise.promisify(fs.readFile);
const writeFile = Promise.promisify(fs.writeFile);
const isWindows = os.platform() === 'win32';
const genHandle = (num) => {
let s = '', t;
while (num > 0) {
t = (num - 1) % 26;
s = String.fromCharCode(65 + t) + s;
num = (num - t) / 26 | 0;
}
return s;
}
module.exports = (files, options) => new Promise((resolve, reject) => {
if (!Array.isArray(files)) {
reject(new TypeError('Expected files to be an array of paths to PDF files.'));
return;
}
files = files
.map((file) => typeof file === 'string' ? ({ file }) : file)
// Object.keys(file)[0] !== '0' -> Check if is not a Buffer object
.filter((file) => (file !== null && typeof file === 'object' && Object.keys(file)[0] !== '0' && typeof file.file === 'string'));
if (files.length === 0) {
reject(new Error('No files were submitted for merging.'));
return;
}
const output = (buffer) => {
if (options.output === Buffer || String(options.output).toUpperCase() === 'BUFFER') {
return buffer;
}
if (options.output === PassThrough || ['STREAM', 'READSTREAM'].indexOf(String(options.output).toUpperCase()) !== -1) {
const stream = new PassThrough();
stream.end(buffer);
return stream;
}
return writeFile(options.output, buffer)
.then(() => buffer);
}
options = Object.assign({
libPath: 'pdftk',
output: Buffer,
}, options);
if (files.length === 1 && files[0].file && files[0].file.replace(/\\/, '/').split('/').pop() !== '*.pdf') {
const fileObjKeys = Object.keys(files[0])
if (fileObjKeys.length === 1 && fileObjKeys[0] === 'file') {
readFile(files[0].file)
.then((buffer) => {
return output(buffer);
})
.then(resolve)
.catch(reject);
return;
}
}
const tmpFilePath = isWindows
? tmp.tmpNameSync()
: shellescape([tmp.tmpNameSync()]);
const inputPws = []
const args = files.map((value, idx) => {
let file = value.file;
let handle = null;
// Check if we need use handles
if (typeof value.inputPw === 'string' && value.inputPw.length > 0) {
handle = genHandle(idx + 1);
inputPws.push({ handle, inputPw: value.inputPw });
file = `${handle}=${file}`;
}
return isWindows
? `"${file}"`
: shellescape([file.replace(/\\/g, '/')]);
})
if (inputPws.length > 0) {
args.push('input_pw');
Array.prototype.push.apply(args, inputPws.map(item => `${item.handle}=${item.inputPw}`));
}
args.push('cat', 'output', tmpFilePath);
if (options.execOptions) {
args.push(options.execOptions);
}
const childPromise = (isWindows && options.libPath !== 'pdftk')
? execFile(options.libPath, args)
: exec(`${options.libPath} ${args.join(' ')}`);
childPromise
.then(() =>
readFile(tmpFilePath)
)
.then((buffer) =>
new Promise((resolve) => {
fs.unlink(tmpFilePath, () => resolve(buffer));
})
)
.then((buffer) => {
return output(buffer);
})
.then(resolve)
.catch(reject);
});