This repository has been archived by the owner on Apr 26, 2019. It is now read-only.
forked from armetiz/node-printer-lp
-
Notifications
You must be signed in to change notification settings - Fork 16
/
printer.js
381 lines (342 loc) · 11.3 KB
/
printer.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
var Job = require ('./job');
var spawn = require('child_process').spawn;
var spawnSync = require('child_process').spawnSync;
var _ = require ('underscore');
var utils = require('util');
var events = require('events');
utils.inherits(Printer, events.EventEmitter);
/**
* Describes the parameter options accepted by lp
*
* options array specifies the valid option names for the option
* if expects is blank '' no option value is required for the option
*
* This was written according to lp man page
* http://unixhelp.ed.ac.uk/CGI/man-cgi?lp
*/
var generalOptions = {
'E': {
'options': ['E', 'encryption'],
'description': 'Forces encryption when connecting to the server',
'expects': '',
'default': false
},
'U': {
'options': ['U', 'Username', 'username'],
'description': 'Specifies the username to use when connecting to the server',
'expects': 'string'
},
'c': {
'options': ['c', 'backwardsCompatibility'],
'description': 'This option is provided for backwards-compatibility only. On systems that support it, this option forces the print file to be copied to the spool directory before printing. In CUPS, print files are always sent to the scheduler via IPP which has the same effect.',
'expects': '',
'default': false
},
'd': {
'options': ['d', 'destination'],
'description': 'Prints files to the named printer',
'expects': 'string'
},
'h': {
'options': ['h', 'hostname'],
'description': 'Chooses an alternate server',
'expects': 'string'
},
'i': {
'options': ['i', 'job-id'],
'description': 'Specifies an existing job to modify',
'expects': 'number'
},
'm': {
'options': ['m'],
'description': 'Sends an email when the job is completed',
'expects': ''
},
'n': {
'options': ['n', 'copies', 'numCopies'],
'description': 'Sets the number of copies to print from 1 to 100',
'expects': 'number',
'default': 1
},
'o': {
'options': ['o'],
'description': '"name=value [name=value ...]" Sets one or more job options',
'expects': 'string',
'default': ''
},
'q': {
'options': ['q', 'priority'],
'description': 'Sets the job priority from 1 (lowest) to 100 (highest). The default priority is 50',
'expects': 'number',
'default': 1
},
's': {
'options': ['s'],
'description': 'Do not report the resulting job IDs (silent mode.)',
'expects': ''
},
't': {
'options': ['t', 'name'],
'description': 'Sets the job name',
'expects': 'string'
},
'H': {
'options': ['H', 'when'],
'description': 'Specifies when the job should be printed. A value of immediate will print the file immediately, a value of hold will hold the job indefinitely, and a time value (HH:MM) will hold the job until the specified time. Use a value of resume with the -i option to resume a held job. Use a value of restart with the -i option to restart a completed job.',
'default': 'immediate',
'expects': 'string'
},
'P': {
'options': ['P', 'page-list'],
'description': 'page-list Specifies which pages to print in the document. The list can contain a list of numbers and ranges (#-#) separated by commas (e.g. 1,3-5,16).',
'expects': 'string'
}
};
/**
* Describes the parameter options accepted by lp -o option
*
* options array specifies the valid option names for the option
* if expects is blank '' no option value is required for the option
*
* This was written according to lp man page
* http://unixhelp.ed.ac.uk/CGI/man-cgi?lp
*/
var oOptions = {
'media': {
'options': ['media'],
'description': 'Sets the page size to size. Most printers support at least the size names "a4", "letter", and "legal".',
'expects': 'string',
'default': 'a4'
},
'landscape': {
'options': ['landscape'],
'description': 'landscape',
'expects': ''
},
'orientation-requested': {
'options': ['orientation-requested'],
'description': '',
'expects': 'number'
},
'sides': {
'options': ['sides'],
'description': 'Prints on one or two sides of the paper. The value "two-sided-long-edge" is normally used when printing portrait (unrotated) pages, while "two-sided-short-edge" is used for landscape pages.',
'expects': 'string'
},
'fitplot': {
'options': ['fitplot'],
'description': 'Scales the print file to fit on the page',
'expects': '',
'default': false
},
'scaling': {
'options': ['scaling'],
'description': 'Scales image files to use up to number percent of the page. Values greater than 100 cause the image file to be printed across multiple pages',
'expects': 'number'
},
'cpi': {
'options': ['cpi'],
'description': 'Sets the number of characters per inch to use when printing a text file. The default is 10',
'expects': 'number'
},
'lpi': {
'options': ['lpi'],
'description': 'Sets the number of lines per inch to use when printing a text file. The default is 6',
'expects': 'number'
},
'page-bottom': {
'options': ['page-bottom'],
'description': 'Sets the page margins when printing text files. The values are in points - there are 72 points to the inch',
'expects': 'number'
},
'page-left': {
'options': ['page-left'],
'description': 'Sets the page margins when printing text files. The values are in points - there are 72 points to the inch',
'expects': 'number'
},
'page-right': {
'options': ['page-right'],
'description': 'Sets the page margins when printing text files. The values are in points - there are 72 points to the inch',
'expects': 'number'
},
'page-top': {
'options': ['page-top'],
'description': 'Sets the page margins when printing text files. The values are in points - there are 72 points to the inch',
'expects': 'number'
}
};
var optionsFactory = function (options) {
var selGeneralOptions = {};
_.each(generalOptions, function(generalOption, generalOptionId) { // for each of lp available options
_.each(generalOption.options, function(option) { // for each option possible name
if(typeof options[option] !== 'undefined') { // check of the method options contains the value
if(generalOption.expects === '') { // when expects is empty, ommit the options defined value, just add
if(options[option]) {
selGeneralOptions[generalOptionId] = true;
}
} else {
selGeneralOptions[generalOptionId] = options[option];
}
}
});
if(typeof generalOption.default != 'undefined') { // if method options does not contains the value, use default if available
if(typeof selGeneralOptions[generalOptionId] == 'undefined') {
selGeneralOptions[generalOptionId] = generalOption.default;
}
}
});
var selOOptions = {};
_.each(oOptions, function(oOption, oOptionId) { // for each of lp available options
_.each(oOption.options, function(option) { // for each option possible name
if(typeof options[option] !== 'undefined') { // check of the method options contains the value
if(oOption.expects === '') { // when expects is empty, ommit the options defined value, just add
if(options[option]) {
selOOptions[oOptionId] = true;
}
} else {
selOOptions[oOptionId] = options[option];
}
} else if(typeof oOption.default != 'undefined') { // if method options does not contains the value, use default if available
selOOptions[oOptionId] = oOption.default;
}
});
if(typeof oOption.default != 'undefined') { // if method options does not contains the value, use default if available
if(typeof selOOptions[oOptionId] == 'undefined') {
selOOptions[oOptionId] = oOption.default;
}
}
});
if(!_.isEmpty(selOOptions)) {
var selOoptionsString = '';
_.each(selOOptions, function(oOption, oOptionId) {
if(oOptions[oOptionId].expects === '') {
if(oOption) {
selOoptionsString = selOoptionsString + ' ' + oOptionId;
}
} else {
selOoptionsString = selOoptionsString + ' ' + oOptionId + '=' + oOption;
}
});
if(typeof selGeneralOptions.o == 'string') {
selGeneralOptions.o = selGeneralOptions.o + selOoptionsString;
} else {
selGeneralOptions.o = selOoptionsString;
}
}
return selGeneralOptions;
};
var argsFactory = function(options) {
var args = [];
_.each(options, function(optionValue, optionKey) {
if(generalOptions[optionKey].expects === '') {
if(optionValue) {
args.push('-' + optionKey);
}
} else {
args.push('-' + optionKey + optionValue);
}
});
return args;
};
var buildArgs = function(options) {
if (!options) return [];
options = optionsFactory(options);
return argsFactory(options);
};
var parseStdout = function(data) {
if (!Buffer.isBuffer(data)) return [];
return data.toString()
.replace(/\n$/, '')
.split('\n');
};
function Printer(name) {
var self = this;
if (!Printer.match(name)) {
console.error(
name + ' printer does not exist ; installed printers are ' + Printer.list()
);
throw new Error('Printer ' + name + ' does not exist on your system.');
}
self.name = name;
self.jobs = [];
self.watch();
}
Printer.list = function() {
return parseStdout(spawnSync('lpstat', ['-e']).stdout);
};
Printer.match = function(name) {
return Boolean(Printer.list().filter(function(printer) {
return name === printer;
}).length);
};
Printer.prototype.watch = function() {
var self = this;
var args = ['-P', this.name];
var lpq = spawn('lpq', args);
lpq.stdout.on('data', function(data) {
var parsedData = parseStdout(data)
.slice(2)
.map(function(line) {
line = line.split(/[ ]{2,}/);
return {
rank: (line[0] === 'active' ? line[0] : parseInt(line[0].slice(0, -2))),
owner: line[1],
identifier: parseInt(line[2]),
files: line[3],
totalSize: line[4]
};
});
self.jobs.map(function(job) {
var status = parsedData.filter(function(status) {
if (status.identifier === job.identifier) return status;
})[0];
if (status) {
job.update(status);
} else {
job.unqueue();
}
});
});
lpq.on('exit', function() {
self.watch();
});
};
Printer.prototype.findJob = function(jobId) {
return this.jobs.filter(function(job) {
if (job.identifier === jobId) return job;
})[0];
};
Printer.prototype.printBuffer = function(data, options) {
var self = this;
var args = buildArgs(options);
args.push('-d', self.name);
var lp = spawn('lp', args);
lp.stdin.write(data);
lp.stdin.end();
var job = new Job(lp);
job.on('sent', function() {
self.jobs.push(job);
});
job.on('completed', function() {
self.jobs.splice(self.jobs.indexOf(job), 1);
});
return job;
};
Printer.prototype.printText = Printer.prototype.printBuffer;
Printer.prototype.printFile = function(filePath, options) {
var self = this;
var args = buildArgs(options);
args.push('-d', self.name);
args.push('--');
args.push(filePath);
var lp = spawn('lp', args);
var job = new Job(lp);
job.on('sent', function() {
self.jobs.push(job);
});
job.on('completed', function() {
self.jobs.splice(self.jobs.indexOf(job), 1);
});
return job;
};
module.exports = Printer;