-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
72 lines (53 loc) · 2.03 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
const shell = require('shelljs');
const assert = require('assert');
const escape = require('shell-escape');
const pdfjamOptions = ['nup', 'scale', 'trim', 'suffix', 'outfile', 'papersize'];
function pdfnup(input, _rows, _cols, _options) {
assert(arguments.length >= 3, "Insufficient arguments for pdfjam.nup, expected at least 3, was given " + arguments.length);
const rows = +_rows;
const cols = +_cols;
assert(Number.isSafeInteger(rows) && rows > 0, "The number of rows needs to be a number");
assert(Number.isSafeInteger(cols) && cols > 0, "The number of columns needs to be a number");
const options = Object.assign({}, _options, {nup: `${rows}x${cols}`});
if (! ("suffix" in options) && ! ("outfile" in options)) {
options.suffix = "nup";
}
return pdfjam(input, options);
}
async function pdfjam(_input, _options) {
/* Properly quote inputs */
let input = _input;
if (!Array.isArray(_input)) {
input = [_input];
}
const options = Object.assign({}, _options);
let args = [];
if (options.orientation === "landscape") {
args.push("--landscape");
}
if (options.batch) {
args.push("--batch");
}
/* So they don't get caught in the assert below */
delete options.orientation;
delete options.batch;
for (const opt of Object.keys(options)) {
assert(pdfjamOptions.indexOf(opt) !== -1, `Unknown pdfjam option: ${opt}`);
args = args.concat([`--${opt}`, options[opt]]);
}
args = args.concat(input);
assert(shell.which('pdfjam'), "No local pdfjam installation detected. Install texlive-extra-utils on your system.");
const result = shell.exec(`pdfjam ${escape(args)}`, {
silent: true
});
if (result.code !== 0) {
throw new Error("Pdfjam failed: exit code " + result.code);
// Todo, proper error message
// throw new Error("Pdfjam failed: " + result.stderr);
}
}
module.exports = (function() {
const ret = pdfjam.bind(null);
ret.nup = pdfnup;
return ret;
})();