printerjobs.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. const commands = require('./commands');
  2. const gbk = require('./gbk');
  3. const printerJobs = function() {
  4. this._queue = Array.from(commands.HARDWARE.HW_INIT);
  5. this._enqueue = function(cmd) {
  6. this._queue.push.apply(this._queue, cmd);
  7. }
  8. };
  9. /**
  10. * 增加打印内容
  11. * @param {string} content 文字内容
  12. */
  13. printerJobs.prototype.text = function(content) {
  14. if (content) {
  15. let uint8Array = gbk.encode(content);
  16. let encoded = Array.from(uint8Array);
  17. this._enqueue(encoded);
  18. }
  19. return this;
  20. };
  21. /**
  22. * 打印文字
  23. * @param {string} content 文字内容
  24. */
  25. printerJobs.prototype.print = function(content) {
  26. this.text(content);
  27. this._enqueue(commands.LF);
  28. return this;
  29. };
  30. /**
  31. * 分页
  32. */
  33. printerJobs.prototype.ctlff = function() {
  34. this._enqueue(commands.FEED_CONTROL_SEQUENCES.CTL_FF);
  35. return this;
  36. };
  37. /**
  38. * 打印文字并换行
  39. * @param {string} content 文字内容
  40. */
  41. printerJobs.prototype.println = function(content = '') {
  42. return this.print(content + commands.EOL);
  43. };
  44. /**
  45. * 设置对齐方式
  46. * @param {string} align 对齐方式 LT/CT/RT
  47. */
  48. printerJobs.prototype.setAlign = function(align) {
  49. this._enqueue(commands.TEXT_FORMAT['TXT_ALIGN_' + align.toUpperCase()]);
  50. return this;
  51. };
  52. /**
  53. * 设置字体
  54. * @param {string} family A/B/C
  55. */
  56. printerJobs.prototype.setFont = function(family) {
  57. this._enqueue(commands.TEXT_FORMAT['TXT_FONT_' + family.toUpperCase()]);
  58. return this;
  59. };
  60. /**
  61. * 设定字体尺寸
  62. * @param {number} width 字体宽度 1~2
  63. * @param {number} height 字体高度 1~2
  64. */
  65. printerJobs.prototype.setSize = function(width, height) {
  66. if (2 >= width && 2 >= height) {
  67. this._enqueue(commands.TEXT_FORMAT.TXT_NORMAL);
  68. if (2 === width && 2 === height) {
  69. this._enqueue(commands.TEXT_FORMAT.TXT_4SQUARE);
  70. } else if (1 === width && 2 === height) {
  71. this._enqueue(commands.TEXT_FORMAT.TXT_2HEIGHT);
  72. } else if (2 === width && 1 === height) {
  73. this._enqueue(commands.TEXT_FORMAT.TXT_2WIDTH);
  74. }
  75. }
  76. return this;
  77. };
  78. /**
  79. * 设定字体是否加粗
  80. * @param {boolean} bold
  81. */
  82. printerJobs.prototype.setBold = function(bold) {
  83. if (typeof bold !== 'boolean') {
  84. bold = true;
  85. }
  86. this._enqueue(bold ? commands.TEXT_FORMAT.TXT_BOLD_ON : commands.TEXT_FORMAT.TXT_BOLD_OFF);
  87. return this;
  88. };
  89. /**
  90. * 设定是否开启下划线
  91. * @param {boolean} underline
  92. */
  93. printerJobs.prototype.setUnderline = function(underline) {
  94. if (typeof underline !== 'boolean') {
  95. underline = true;
  96. }
  97. this._enqueue(underline ? commands.TEXT_FORMAT.TXT_UNDERL_ON : commands.TEXT_FORMAT.TXT_UNDERL_OFF);
  98. return this;
  99. };
  100. /**
  101. * 设置行间距为 n 点行,默认值行间距是 30 点
  102. * @param {number} n 0≤n≤255
  103. */
  104. printerJobs.prototype.setLineSpacing = function(n) {
  105. if (n === undefined || n === null) {
  106. this._enqueue(commands.LINE_SPACING.LS_DEFAULT);
  107. } else if (n === 0) {
  108. this._enqueue(commands.LINE_SPACING.LS_ZERO);
  109. } else {
  110. this._enqueue(commands.LINE_SPACING.LS_SET);
  111. this._enqueue([n]);
  112. }
  113. return this;
  114. };
  115. /**
  116. * 打印空行
  117. * @param {number} n
  118. */
  119. printerJobs.prototype.lineFeed = function(n = 1) {
  120. return this.print(new Array(n).fill(commands.EOL).join(''));
  121. };
  122. /**
  123. * 设置字体颜色,需要打印机支持
  124. * @param {number} color - 0 默认颜色黑色 1 红色
  125. */
  126. printerJobs.prototype.setColor = function(color) {
  127. this._enqueue(commands.COLOR[color === 1 ? 1 : 0]);
  128. return this;
  129. };
  130. /**
  131. * https://support.loyverse.com/hardware/printers/use-the-beeper-in-a-escpos-printers
  132. * 蜂鸣警报,需要打印机支持
  133. * @param {number} n 蜂鸣次数,1-9
  134. * @param {number} t 蜂鸣长短,1-9
  135. */
  136. printerJobs.prototype.beep = function(n, t) {
  137. this._enqueue(commands.BEEP);
  138. this._enqueue([n, t]);
  139. return this;
  140. };
  141. /**
  142. * 清空任务
  143. */
  144. printerJobs.prototype.clear = function() {
  145. this._queue = Array.from(commands.HARDWARE.HW_INIT);
  146. return this;
  147. };
  148. /**
  149. * 打印条码
  150. * @param {string} content 条码内容 code128A,B,C可以混合打印在一起
  151. */
  152. printerJobs.prototype.printBarcode = function(content) {
  153. if (content) {
  154. let bar = commands.BARCODE_FORMAT;
  155. // const cmds = [].concat(bar.BARCODE_TXT_OFF, bar.BARCODE_HEIGHT_DEFAULT, bar.BARCODE_WIDTH(2), bar
  156. // .BARCODE_CODE128, content);
  157. const cmds = [].concat(bar.BARCODE_TXT_BLW, bar.BARCODE_HEIGHT_DEFAULT, bar.BARCODE_WIDTH(2), bar
  158. .BARCODE_CODE128, content);
  159. this._enqueue(cmds);
  160. // this._enqueue(commands.LF);
  161. }
  162. return this;
  163. };
  164. /**
  165. * 打印二维码
  166. * @param {string} content 二维码内容
  167. */
  168. printerJobs.prototype.printQrcode = function(content) {
  169. if (content) {
  170. let qr = commands.QRCODE_FORMAT;
  171. // const cmds = [].concat([27, 97, 1], [29, 118, 48, 0, 30, 0, 240, 0], content, [27, 74, 3], [27, 64]);
  172. const cmds = [].concat(qr.QRCODE_SIZE(4), qr.QRCODE_ERROR(49), qr.QRCODE_DATA(content),
  173. qr.QRCODE_PRINT);
  174. this._enqueue(cmds);
  175. // this._enqueue(commands.LF);
  176. }
  177. return this;
  178. };
  179. /**
  180. * 返回ArrayBuffer
  181. */
  182. printerJobs.prototype.buffer = function() {
  183. return new Uint8Array(this._queue).buffer;
  184. };
  185. module.exports = printerJobs;