forked from devongovett/node-wkhtmltopdf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
87 lines (67 loc) · 2.35 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
//WKHTMLTOPDF
var spawn = require('child_process').spawn;
var slang = require('slang');
function wkhtmltopdf(input, options, callback) {
if (!options) {
options = { quiet: true, logging: false, };
} else if (typeof options == 'function') {
callback = options;
options = { quiet: true, logging: false, };
}
var logging = options.logging ? options.logging===true ? true : false : false;
delete options.logging;
var output = options.output;
delete options.output;
var args = [];
args.push(wkhtmltopdf.command );
if ( options.quiet )
args.push('--quiet');
delete options.quiet;
for (var key in options) {
var val = options[key];
key = key.length === 1 ? '-' + key : '--' + slang.dasherize(key);
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);
if (process.platform === 'win32')
input = '"' + input + '"';
args.push(isUrl ? input : '-'); // stdin if HTML given directly
args.push(output || '-'); // stdout if no output file
if (process.platform === 'win32') {
args.unshift('"');
args.unshift('/C');
args.push('"');
if (logging) {
console.log('WKHTMLTOPDF args:\n');
console.dir(args);
console.log('\n');
}
var child = spawn('cmd', args, { windowsVerbatimArguments: true });
if (logging) logError(child);
} else {
// this nasty business prevents piping problems on linux
var child = spawn('/bin/sh', ['-c', args.join(' ') + ' | cat']);
if (logging) logError(child);
}
if (callback)
child.on('exit', callback);
if (!isUrl)
child.stdin.end(input);
// return stdout stream so we can pipe
return child.stdout;
}
function logError(child) {
child.stdout.setEncoding('utf8');
child.stdout.on('data', function(data) { console.log('(INFO) WKHTML INFO --------------------------- \n'); console.dir(data); });
child.stderr.setEncoding('utf8');
child.stderr.on('data', function(data) { console.log('(ERROR) WKHTML ERROR --------------------------- \n'); console.dir(data); });
}
wkhtmltopdf.command = 'wkhtmltopdf';
module.exports = wkhtmltopdf;