printerjobs.js 4.1 KB

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