forked from devongovett/node-wkhtmltopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (128 loc) · 4.16 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var spawn = require('child_process').spawn;
var slang = require('slang');
var _ = require('underscore');
function quote(val) {
// escape and quote the value if it is a string and this isn't windows
if (typeof val === 'string' && process.platform !== 'win32')
val = '"' + val.replace(/(["\\$`])/g, '\\$1') + '"';
return val;
}
function wkhtmltopdf(input, options, callback) {
if (!options) {
options = {};
} else if (typeof options == 'function') {
callback = options;
options = {};
}
var output = options.output;
delete options.output;
// make sure the special keys are last
var args = [wkhtmltopdf.command, '--quiet'];
if (options.rawArgs && options.rawArgs.length) {
options.rawArgs.forEach(function(arg) {
args.push(quote(arg));
});
}
else {
var extraKeys = [];
var keys = Object.keys(options).filter(function(key) {
if (key === 'toc' || key === 'cover' || key === 'page') {
extraKeys.push(key);
return false;
}
return true;
}).concat(extraKeys);
// make sure toc specific args appear after toc arg
if(_.find(keys, function(key){return key === 'toc'})) {
var tocArgs = ['disableDottedLines', 'tocHeaderText', 'tocLevelIndentation', 'disableTocLinks', 'tocTextSizeShrink', 'xslStyleSheet'];
var myTocArgs = [];
keys = keys.filter(function(key){
if(_.find(tocArgs, function(tkey){ return tkey === key })) {
myTocArgs.push(key);
return false;
}
return true;
});
var spliceArgs = [keys.indexOf('toc')+1, 0].concat(myTocArgs);
Array.prototype.splice.apply(keys, spliceArgs);
}
keys.forEach(function(key) {
var val = options[key];
if (key === 'ignore') { // skip adding the ignore key
return false;
}
if (key !== 'toc' && key !== 'cover' && key !== 'page') {
key = key.length === 1 ? '-' + key : '--' + slang.dasherize(key);
}
if (val !== false) {
args.push(key);
}
if (Array.isArray(val)) {
val.forEach(function(valPart) {
args.push(quote(valPart));
});
} else if (typeof val !== 'boolean') {
args.push(quote(val));
}
});
var isUrl = /^(https?|file):\/\//.test(input);
args.push(isUrl ? quote(input) : '-'); // stdin if HTML given directly
args.push(output ? quote(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(' ') + ' | cat']);
}
// call the callback with null error when the process exits successfully
if (callback) {
child.on('exit', function() { callback(null); });
}
// setup error handling
var stream = child.stdout;
function handleError(err) {
// check ignore warnings array before killing child
if (options.ignore && options.ignore instanceof Array) {
var ignoreError = false;
options.ignore.forEach(function(opt) {
if (typeof opt === 'string' && opt === err.message) {
ignoreError = true;
}
if (opt instanceof RegExp && err.message.match(opt)) {
ignoreError = true;
}
});
if (ignoreError) {
return true;
}
}
child.removeAllListeners('exit');
child.kill();
// call the callback if there is one
if (callback) {
callback(err);
}
// if not, or there are listeners for errors, emit the error event
if (!callback || stream.listeners('error').length > 0) {
stream.emit('error', err);
}
}
child.once('error', handleError);
child.stderr.once('data', function(err) {
handleError(new Error((err || '').toString().trim()));
});
// write input to stdin if it isn't a url
if (!isUrl) {
child.stdin.end(input);
}
// return stdout stream so we can pipe
if (callback) {
return callback(null, stream);
} else {
return stream;
}
}
wkhtmltopdf.command = 'wkhtmltopdf';
module.exports = wkhtmltopdf;
module.exports.render = wkhtmltopdf;