ke_junjie
2025-06-04 84620534eb627e95811b971a4b552b6a177829bf
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
import gbk from './gbk.js';
 
const commands = {
    LF: [0x0a],
    FS: [0x1c],
    FF: [0x0c],
    GS: [0x1d],
    DLE: [0x10],
    EOT: [0x04],
    NUL: [0x00],
    ESC: [0x1b],
    EOL: '\n',
    FEED_CONTROL_SEQUENCES:{
        CTL_LF: [0x0a],   // Print and line feed
        CTL_GLF: [0x4a, 0x00],   // Print and feed paper (without spaces between lines)
        CTL_FF: [0x0c],   // Form feed
        CTL_CR: [0x0d],   // Carriage return
        CTL_HT: [0x09],   // Horizontal tab
        CTL_VT: [0x0b]   // Vertical tab
    },
    CHARACTER_SPACING:{
        CS_DEFAULT: [0x1b, 0x20, 0x00],
        CS_SET: [0x1b, 0x20]
    },
    LINE_SPACING:{
        LS_DEFAULT: [0x1b, 0x32],
        LS_SET: [0x1b, 0x33]
    },
    HARDWARE:{
        HW_INIT: [0x1b, 0x40], // Clear data in buffer and reset modes
        HW_SELECT: [0x1b, 0x3d, 0x01], // Printer select
        HW_RESET: [0x1b, 0x3f, 0x0a, 0x00] // Reset printer hardware
    },
    CASH_DRAWER:{
        CD_KICK_2: [0x1b, 0x70, 0x00], // Sends a pulse to pin 2 []
        CD_KICK_5: [0x1b, 0x70, 0x01] // Sends a pulse to pin 5 []
    },
    MARGINS:{
        BOTTOM: [0x1b, 0x4f], // Fix bottom size
        LEFT: [0x1b, 0x6c], // Fix left size
        RIGHT: [0x1b, 0x51] // Fix right size
    },
    PAPER:{
        PAPER_FULL_CUT: [0x1d, 0x56, 0x00], // Full cut paper
        PAPER_PART_CUT: [0x1d, 0x56, 0x01], // Partial cut paper
        PAPER_CUT_A: [0x1d, 0x56, 0x41], // Partial cut paper
        PAPER_CUT_B: [0x1d, 0x56, 0x42] // Partial cut paper
    },
    TEXT_FORMAT:{
        TXT_NORMAL: [0x1b, 0x21, 0x00], // Normal text
        TXT_2HEIGHT: [0x1b, 0x21, 0x10], // Double height text
        TXT_2WIDTH: [0x1b, 0x21, 0x20], // Double width text
        TXT_4SQUARE: [0x1b, 0x21, 0x30], // Double width & height text
        
        TXT_UNDERL_OFF: [0x1b, 0x2d, 0x00], // Underline font OFF
        TXT_UNDERL_ON: [0x1b, 0x2d, 0x01], // Underline font 1-dot ON
        TXT_UNDERL2_ON: [0x1b, 0x2d, 0x02], // Underline font 2-dot ON
        TXT_BOLD_OFF: [0x1b, 0x45, 0x00], // Bold font OFF
        TXT_BOLD_ON: [0x1b, 0x45, 0x01], // Bold font ON
        TXT_ITALIC_OFF: [0x1b, 0x35], // Italic font ON
        TXT_ITALIC_ON: [0x1b, 0x34], // Italic font ON
        
        TXT_FONT_A: [0x1b, 0x4d, 0x00], // Font type A
        TXT_FONT_B: [0x1b, 0x4d, 0x01], // Font type B
        TXT_FONT_C: [0x1b, 0x4d, 0x02], // Font type C
        
        TXT_ALIGN_LT: [0x1b, 0x61, 0x00], // Left justification
        TXT_ALIGN_CT: [0x1b, 0x61, 0x01], // Centering
        TXT_ALIGN_RT: [0x1b, 0x61, 0x02] // Right justification
    },
    BARCODE_FORMAT:{
        BARCODE_TXT_OFF: [0x1d, 0x48, 0x00], // HRI barcode chars OFF
        BARCODE_TXT_ABV: [0x1d, 0x48, 0x01], // HRI barcode chars above
        BARCODE_TXT_BLW: [0x1d, 0x48, 0x02], // HRI barcode chars below
        BARCODE_TXT_BTH: [0x1d, 0x48, 0x03], // HRI barcode chars both above and below
        
        BARCODE_FONT_A: [0x1d, 0x66, 0x00], // Font type A for HRI barcode chars
        BARCODE_FONT_B: [0x1d, 0x66, 0x01], // Font type B for HRI barcode chars
        
        BARCODE_HEIGHT: function (height) { // Barcode Height [1-255]
          return [0x1d, 0x68, height];
        },
        BARCODE_WIDTH: function (width) {   // Barcode Width  [2-6]
          return [0x1d, 0x77, width];
        },
        BARCODE_HEIGHT_DEFAULT: [0x1d, 0x68, 0x64], // Barcode height default:100
        BARCODE_WIDTH_DEFAULT: [0x1d, 0x77, 0x01], // Barcode width default:1
        
        BARCODE_UPC_A: [0x1d, 0x6b, 0x00], // Barcode type UPC-A
        BARCODE_UPC_E: [0x1d, 0x6b, 0x01], // Barcode type UPC-E
        BARCODE_EAN13: [0x1d, 0x6b, 0x02], // Barcode type EAN13
        BARCODE_EAN8: [0x1d, 0x6b, 0x03], // Barcode type EAN8
        BARCODE_CODE39: [0x1d, 0x6b, 0x04], // Barcode type CODE39
        BARCODE_ITF: [0x1d, 0x6b, 0x05], // Barcode type ITF
        BARCODE_NW7: [0x1d, 0x6b, 0x06], // Barcode type NW7
        BARCODE_CODE93: [0x1d, 0x6b, 0x48], // Barcode type CODE93
        BARCODE_CODE128: [0x1d, 0x6b, 0x49] // Barcode type CODE128
    },
    IMAGE_FORMAT:{
        S_RASTER_N: [0x1d, 0x76, 0x30, 0x00], // Set raster image normal size
        S_RASTER_2W: [0x1d, 0x76, 0x30, 0x01], // Set raster image double width
        S_RASTER_2H: [0x1d, 0x76, 0x30, 0x02], // Set raster image double height
        S_RASTER_Q: [0x1d, 0x76, 0x30, 0x03] // Set raster image quadruple
    },
    BITMAP_FORMAT:{
        BITMAP_S8: [0x1b, 0x2a, 0x00],
        BITMAP_D8: [0x1b, 0x2a, 0x01],
        BITMAP_S24: [0x1b, 0x2a, 0x20],
        BITMAP_D24: [0x1b, 0x2a, 0x21]
    },
    GSV0_FORMAT:{
        GSV0_NORMAL: [0x1d, 0x76, 0x30, 0x00],
        GSV0_DW: [0x1d, 0x76, 0x30, 0x01],
        GSV0_DH: [0x1d, 0x76, 0x30, 0x02],
        GSV0_DWDH: [0x1d, 0x76, 0x30, 0x03]
    },
    BEEP:[0x1b, 0x42],
    COLOR:{
        0: [0x1b, 0x72, 0x00], // black
        1: [0x1b, 0x72, 0x01] // red
    }
}
 
