-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
62 lines (48 loc) · 1.48 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
var spawn = require('child_process').spawn;
function WKHtmlToImage() {
this.command = 'wkhtmltoimage';
this.setCommand = function(path) {
this.command = path;
return this;
}
this.generate = function(input, options, callback) {
var options = options || {};
if (typeof options == 'function') {
callback = options;
options = {};
}
var output = options.output;
delete options.output;
var args = [this.command, '--quiet'];
for (var key in options) {
var val = options[key];
key = key.length === 1 ? '-' + key : '--' + key.replace(/\W+/g, '-').replace(/([a-z\d])([A-Z])/g, '$1-$2').toLowerCase();
if (val !== false)
args.push(key);
if (typeof val !== 'boolean') {
// escape and quote the value if it is a string
if (typeof val === 'string')
val = '"' + val.replace(/(["\\$`])/g, '\\$1') + '"';
args.push(val);
}
}
var isUrl = /^(https?|file):\/\//.test(input);
args.push(isUrl ? '"' + input + '"' : '-'); // stdin if HTML given directly
args.push(output || '-'); // stdout if no output file
if (process.platform === 'win32') {
var child = spawn(args[0], args.slice(1));
} else {
// this nasty business prevents piping problems on linux
var child = spawn('/bin/sh', ['-c', args.join(' ')]);
}
if (callback) {
child.on('exit', callback);
}
if (!isUrl) {
child.stdin.end(input);
}
// return stdout stream so we can pipe
return child.stdout;
}
}
module.exports = new WKHtmlToImage();