forked from lsongdev/node-escpos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter.js
More file actions
467 lines (441 loc) · 12.7 KB
/
printer.js
File metadata and controls
467 lines (441 loc) · 12.7 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
'use strict';
const util = require('util');
const qr = require('qr-image');
const iconv = require('iconv-lite');
const getPixels = require('get-pixels');
const Buffer = require('mutable-buffer');
const EventEmitter = require('events');
const Image = require('./image');
const utils = require('./utils');
const _ = require('./commands');
/**
* [function ESC/POS Printer]
* @param {[Adapter]} adapter [eg: usb, network, or serialport]
* @return {[Printer]} printer [the escpos printer instance]
*/
function Printer(adapter){
if (!(this instanceof Printer)) {
return new Printer(adapter);
}
var self = this;
EventEmitter.call(this);
this.adapter = adapter;
this.buffer = new Buffer();
this.encoding = 'GB18030';
};
/**
* Printer extends EventEmitter
*/
util.inherits(Printer, EventEmitter);
Printer.prototype.init = function(){
this.buffer.write('\x1b\x40');
return this;
};
/**
* Fix bottom margin
* @param {[String]} size
* @return printer instance
*/
Printer.prototype.marginBottom = function(size){
this.buffer.write(_.MARGINS.BOTTOM);
this.buffer.writeUInt8(size);
return this;
};
/**
* Fix left margin
* @param {[String]} size
* @return printer instance
*/
Printer.prototype.marginLeft = function(size){
this.buffer.write(_.MARGINS.LEFT);
this.buffer.writeUInt8(size);
return this;
};
/**
* Fix right margin
* @param {[String]} size
* @return printer instance
*/
Printer.prototype.marginRight = function(size){
this.buffer.write(_.MARGINS.RIGHT);
this.buffer.writeUInt8(size);
return this;
};
/**
* Send data to hardware and flush buffer
* @param {Function} callback
* @return printer instance
*/
Printer.prototype.flush = function(callback){
var buf = this.buffer.flush();
this.adapter.write(buf, callback);
return this;
};
/**
* [function print]
* @param {[String]} content [description]
* @param {[String]} encoding [description]
* @return printer instance
*/
Printer.prototype.print = function(content){
this.buffer.write(content);
return this;
};
/**
* [function println]
* @param {[String]} content [description]
* @param {[String]} encoding [description]
* @return printer instance
*/
Printer.prototype.println = function(content){
return this.print([ content, _.EOL ].join(''));
};
/**
* [function Print alpha-numeric text]
* @param {[String]} content [description]
* @param {[String]} encoding [description]
* @return printer instance
*/
Printer.prototype.text = function(content, encoding){
return this.print(iconv.encode(content + _.EOL, encoding || this.encoding));
};
/**
* [function encode text]
* @param {[String]} encoding [description]
* @return printer instance
*/
Printer.prototype.encode = function(encoding) {
this.encoding = encoding;
return this;
}
/**
* [line feed]
* @param {[type]} lines [description]
* @return {[Printer]} printer [description]
*/
Printer.prototype.feed = function (n) {
this.buffer.write(new Array(n || 1).fill(_.EOL).join(''));
return this.flush();
};
/**
* [feed control sequences]
* @param {[type]} ctrl [description]
* @return printer instance
*/
Printer.prototype.control = function(ctrl){
this.buffer.write(_.FEED_CONTROL_SEQUENCES[
'CTL_' + ctrl.toUpperCase()
]);
return this;
};
/**
* [text align]
* @param {[type]} align [description]
* @return printer instance
*/
Printer.prototype.align = function(align){
this.buffer.write(_.TEXT_FORMAT[
'TXT_ALIGN_' + align.toUpperCase()
]);
return this;
};
/**
* [font family]
* @param {[type]} family [description]
* @return {[Printer]} printer [description]
*/
Printer.prototype.font = function(family){
this.buffer.write(_.TEXT_FORMAT[
'TXT_FONT_' + family.toUpperCase()
]);
return this;
};
/**
* [font style]
* @param {[type]} type [description]
* @return printer instance
*/
Printer.prototype.style = function(type){
switch(type.toUpperCase()){
case 'B':
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON);
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF);
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_OFF);
break;
case 'I':
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF);
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON);
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_OFF);
break;
case 'U':
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF);
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF);
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_ON);
break;
case 'U2':
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF);
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF);
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL2_ON);
break;
case 'BI':
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON);
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON);
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_OFF);
break;
case 'BIU':
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON);
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON);
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_ON);
break;
case 'BIU2':
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON);
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON);
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL2_ON);
break;
case 'BU':
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON);
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF);
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_ON);
break;
case 'BU2':
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON);
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF);
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL2_ON);
break;
case 'IU':
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF);
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON);
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_ON);
break;
case 'IU2':
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF);
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON);
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL2_ON);
break;
case 'NORMAL':
default:
this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF);
this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF);
this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_OFF);
break;
}
return this;
};
/**
* [font size]
* @param {[String]} width [description]
* @param {[String]} height [description]
* @return {[Printer]} printer [description]
*/
Printer.prototype.size = function(width, height) {
if (2 >= width && 2 >= height) {
this.buffer.write(_.TEXT_FORMAT.TXT_NORMAL);
if (2 == width && 2 == height) {
this.buffer.write(_.TEXT_FORMAT.TXT_4SQUARE);
} else if (1 == width && 2 == height) {
this.buffer.write(_.TEXT_FORMAT.TXT_2HEIGHT);
} else if (2 == width && 1 == height) {
this.buffer.write(_.TEXT_FORMAT.TXT_2WIDTH);
}
} else {
this.buffer.write(_.TEXT_FORMAT.TXT_CUSTOM_SIZE(width, height));
}
return this;
};
/**
* [set line spacing]
* @param {[type]} n [description]
* @return {[type]} [description]
*/
Printer.prototype.lineSpace = function(n) {
if (n === undefined || n === null) {
this.buffer.write(_.LINE_SPACING.LS_DEFAULT);
} else {
this.buffer.write(_.LINE_SPACING.LS_SET);
this.buffer.writeUInt8(n);
}
return this;
};
/**
* [hardware]
* @param {[type]} hw [description]
* @return printer instance
*/
Printer.prototype.hardware = function(hw){
this.buffer.write(_.HARDWARE[ 'HW_'+ hw ]);
return this.flush();
};
/**
* [barcode]
* @param {[type]} code [description]
* @param {[type]} type [description]
* @param {[type]} width [description]
* @param {[type]} height [description]
* @param {[type]} position [description]
* @param {[type]} font [description]
* @return printer instance
*/
Printer.prototype.barcode = function(code, type, width, height, position, font){
type = type || 'EAN13'; // default type is EAN13, may a good choice ?
var convertCode = String(code), parityBit = '';
if(typeof type === 'undefined' || type === null){
throw new TypeError('barcode type is required');
}
if (type === 'EAN13' && convertCode.length != 12) {
throw new Error('EAN13 Barcode type requires code length 12');
}
if (type === 'EAN8' && convertCode.length != 7) {
throw new Error('EAN8 Barcode type requires code length 7');
}
if(width >= 2 || width <= 6){
this.buffer.write(_.BARCODE_FORMAT.BARCODE_WIDTH[width]);
}else{
this.buffer.write(_.BARCODE_FORMAT.BARCODE_WIDTH_DEFAULT);
}
if(height >=1 || height <= 255){
this.buffer.write(_.BARCODE_FORMAT.BARCODE_HEIGHT(height));
}else{
this.buffer.write(_.BARCODE_FORMAT.BARCODE_HEIGHT_DEFAULT);
}
this.buffer.write(_.BARCODE_FORMAT[
'BARCODE_FONT_' + (font || 'A').toUpperCase()
]);
this.buffer.write(_.BARCODE_FORMAT[
'BARCODE_TXT_' + (position || 'BLW').toUpperCase()
]);
this.buffer.write(_.BARCODE_FORMAT[
'BARCODE_' + ((type || 'EAN13').replace('-', '_').toUpperCase())
]);
if (type === 'EAN13' || type === 'EAN8') {
parityBit = utils.getParityBit(code);
}
this.buffer.write(code + parityBit);
return this;
};
/**
* [print qrcode]
* @param {[type]} code [description]
* @param {[type]} version [description]
* @param {[type]} level [description]
* @param {[type]} size [description]
* @return {[type]} [description]
*/
Printer.prototype.qrcode = function(code, version, level, size){
this.buffer.write(_.CODE2D_FORMAT.TYPE_QR);
this.buffer.write(_.CODE2D_FORMAT.CODE2D);
this.buffer.writeUInt8(version || 3);
this.buffer.write(_.CODE2D_FORMAT[
'QR_LEVEL_' + (level || 'L').toUpperCase()
]);
this.buffer.writeUInt8(size || 6);
this.buffer.writeUInt16LE(code.length);
this.buffer.write(code);
return this;
};
/**
* [print qrcode image]
* @param {[type]} content [description]
* @param {[type]} options [description]
* @param {Function} callback [description]
* @return {[type]} [description]
*/
Printer.prototype.qrimage = function(content, options, callback){
var self = this;
if(typeof options == 'function'){
callback = options;
options = null;
}
options = options || { type: 'png', mode: 'dhdw' };
var buffer = qr.imageSync(content, options);
var type = [ 'image', options.type ].join('/');
getPixels(buffer, type, function (err, pixels) {
if(err) return callback && callback(err);
self.raster(new Image(pixels), options.mode);
callback && callback.call(self, null, self);
});
return this;
};
/**
* [image description]
* @param {[type]} image [description]
* @param {[type]} density [description]
* @return {[type]} [description]
*/
Printer.prototype.image = function(image, density){
if(!(image instanceof Image))
throw new TypeError('Only escpos.Image supported');
density = density || 'd24';
var n = !!~[ 'd8', 's8' ].indexOf(density) ? 1 : 3;
var header = _.BITMAP_FORMAT['BITMAP_' + density.toUpperCase()];
var bitmap = image.toBitmap(n * 8);
var self = this;
this.lineSpace(0); // set line spacing to 0
bitmap.data.forEach(function (line) {
self.buffer.write(header);
self.buffer.writeUInt16LE(line.length / n);
self.buffer.write(line);
self.buffer.write(_.EOL);
});
// restore line spacing to default
return this.lineSpace();
};
/**
* [raster description]
* @param {[type]} image [description]
* @param {[type]} mode [description]
* @return {[type]} [description]
*/
Printer.prototype.raster = function (image, mode) {
if(!(image instanceof Image))
throw new TypeError('Only escpos.Image supported');
mode = mode || 'normal';
if (mode === 'dhdw' ||
mode === 'dwh' ||
mode === 'dhw') mode = 'dwdh';
var raster = image.toRaster();
var header = _.GSV0_FORMAT['GSV0_' + mode.toUpperCase()];
this.buffer.write(header);
this.buffer.writeUInt16LE(raster.width);
this.buffer.writeUInt16LE(raster.height);
this.buffer.write(raster.data);
return this;
};
/**
* [function Cut paper]
* @param {[type]} part [description]
* @return printer instance
*/
Printer.prototype.cut = function(part, feed){
this.feed(feed || 3);
this.buffer.write(_.PAPER[
part ? 'PAPER_PART_CUT' : 'PAPER_FULL_CUT'
]);
return this.flush();
};
/**
* [function Send pulse to kick the cash drawer]
* @param {[type]} pin [description]
* @return printer instance
*/
Printer.prototype.cashdraw = function(pin){
this.buffer.write(_.CASH_DRAWER[
'CD_KICK_' + (pin || 2)
]);
return this.flush();
};
/**
* [close description]
* @param {Function} callback [description]
* @return {[type]} [description]
*/
Printer.prototype.close = function(callback){
var self = this;
return this.flush(function(){
self.adapter.close(callback);
});
};
/**
* [exports description]
* @type {[type]}
*/
module.exports = Printer;