const printerJobs = function() {
    this._queue = Array.from(commands.HARDWARE.HW_INIT);
    this._enqueue = function(cmd) {
        this._queue.push.apply(this._queue, cmd);
    }
};
 
/**
 * 增加打印内容
 * @param  {string} content  文字内容
 */
printerJobs.prototype.text = function(content) {
    if (content) {
        let uint8Array = gbk.encode(content);
        let encoded = Array.from(uint8Array);
        this._enqueue(encoded);
    }
    return this;
};
 
/**
 * 打印文字
 * @param  {string} content  文字内容
 */
printerJobs.prototype.print = function(content) {
    this.text(content);
    this._enqueue(commands.LF);
    return this;
};
 
 
printerJobs.prototype.printQrcode = function(content) {
 
    if (content) {
        const cmds = [].concat([27, 97, 1], [29, 118, 48, 0, 30, 0, 240, 0], content, [27, 74, 3], [27, 64]);
        this._enqueue(cmds);    
        this._enqueue(commands.LF);
    }
 
    return this;
};
 
 
 
/**
 * 打印文字并换行
 * @param  {string}  content  文字内容
 */
printerJobs.prototype.println = function(content = '') {
    return this.print(content + commands.EOL);
};
 
/**
 * 设置对齐方式
 * @param {string} align 对齐方式 LT/CT/RT
 */
