Skip to content

Commit

Permalink
Fix piping on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Jun 1, 2013
1 parent bf19d5e commit 5a765a3
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,29 @@ function wkhtmltopdf(input, options) {
var output = options.output;
delete options.output;

var args = ['--quiet'];
var args = [wkhtmltopdf.command, '--quiet'];
for (var key in options) {
var val = options[key];
key = '--' + slang.dasherize(key);

if (val !== false)
args.push(key);

if (typeof val !== 'boolean')
args.push('"' + val + '"');
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

var child = spawn(wkhtmltopdf.command, args);
// this nasty business prevents piping problems on linux
var child = spawn('/bin/sh', ['-c', args.join(' ') + ' | cat']);
if (!isUrl)
child.stdin.end(input);

Expand Down

0 comments on commit 5a765a3

Please sign in to comment.