PrintHelper.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package com.mes.print;
  2. import com.mes.print.model.FlowCardData;
  3. import com.mes.print.model.LabelData;
  4. /**
  5. * 打印辅助类:封装 mes-print 模块调用
  6. *
  7. * 根据 print.properties 配置的打印机类型自动选择打印方式:
  8. * - hp: 打印流转卡(A4/A5,走 HP 激光)
  9. * - tsc: 打印标签(走 TSC 热敏)
  10. * - none: 不打印
  11. */
  12. public class PrintHelper {
  13. /**
  14. * 预热打印模块(异步调用,不阻塞主流程)
  15. * 首次打印约 3-5 秒的 PDF 模板加载 + 底图渲染放到启动阶段完成
  16. */
  17. public static void warmUpAsync() {
  18. Thread t = new Thread(() -> {
  19. try {
  20. Printer.warmUp();
  21. } catch (Throwable e) {
  22. System.err.println("[打印] 预热异常: " + e.getMessage());
  23. }
  24. }, "print-warmup");
  25. t.setDaemon(true);
  26. t.start();
  27. }
  28. /**
  29. * 打印流转卡(HP 激光打印机)
  30. *
  31. * @param sn 产品序列号(客户码)
  32. * @param steelSn 钢印码(可为 null)
  33. * @return true=成功或已跳过;false=失败
  34. */
  35. public static boolean printFlowCard(String sn, String steelSn) {
  36. try {
  37. String type = Printer.getCurrentType();
  38. if ("none".equals(type)) {
  39. System.out.println("[打印] 配置为不打印,跳过流转卡: " + sn);
  40. return true;
  41. }
  42. if (!"hp".equals(type)) {
  43. System.out.println("[打印] 打印机类型为 " + type + ",不支持流转卡");
  44. return false;
  45. }
  46. FlowCardData data = new FlowCardData();
  47. data.setSn(sn);
  48. if (steelSn != null) {
  49. data.setSteelSn(steelSn);
  50. }
  51. Printer.printFlowCard(data);
  52. System.out.println("[打印] 流转卡打印成功: " + sn);
  53. return true;
  54. } catch (PrinterException e) {
  55. System.err.println("[打印] 流转卡打印失败: " + e.getMessage());
  56. e.printStackTrace();
  57. return false;
  58. } catch (Exception e) {
  59. System.err.println("[打印] 流转卡打印异常: " + e.getMessage());
  60. e.printStackTrace();
  61. return false;
  62. }
  63. }
  64. /**
  65. * 打印标签(TSC 热敏标签机)
  66. *
  67. * @param sn 产品序列号
  68. * @param barcode 条码内容(null 时用 sn)
  69. * @param qrcode 二维码内容(null 时用 sn)
  70. * @param title 标题(null 时用默认「产品标签」)
  71. * @param copies 打印份数(≤0 时按 1 份)
  72. * @return true=成功或已跳过;false=失败
  73. */
  74. public static boolean printLabel(String sn, String barcode, String qrcode, String title, int copies) {
  75. try {
  76. String type = Printer.getCurrentType();
  77. if ("none".equals(type)) {
  78. System.out.println("[打印] 配置为不打印,跳过标签: " + sn);
  79. return true;
  80. }
  81. if (!"tsc".equals(type)) {
  82. System.out.println("[打印] 打印机类型为 " + type + ",不支持标签");
  83. return false;
  84. }
  85. LabelData data = new LabelData();
  86. data.setSn(sn);
  87. data.setBarcode(barcode != null ? barcode : sn);
  88. data.setQrcode(qrcode != null ? qrcode : sn);
  89. data.setTitle(title != null ? title : "产品标签");
  90. data.setCopies(copies > 0 ? copies : 1);
  91. Printer.printLabel(data);
  92. System.out.println("[打印] 标签打印成功: " + sn);
  93. return true;
  94. } catch (PrinterException e) {
  95. System.err.println("[打印] 标签打印失败: " + e.getMessage());
  96. e.printStackTrace();
  97. return false;
  98. } catch (Exception e) {
  99. System.err.println("[打印] 标签打印异常: " + e.getMessage());
  100. e.printStackTrace();
  101. return false;
  102. }
  103. }
  104. /**
  105. * 根据当前配置自动打印
  106. * - hp: 打流转卡(steelSn 传 null)
  107. * - tsc: 打标签(barcode/qrcode 都用 sn)
  108. * - none: 跳过
  109. */
  110. public static boolean autoPrint(String sn) {
  111. return autoPrint(sn, null);
  112. }
  113. /**
  114. * 根据当前配置自动打印
  115. *
  116. * @param sn 产品序列号
  117. * @param steelSn 钢印码(仅流转卡使用,可为 null)
  118. */
  119. public static boolean autoPrint(String sn, String steelSn) {
  120. try {
  121. String type = Printer.getCurrentType();
  122. if ("none".equals(type)) {
  123. System.out.println("[打印] 配置为不打印,跳过");
  124. return true;
  125. } else if ("hp".equals(type)) {
  126. return printFlowCard(sn, steelSn);
  127. } else if ("tsc".equals(type)) {
  128. return printLabel(sn, sn, sn, "产品标签", 1);
  129. } else {
  130. System.err.println("[打印] 未知的打印机类型: " + type);
  131. return false;
  132. }
  133. } catch (Exception e) {
  134. System.err.println("[打印] 自动打印失败: " + e.getMessage());
  135. e.printStackTrace();
  136. return false;
  137. }
  138. }
  139. }