printerJobs.prototype.setAlign = function(align) {
    this._enqueue(commands.TEXT_FORMAT['TXT_ALIGN_' + align.toUpperCase()]);
    return this;
};
 
/**
 * 设置字体
 * @param  {string} family A/B/C
 */
printerJobs.prototype.setFont = function(family) {
    this._enqueue(commands.TEXT_FORMAT['TXT_FONT_' + family.toUpperCase()]);
    return this;
};
 
/**
 * 设定字体尺寸
 * @param  {number} width 字体宽度 1~2
 * @param  {number} height 字体高度 1~2
 */
printerJobs.prototype.setSize = function(width, height) {
    if (2 >= width && 2 >= height) {
        this._enqueue(commands.TEXT_FORMAT.TXT_NORMAL);
        if (2 === width && 2 === height) {
            this._enqueue(commands.TEXT_FORMAT.TXT_4SQUARE);
        } else if (1 === width && 2 === height) {
            this._enqueue(commands.TEXT_FORMAT.TXT_2HEIGHT);
        } else if (2 === width && 1 === height) {
            this._enqueue(commands.TEXT_FORMAT.TXT_2WIDTH);
        }
    }
    return this;
};
 
/**
 * 设定字体是否加粗
 * @param  {boolean} bold
 */
printerJobs.prototype.setBold = function(bold) {
    if (typeof bold !== 'boolean') {
        bold = true;
    }
    this._enqueue(bold ? commands.TEXT_FORMAT.TXT_BOLD_ON : commands.TEXT_FORMAT.TXT_BOLD_OFF);
    return this;
};
 
/**
 * 设定是否开启下划线
 * @param  {boolean} underline
 */
printerJobs.prototype.setUnderline = function(underline) {
    if (typeof underline !== 'boolean') {
        underline = true;
    }
    this._enqueue(underline ? commands.TEXT_FORMAT.TXT_UNDERL_ON : commands.TEXT_FORMAT.TXT_UNDERL_OFF);
    return this;
};
 
/**
 * 设置行间距为 n 点行,默认值行间距是 30 点
 * @param {number} n 0≤n≤255
 */
printerJobs.prototype.setLineSpacing = function(n) {
    if (n === undefined || n === null) {
        this._enqueue(commands.LINE_SPACING.LS_DEFAULT);
    } else {
        this._enqueue(commands.LINE_SPACING.LS_SET);
        this._enqueue([n]);
    }
    return this;
};
 
/**
 * 打印空行
 * @param {number} n
 */
printerJobs.prototype.lineFeed = function(n = 1) {
    return this.print(new Array(n).fill(commands.EOL).join(''));
};
 
 
/**
 * 设置放大倍数
 * @param x 0-7 字符宽放大倍数
 * @param y 0-7 字符高放大倍数
 */
printerJobs.prototype.setCharacterMultiple = function ( x,  y) {
  this._enqueue([0x1d, 0x21]);
  if (0 <= x && x <= 7 && 0 <= y && y <= 7) {
    var cmd = x * 16 + y;
    this._enqueue([cmd]);
  }
  return this;
};
 
 /**
  *  * 打印二维码
  * @param content 条码数据
  * @param barcodeType 条码类型:
  * 0:PDF417, 1:DATAMATRIX , 2:QRCODE。
  * @param param1,param2,param3  条码参数:
  * 三个参数表示不同的意思:
  * 1. PDF417
  * param1:表示每行字符数,1<=n<=30。
  * param2:表示纠错等级,0<=n<=8。
  * param3:表示纵向放大倍数。
  * 2. DATAMATRIX
  * param1:表示图形高,0<=n<=144(0:自动选择)。
  * param2:表示图形宽,8<=n<=144(param1 为 0时,无效)。
  * param3:表示纵向放大倍数。
  * 3. QRCODE
  * param1:表示图形版本号,1<=n<=30(0:自动选择)。
  * param2:表示纠错等级,n = 76,77,81,72(L:7%,M:15%,Q:25%,H:30%)。
  * param3:表示纵向放大倍数。
  *
  */
printerJobs.prototype.printQrcode = function (content, barcodeType, param1, param2, param3) {
  var str = content;
  var length = str.replace(/[^\u0000-\u00ff]/g, "aa").length;
  this._enqueue([0x1d,0x5a]);
  this._enqueue([barcodeType]);
  this._enqueue([0x1b, 0x5a]);
  this._enqueue([param1]);
  this._enqueue([param2]);
  this._enqueue([param3]);
  this._enqueue([length%256]);
  this._enqueue([length/256]);
  this.print(content);
  return this;
};
 
