printTools.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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, 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+10
  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 = 24 // 距离顶部10点像素
  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*2)/lightHeight) // 每行最多字体数
  79. const leftMaxFnums = Math.floor(maxFontNums*0.6)
  80. let rightTop = 0
  81. let rightLeft = (leftMaxFnums+1.2)*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. rightTop = top
  93. // top = textFormat(command,data.shelfName,leftMaxFnums,left,top,lightHeight,"left",1)
  94. // 产品编码 + 产品名称
  95. // top = top+10
  96. top = textFormat(command,data.shelfName +'/'+ data.productCode + '/' + data.productName,leftMaxFnums,left,top,lightHeight,"left",1)
  97. // 产品名称
  98. // top = top+6
  99. // top = textFormat(command,data.productName,leftMaxFnums,left,top,lightHeight,"left",1)
  100. // 打印人打印时间
  101. top = top+10
  102. top = textFormat(command,data.printDate+' '+data.printUser,leftMaxFnums,left,top,lightHeight,"left",1)
  103. // 货位号
  104. const qrtop = textFormat(command,data.shelfPlaceCode,maxFontNums-leftMaxFnums-1.2,rightLeft,rightTop,lightHeight,"center",2)
  105. // 二维码
  106. command.setQR(rightLeft, qrtop, "M", 5, "A", data.barCode)
  107. command.setPagePrint(1,data.currentInven) // 打印分数1,每个标签重发打印2次
  108. return command
  109. }