UNPKG

3.64 kBJavaScriptView Raw
1/*jslint es5:true, nomen:true, node:true*/
2(function () {
3 var hasOwn = Object.prototype.hasOwnProperty,
4 Undefined = function (){},
5 spawn = require('child_process').spawn,
6 StringStripped = function(value){
7 return(value.replace('$','').replace('&','').replace(';',''));
8 },
9 buildOpts = function (options) {
10 var opts = [];
11 Object.keys(options).forEach(function (key) {
12 if (hasOwn.call(validOptions, key)) {
13 opts.push('--' + key);
14 if (validOptions[key] && validOptions[key] !== Undefined) {
15 opts.push(validOptions[key](options[key]));
16 }
17 }
18 });
19 return (opts);
20 },
21 validOptions = {
22 'background': Undefined,
23 'collate': Undefined,
24 'copies': Number,
25 'default-header': Undefined,
26 'disable-external-links': Undefined,
27 'disable-forms': Undefined,
28 'disable-internal-links': Undefined,
29 'disable-javascript': Undefined,
30 'disable-smart-shrinking': Undefined,
31 'disable-toc-back-links': Undefined,
32 'enable-external-links': Undefined,
33 'enable-forms': Undefined,
34 'enable-internal-links': Undefined,
35 'enable-javascript': Undefined,
36 'enable-smart-shrinking': Undefined,
37 'enable-toc-back-links': Undefined,
38 'encoding': StringStripped,
39 'exclude-from-outline': Undefined,
40 'grayscale': Undefined,
41 'dpi': Number,
42 'image-quality': Number,
43 'images': Undefined,
44 'include-in-outline': Undefined,
45 'javascript-delay': Number,
46 'load-error-handling': StringStripped,
47 'lowquality': Undefined,
48 'margin-bottom': StringStripped,
49 'margin-left': StringStripped,
50 'margin-right': StringStripped,
51 'margin-top': StringStripped,
52 'minimum-font-size': Number,
53 'no-background': Undefined,
54 'no-collate': Undefined,
55 'no-images': Undefined,
56 'no-outline': Undefined,
57 'no-pdf-compression': Undefined,
58 'no-print-media-type': Undefined,
59 'no-stop-slow-scripts': Undefined,
60 'orientation': StringStripped,
61 'outline': Undefined,
62 'outline-depth': Number,
63 'output-format': StringStripped,
64 'page-height': StringStripped,
65 'page-offset': Number,
66 'page-size': StringStripped,
67 'page-width': StringStripped,
68 'password': StringStripped,
69 'print-media-type': Undefined,
70 'proxy': StringStripped,
71 'stop-slow-scripts': Undefined,
72 'title': StringStripped,
73 'user-style-sheet': StringStripped,
74 'username': StringStripped,
75 'zoom': Number
76 };
77 module.exports.pipePdf = function (options, stream) {
78 var child = spawn('/bin/sh', ['-c', 'wkhtmltopdf - - ' + buildOpts(options).join(' ') + ' | cat']);
79 child.stdin.end(options.html);
80 child.stdout.pipe(stream);
81 };
82 module.exports.getPdfStream = function (options, callback) {
83 var child = spawn('/bin/sh', ['-c', 'wkhtmltopdf - - ' + buildOpts(options).join(' ') + ' | cat']);
84 child.stdin.end(options.html);
85 callback(child.stdout);
86 };
87}());
\No newline at end of file