printerJobs.prototype.queryStatus = function () {
  this._enqueue([0x10, 0x04,0x02]);
  return this;
};
 
/**
 * 清空任务
 */
printerJobs.prototype.clear = function () {
  this._queue = Array.from(commands.HARDWARE.HW_INIT);
  return this;
};
 
/**
 * 返回ArrayBuffer
 */
printerJobs.prototype.buffer = function () {
  return new Uint8Array(this._queue).buffer;
};
 
 
/**
 * CPCL打印
 */
 
/**
 * 设置标签宽高 必须在开头设置 
 * width:80mm 为576;58mm为384
 */
printerJobs.prototype.label_set_page = function (width,height) {
  var content= "! 0 200 200 " + height + " 1\r\nPAGE-WIDTH " + width + "\r\n";
  this.text(content);
  return this;
};
 
/**
 * 打印表格
 * lineWidth:边框线条宽度0-1
 * top_left_x:左上角X坐标
 * top_left_y:左上角Y坐标
 * bottom_right_x:右下角X坐标
 * bottom_right_y:右下角X坐标
 */
printerJobs.prototype.drawbox = function (lineWidth, top_left_x,  top_left_y,  bottom_right_x,  bottom_right_y) {
  var content = "BOX " + top_left_x + " " + top_left_y + " " + bottom_right_x + " " + bottom_right_y + " " + lineWidth + "\r\n";
  this.text(content);
  return this;
};
 
/**
 *  标签打印 必须在结尾调用
 * @param horizontal 0:正常打印,不旋转;1:整个页面顺时针旋转180°后,打印
 * @param skip       0:打印结束后不定位,直接停止;1:打印结束后定位到标签分割线,如果无缝隙,最大进纸260mm后停止
 */
printerJobs.prototype.label_print = function (horizontal,  skip) {
  var horizontal1;
  var str;
  switch (horizontal) {
    case 0:
      horizontal1 = 0;
      break;
    case 1:
      horizontal1 = 1;
      break;
    case 2:
      horizontal1 = 2;
      break;
    default:
      horizontal1 = 0;
  }
 
  if (skip == 1) {
    str = "PR " + horizontal1 + "\r\nFORM\r\nPRINT\r\n";
  } else {
    str = "PR " + horizontal1 + "\r\nPRINT\r\n";
  }
  this.text(str);
  return this;
};
 
/**
 * 打印一维码
 * @param start_x   打印的起始横坐标
 * @param start_y   打印的起始纵坐标
 * @param text      字符串
 * @param type      条码类型
 *                  0:CODE39;1:CODE128;2:CODE93;3:CODEBAR;4:EAN8;5:EAN13;6:UPCA
 *                  ;7:UPC-E;8:ITF
 * @param rotate    旋转角度 0:不旋转;1 旋转
 * @param linewidth 条码宽度
 * @param height    条码高度
 */
printerJobs.prototype.drawBarCode = function (start_x, start_y, text, barcodeType,  rotate,  linewidth,  height) {
    console.log('start_x',start_x)
    console.log('start_y',start_y)
    console.log('text',text)
    console.log('barcodeType',barcodeType)
    console.log('rotate',rotate)
    console.log('linewidth',linewidth)
    console.log('height',height)
  var str1 = "B";
  if (rotate) {
    str1 = "VB";
  }
 
  var st1 = "128";
  if (barcodeType == 0) {
    st1 = "39";
  } else if (barcodeType == 1) {
    st1 = "128";
  } else if (barcodeType == 2) {
    st1 = "93";
  } else if (barcodeType == 3) {
    st1 = "CODABAR";
  } else if (barcodeType == 4) {
    st1 = "EAN8";
  } else if (barcodeType == 5) {
    st1 = "EAN13";
  } else if (barcodeType == 6) {
    st1 = "UPCA";
  } else if (barcodeType == 7) {
    st1 = "UPCE";
  }
 
  var content = str1 + " " + st1 + " " + linewidth + " 0 " + height + " " + start_x + " " + start_y + " " + text + "\r\n";
    console.log('content')
    console.log(content)
  this.text(content);
  return this;
};
 
/**
 * 打印二维码
 * @param start_x 二维码起始位置
 * @param start_y 二维码结束位置
 * @param text    二维码内容
 * @param rotate  旋转角度
 * @param ver     : QrCode宽度(2-15)
 * @param lel     : QrCode纠错等级(0-20)
 */
