ke_junjie
2025-06-04 4bf0783db564f0d446c74d7b42242aff879b894c
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
const commands = require('./commands');
const gbk = require('./gbk');
 
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) {
  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";
 
  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;
};
 
module.exports = printerJobs;