PrintHelper.java 5.3 KB

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