printerJobs.prototype.drawQrCode = function ( start_x,  start_y,  text,  rotate,  ver,  lel) {
   var str1 = "B";
  if (rotate != 0) {
    str1 = "VB";
  }
  var content = str1 + " QR " + start_x + " " + start_y + " " + " M " + " " + 2 + " " + "U" + " " + ver + " " + "\r\n" + "MA," + text + "\r\n" + "ENDQR" + "\r\n";
 
  this.text(content);
  return this;
};
 
/**
     * 打印文字
     *
     * @param text_x    起始横坐标
     * @param text_y    起始纵坐标
     * @param text      字符串
     * @param fontSize  字体大小 20:20点阵 1:16点阵;2:24点阵;3:32点阵;4:24点阵放大一倍;5:32点阵放大一倍
     *                  6:24点阵放大两倍;7:32点阵放大两倍;其他:24点阵
     * @param rotate    旋转角度 0:不旋转;1:90度;2:180°;3:270°
     * @param bold      是否粗体 0:取消;1:设置
     * @param underline 是有有下划线 false:没有;true:有
     * @param reverse   是否反白 false:不反白;true:反白
     */
printerJobs.prototype.drawText = function ( text_x,  text_y,  text,  fontSize,  rotate,  bold,  reverse,  underline) {
  var st1 = "";
  if (rotate == 0) {
    st1 = "T";
  } else if (rotate == 1) {
    st1 = "T90";
  } else if (rotate == 2) {
    st1 = "T180";
  } else if (rotate == 3) {
    st1 = "T270";
  }
   var rev = 0;
   var und = 0;
  if (reverse) {
    rev = 1;
  }
  if (underline) {
    und = 1;
  }
  var st2 = 24;
  var st3 = 0;
  if (fontSize == 20) {
    st2 = 20;
    st3 = 0;
  } else if (fontSize == 1) {
    st2 = 55;
    st3 = 0;
  } else if (fontSize == 2) {
    st2 = 24;
    st3 = 0;
  } else if (fontSize == 3) {
    st2 = 55;
    st3 = 11;
  } else if (fontSize == 4) {
    st2 = 24;
    st3 = 11;
  } else if (fontSize == 5) {
    st2 = 55;
    st3 = 33;
  } else if (fontSize == 6) {
    st2 = 24;
    st3 = 22;
  } else if (fontSize == 7) {
    st2 = 55;
    st3 = 77;
  }
 var content = "SETBOLD " + bold + "\r\n" + "IT " + rev + "\r\n" + "UT " + und + "\r\n" + st1 + " " + st2 + " " + st3 + " " + text_x + " " + text_y + " " + text + "\r\n";
 
  this.text(content);
  return this;
};
 
/**
 * 打印反显
 */
printerJobs.prototype.inverse = function ( var1,  var2,  var3,  var4,  width) {
  var str1 = "IL";
  var x0 = var1;
  var y0 = var2;
  var x1 = var3;
  var y1 = var4;
  var w = width;
  if (var1 == var3) {
    x0 = var1;                                                                                                                                           y0 = var2;
    x1 = var1 + width;
    y1 = var2;                                                                                                                          
    w = var4 - var2;
  }
  var content = str1 + " " + x0 + " " + y0 + " " + x1 + " " + y1 + " " + w + "\r\n";
 
  this.text(content);
  return this;
};
 
/**
     * 线条
     *
     * @param lineWidth 线条宽度
     * @param start_x   线条起始点x坐标
     * @param start_y   线条起始点y坐标
     * @param end_x     线条结束点x坐标
     * @param end_y     线条结束点y坐标
     * @param fullline  true:实线  false:?虚线
     */
printerJobs.prototype.drawLine = function ( width,  x0,  y0,  x1,  y1) {
  var content = "LINE " + x0 + " " + y0 + " " + x1 + " " + y1 + " " + width + "\r\n";
 
 
  this.text(content);
  return this;
};
 
 
 
 
 
/**
 * 清空任务
 */
printerJobs.prototype.clear = function() {
    this._queue = Array.from(commands.HARDWARE.HW_INIT);
    return this;
};
 
/**
 * 返回ArrayBuffer
 */
printerJobs.prototype.buffer = function() {
    return new Uint8Array(this._queue).buffer;
};
 
export default printerJobs
 
//module.exports = printerJobs;