printerjobs.js 4.1 KB

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