forked from ncb000gt/node-gs
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
112 lines (105 loc) · 3.4 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
var spawn = require('child_process').spawn;
function gs() {
return {
"options": [],
"_input": null,
"excPath": '',
"option": function (option) {
this.options.push(option);
return this;
},
"executablePath": function (path) {
this.excPath = path;
return this;
},
"batch": function () {
this.options.push('-dBATCH');
return this;
},
"diskfonts": function () {
this.options.push('-dDISKFONTS');
return this;
},
"nobind": function () {
this.options.push('-dNOBIND');
return this;
},
"nocache": function () {
this.options.push('-dNOCACHE');
return this;
},
"nodisplay": function () {
this.options.push('-dNODISPLAY');
return this;
},
"nopause": function () {
this.options.push('-dNOPAUSE');
return this;
},
"define": function (key, val) {
this.options.push('-d' + key + (val ? '=' + val : ''));
return this;
},
"device": function (dev) {
dev = dev || 'txtwrite';
this.options.push('-sDEVICE=' + dev);
return this;
},
"input": function (file) {
this._input = file;
return this;
},
"output": function (file) {
file = file || '-';
this.options.push('-sOutputFile=' + file);
if (file === '-') return this.q();
return this;
},
"q": function () {
this.options.push('-q');
return this;
},
"p": function () {
this.options.push('-p');
return this;
},
"papersize": function (size) {
this.options.push('-sPAPERSIZE=' + size);
return this;
},
"res": function (xres, yres) {
this.options.push('-r' + xres + (yres ? 'x' + yres : ''));
return this;
},
"safer": function () {
this.options.push('-dSAFER');
return this;
},
"exec": function (cb) {
var self = this;
if (!this._input) return cb.call(self, 'No input specified');
console.log('gs command: ' + this.options.concat([this._input]));
if (this.excPath) {
var proc = spawn(this.excPath, this.options.concat([this._input]));
} else {
var proc = spawn('gs', this.options.concat([this._input]));
}
proc.stdin.on('error', cb);
proc.stdout.on('error', cb);
var _data = [];
var totalBytes = 0;
proc.stdout.on('data', function (data) {
totalBytes += data.length;
_data.push(data);
});
proc.on('close', function () {
var buf = Buffer.concat(_data, totalBytes);
return cb.call(self, null, buf.toString());
});
process.on('exit', function () {
proc.kill();
});
}
};
}
module.exports = exports = gs;