printTools.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // 获取文本字节总数
  2. export const getBytesCount = function(str){
  3. var bytesCount = 0;
  4. if (str != null) {
  5. for (var i = 0; i < str.length; i++) {
  6. var c = str.charAt(i);
  7. if (c.match(/[^\x00-\xff]/ig) != null) //全角
  8. {
  9. bytesCount += 2;
  10. } else {
  11. bytesCount += 1;
  12. }
  13. }
  14. }
  15. return bytesCount;
  16. }
  17. // JS 包含中文的字符串按固定长度拆分换行算法
  18. export const getTextRows = function(str,iPageSize){
  19. var strArray = [];
  20. var tempStr = "";
  21. var iTotalLength = getBytesCount(str);
  22. for (var i = 0; i < str.length; i++) {
  23. var iCount = getBytesCount(str[i]);
  24. var iCountTemp = getBytesCount(tempStr);
  25. //iCountTemp + iCount >= iPageSize 这里可能出现两种情况:
  26. //长度等于 iPageSize 或 等于 iPageSize + 1(最后一个为中文字符就等于 9 + 2 情况)
  27. if (iCountTemp + iCount >= iPageSize) {
  28. tempStr += str[i];
  29. strArray.push(tempStr);
  30. tempStr = "";
  31. } else {
  32. tempStr += str[i];
  33. }
  34. }
  35. // //最后一次尾巴
  36. if (tempStr.length > 0){
  37. strArray.push(tempStr)
  38. }
  39. return strArray
  40. }
  41. // 格式化打印文本
  42. export const textFormat = function(command,text,maxFontNums,left,top,lightHeight,align,fontSize){
  43. let rowTop = top
  44. const maxNums = maxFontNums
  45. const rowHeight = lightHeight
  46. const textArr = getTextRows(text.replace(/\s+/g, ' '),maxNums*2)
  47. const rows = textArr.length
  48. for(let i=0;i<rows;i++){
  49. if(align=="center"){
  50. const ltxt = textArr[i]
  51. const ltlen = getBytesCount(ltxt)/2*fontSize
  52. command.setText(left+((maxNums-ltlen)/2)*rowHeight-2, rowTop, "TSS24.BF2", fontSize, fontSize, ltxt)
  53. }
  54. else{
  55. command.setText(left, rowTop, "TSS24.BF2", fontSize, fontSize, textArr[i])
  56. }
  57. rowTop = rowTop+rowHeight*fontSize+5
  58. }
  59. return rowTop
  60. }
  61. export const printText = function(tsc,text){
  62. const command = tsc.jpPrinter.createNew()
  63. command.init()
  64. command.setSize(40, 30) // 标签纸张宽高,单位mm
  65. command.setGap(3) // 标签上下间距,单位mm
  66. command.setCls() // 清除缓冲区数据
  67. command.setText(100, 48, "5", 4, 3, text)
  68. command.setPagePrint(1,1) // 打印分数1,每个标签重发打印2次
  69. return command
  70. }
  71. // 60 * 40 尺寸模板
  72. export const printTempl = function(tsc,data){
  73. let top = 32 // 距离顶部
  74. const left = 8 // 距离左边
  75. const lightHeight = 24 // 行高3mm,1mm = 8点
  76. const pageW = 60 // 页签宽度mm
  77. const pageH = 40 // 页签高度mm
  78. const maxFontNums = Math.floor((pageW*8-left*4)/lightHeight) // 每行最多字体数
  79. const leftMaxFnums = Math.floor(maxFontNums*0.4)
  80. const rightMaxFnums = leftMaxFnums + 1
  81. let rightLeft = (leftMaxFnums+2.5)*lightHeight
  82. // 初始化打印机
  83. const command = tsc.jpPrinter.createNew()
  84. command.init()
  85. command.setSize(pageW, pageH) // 标签纸张宽高,单位mm
  86. command.setGap(3) // 标签上下间距,单位mm
  87. command.setCls() // 清除缓冲区数据
  88. // 经销商文字高度,是否换行
  89. top = textFormat(command,data.dealerName,maxFontNums,left,top,lightHeight,"center",1)
  90. // 数字货架名称文字
  91. top = top+10
  92. top = textFormat(command,data.shelfName,maxFontNums,left,top,lightHeight,"center",1)
  93. // 二维码
  94. top = top+15
  95. command.setQR(left*6, top, "M", 5, "A", data.barCode)
  96. // 货位号
  97. const rightTop = textFormat(command,data.shelfPlaceCode,rightMaxFnums,rightLeft,top,lightHeight,"left",4)
  98. // 产品编码
  99. top = rightTop + 1
  100. top = textFormat(command,data.productCode,rightMaxFnums,rightLeft,top,lightHeight,"left",1)
  101. // 打印人打印时间
  102. top = top+3
  103. top = textFormat(command,data.printDate,rightMaxFnums,rightLeft,top,lightHeight,"left",1)
  104. // 打印分数1,每个标签重发打印2次
  105. command.setPagePrint(1,data.currentInven)
  106. return command
  107. }
  108. // 40 * 30 尺寸模板
  109. export const printMiniTempl = function(tsc,data){
  110. let top = 24 // 距离顶部
  111. const left = 4 // 距离左边
  112. const lightHeight = 24 // 行高3mm,1mm = 8点
  113. const pageW = 40 // 页签宽度mm
  114. const pageH = 30 // 页签高度mm
  115. const maxFontNums = Math.floor((pageW*8-left*2)/lightHeight) // 每行最多字体数
  116. const leftMaxFnums = Math.floor(maxFontNums*0.5)
  117. const rightMaxFnums = Math.floor(maxFontNums*0.4)
  118. let rightLeft = (leftMaxFnums+1)*lightHeight
  119. // 初始化打印机
  120. const command = tsc.jpPrinter.createNew()
  121. command.init()
  122. command.setSize(pageW, pageH) // 标签纸张宽高,单位mm
  123. command.setGap(3) // 标签上下间距,单位mm
  124. command.setCls() // 清除缓冲区数据
  125. // 经销商文字高度,是否换行
  126. top = textFormat(command,data.dealerName,maxFontNums,left,top,lightHeight,"center",1)
  127. // 数字货架名称文字
  128. top = top+10
  129. top = textFormat(command,data.shelfName,maxFontNums,left,top,lightHeight,"center",1)
  130. // 二维码
  131. top = top+10
  132. command.setQR(left, top, "M", 5, "A", data.barCode)
  133. // 货位号
  134. const rightTop = textFormat(command,data.shelfPlaceCode,rightMaxFnums,rightLeft,top,lightHeight,"left",3)
  135. // 产品编码
  136. top = rightTop
  137. top = textFormat(command,data.productCode,rightMaxFnums,rightLeft,top,lightHeight,"left",1)
  138. // 打印分数1,每个标签重发打印2次
  139. command.setPagePrint(1,data.currentInven)
  140. return command
  141. }