cpcl.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * cpcl 命令打印工具类
  3. * 2021.04.26 uni-app版本
  4. * @auth boolTrue
  5. */
  6. var gbk = require("./printUtil-GBK.js");
  7. var jpPrinter = {
  8. createNew: function() {
  9. var jpPrinter = {};
  10. var data = "";
  11. var command = []
  12. var rawCommand = ''
  13. jpPrinter.name = "标签模式";
  14. jpPrinter.addCommand = function(content) { //将指令转成数组装起
  15. rawCommand += content
  16. }
  17. jpPrinter.init = function(width, height, printNum, rotation = 0, offset = 0) {
  18. var strCmd = '! ' + offset + ' 200 200 ' + height + ' ' + printNum + '\n';
  19. strCmd += "PAGE-WIDTH " + width + '\n';
  20. if (rotation == 1)
  21. strCmd += "ZPROTATE90\n";
  22. else if (rotation == 2)
  23. strCmd += "ZPROTATE180\n";
  24. else if (rotation == 3)
  25. strCmd += "ZPROTATE270\n";
  26. else
  27. strCmd += "ZPROTATE0\n";
  28. jpPrinter.addCommand(strCmd)
  29. };
  30. jpPrinter.setText = function(x, y, font, xsize, ysize, str) { //横向打印文字
  31. data = "T " + "4" + " " + xsize + " " + x + " " + y + " " + str + "\n"
  32. jpPrinter.addCommand(data)
  33. };
  34. jpPrinter.setGap = function(pageWidth) { //设置标签模式
  35. data = "GAP-SENSE" + "\n";
  36. jpPrinter.addCommand(data)
  37. };
  38. jpPrinter.setSpeed = function(printSpeed) { //设置打印机速度0 - 5
  39. data = "SPEED " + printSpeed + "\n";
  40. jpPrinter.addCommand(data)
  41. };
  42. jpPrinter.setDensity = function(printDensity) { //设置打印机浓度最亮的打印输出为对比度级别 0。最暗的对比度级别为 3。
  43. data = "CONTRAST " + printDensity + "\n";
  44. jpPrinter.addCommand(data)
  45. };
  46. jpPrinter.setMag = function(w,h) { //字体放大指定的放大倍数。
  47. data = "SETMAG " + w + " " + h + "\n"
  48. jpPrinter.addCommand(data)
  49. };
  50. jpPrinter.setLine = function(x0, y0, x1, y1, width) { //绘制线条
  51. data = "LINE " + x0 + " " + y0 + " " + x1 + " " + y1 + " " + width + "\n"
  52. jpPrinter.addCommand(data)
  53. };
  54. jpPrinter.setBox = function(x_start, y_start, x_end, y_end, thickness) { //绘制方框
  55. data = "BOX " + x_start + " " + y_start + " " + x_end + " " + y_end + " " + thickness + "\n";
  56. jpPrinter.addCommand(data)
  57. };
  58. jpPrinter.setFeed = function(feed) { //将纸向前推出n
  59. data = "PREFEED " + feed + "\n";
  60. jpPrinter.addCommand(data)
  61. };
  62. jpPrinter.setQR = function(x, y, level, ver, scale, content) { //打印二维码
  63. var strCmd = 'B QR ' + x + ' ' + y + ' M ' + ver + ' U ' + scale + '\n' + level + 'A,' + content + '\n';
  64. strCmd += 'ENDQR\n';
  65. jpPrinter.addCommand(strCmd)
  66. };
  67. jpPrinter.setBar = function(type, width, ratio, height, x, y, content) { //打印条形码
  68. data = "BARCODE " + type + " " + width + " " + ratio + " " + height + " " + x + " " + y + " " + content + "\n"
  69. jpPrinter.addCommand(data)
  70. };
  71. jpPrinter.setSound = function(interval) { //控制蜂鸣器,蜂鸣持续时间
  72. data = "BEEP " + interval + "\n";
  73. jpPrinter.addCommand(data)
  74. };
  75. jpPrinter.setPageForm = function() { //换页
  76. data = "FORM "+"\n"
  77. jpPrinter.addCommand(data)
  78. };
  79. jpPrinter.setPagePrint = function() { //打印页面
  80. data = "PRINT "+"\n"
  81. jpPrinter.addCommand(data)
  82. };
  83. // 添加命令
  84. jpPrinter.RawCommand = function(data) {
  85. jpPrinter.addCommand(data)
  86. }
  87. //获取打印数据
  88. jpPrinter.getData = function() {
  89. let buffer = gbk.strToGBKByte(rawCommand)
  90. return buffer;
  91. };
  92. jpPrinter.getRawData = function() {
  93. return rawCommand;
  94. };
  95. jpPrinter.clearCommand = function() {
  96. rawCommand = ''
  97. };
  98. return jpPrinter;
  99. }
  100. };
  101. module.exports.jpPrinter = jpPrinter;