forked from making3/node-wkhtmltopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (38 loc) · 1.28 KB
/
index.js
File metadata and controls
50 lines (38 loc) · 1.28 KB
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
var spawn = require('child_process').spawn;
var slang = require('slang');
function wkhtmltopdf(input, options, callback) {
if (!options)
options = {};
else if (options === typeof 'function') {
callback = options;
options = {};
}
var output = options.output;
delete options.output;
var args = [wkhtmltopdf.command, '--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);
args.push(isUrl ? input : '-'); // stdin if HTML given directly
args.push(output || '-'); // stdout if no output file
// this nasty business prevents piping problems on linux
var child = spawn('/bin/sh', ['-c', args.join(' ') + ' | cat']);
if (callback)
child.on('exit', callback);
if (!isUrl)
child.stdin.end(input);
// return stdout stream so we can pipe
return child.stdout;
}
wkhtmltopdf.command = 'wkhtmltopdf';
module.exports = wkhtmltopdf;