Ver Fonte

联动打印

wangxichen há 1 dia atrás
pai
commit
2e9aee2410

+ 4 - 0
.classpath

@@ -24,5 +24,9 @@
 	<classpathentry kind="lib" path="lib/s7connector-2.1.jar"/>
 	<classpathentry kind="lib" path="lib/slf4j-api-1.7.36.jar"/>
 	<classpathentry kind="lib" path="lib/sqlite-jdbc-3.36.0.3.jar"/>
+	<classpathentry kind="lib" path="lib/mes-print.jar"/>
+	<classpathentry kind="lib" path="lib/pdfbox-2.0.30.jar"/>
+	<classpathentry kind="lib" path="lib/fontbox-2.0.30.jar"/>
+	<classpathentry kind="lib" path="lib/commons-logging-1.2.jar"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>

BIN
config/liuzhuanka_template.pdf


+ 8 - 0
config/print.properties

@@ -0,0 +1,8 @@
+# 打印机配置文件
+# printer.type: hp=HP激光(流转卡) | tsc=TSC热敏(标签) | none=不打印
+# printer.name: 打印机名称(Windows 设备管理器显示名称)
+# printer.paper: A4 | A5(仅 hp 类型有效,流转卡模板是 A5 设计)
+
+printer.type=hp
+printer.name=HP-3004dn
+printer.paper=A5

BIN
lib/commons-logging-1.2.jar


BIN
lib/fontbox-2.0.30.jar


BIN
lib/mes-print.jar


BIN
lib/pdfbox-2.0.30.jar


+ 152 - 0
src/com/mes/print/PrintHelper.java

@@ -0,0 +1,152 @@
+package com.mes.print;
+
+import com.mes.print.model.FlowCardData;
+import com.mes.print.model.LabelData;
+
+/**
+ * 打印辅助类:封装 mes-print 模块调用
+ *
+ * 根据 print.properties 配置的打印机类型自动选择打印方式:
+ * - hp: 打印流转卡(A4/A5,走 HP 激光)
+ * - tsc: 打印标签(走 TSC 热敏)
+ * - none: 不打印
+ */
+public class PrintHelper {
+
+    /**
+     * 预热打印模块(异步调用,不阻塞主流程)
+     * 首次打印约 3-5 秒的 PDF 模板加载 + 底图渲染放到启动阶段完成
+     */
+    public static void warmUpAsync() {
+        Thread t = new Thread(() -> {
+            try {
+                Printer.warmUp();
+            } catch (Throwable e) {
+                System.err.println("[打印] 预热异常: " + e.getMessage());
+            }
+        }, "print-warmup");
+        t.setDaemon(true);
+        t.start();
+    }
+
+    /**
+     * 打印流转卡(HP 激光打印机)
+     *
+     * @param sn 产品序列号(客户码)
+     * @param steelSn 钢印码(可为 null)
+     * @return true=成功或已跳过;false=失败
+     */
+    public static boolean printFlowCard(String sn, String steelSn) {
+        try {
+            String type = Printer.getCurrentType();
+            if ("none".equals(type)) {
+                System.out.println("[打印] 配置为不打印,跳过流转卡: " + sn);
+                return true;
+            }
+            if (!"hp".equals(type)) {
+                System.out.println("[打印] 打印机类型为 " + type + ",不支持流转卡");
+                return false;
+            }
+
+            FlowCardData data = new FlowCardData();
+            data.setSn(sn);
+            if (steelSn != null) {
+                data.setSteelSn(steelSn);
+            }
+
+            Printer.printFlowCard(data);
+            System.out.println("[打印] 流转卡打印成功: " + sn);
+            return true;
+
+        } catch (PrinterException e) {
+            System.err.println("[打印] 流转卡打印失败: " + e.getMessage());
+            e.printStackTrace();
+            return false;
+        } catch (Exception e) {
+            System.err.println("[打印] 流转卡打印异常: " + e.getMessage());
+            e.printStackTrace();
+            return false;
+        }
+    }
+
+    /**
+     * 打印标签(TSC 热敏标签机)
+     *
+     * @param sn 产品序列号
+     * @param barcode 条码内容(null 时用 sn)
+     * @param qrcode 二维码内容(null 时用 sn)
+     * @param title 标题(null 时用默认「产品标签」)
+     * @param copies 打印份数(≤0 时按 1 份)
+     * @return true=成功或已跳过;false=失败
+     */
+    public static boolean printLabel(String sn, String barcode, String qrcode, String title, int copies) {
+        try {
+            String type = Printer.getCurrentType();
+            if ("none".equals(type)) {
+                System.out.println("[打印] 配置为不打印,跳过标签: " + sn);
+                return true;
+            }
+            if (!"tsc".equals(type)) {
+                System.out.println("[打印] 打印机类型为 " + type + ",不支持标签");
+                return false;
+            }
+
+            LabelData data = new LabelData();
+            data.setSn(sn);
+            data.setBarcode(barcode != null ? barcode : sn);
+            data.setQrcode(qrcode != null ? qrcode : sn);
+            data.setTitle(title != null ? title : "产品标签");
+            data.setCopies(copies > 0 ? copies : 1);
+
+            Printer.printLabel(data);
+            System.out.println("[打印] 标签打印成功: " + sn);
+            return true;
+
+        } catch (PrinterException e) {
+            System.err.println("[打印] 标签打印失败: " + e.getMessage());
+            e.printStackTrace();
+            return false;
+        } catch (Exception e) {
+            System.err.println("[打印] 标签打印异常: " + e.getMessage());
+            e.printStackTrace();
+            return false;
+        }
+    }
+
+    /**
+     * 根据当前配置自动打印
+     * - hp: 打流转卡(steelSn 传 null)
+     * - tsc: 打标签(barcode/qrcode 都用 sn)
+     * - none: 跳过
+     */
+    public static boolean autoPrint(String sn) {
+        return autoPrint(sn, null);
+    }
+
+    /**
+     * 根据当前配置自动打印
+     *
+     * @param sn 产品序列号
+     * @param steelSn 钢印码(仅流转卡使用,可为 null)
+     */
+    public static boolean autoPrint(String sn, String steelSn) {
+        try {
+            String type = Printer.getCurrentType();
+            if ("none".equals(type)) {
+                System.out.println("[打印] 配置为不打印,跳过");
+                return true;
+            } else if ("hp".equals(type)) {
+                return printFlowCard(sn, steelSn);
+            } else if ("tsc".equals(type)) {
+                return printLabel(sn, sn, sn, "产品标签", 1);
+            } else {
+                System.err.println("[打印] 未知的打印机类型: " + type);
+                return false;
+            }
+        } catch (Exception e) {
+            System.err.println("[打印] 自动打印失败: " + e.getMessage());
+            e.printStackTrace();
+            return false;
+        }
+    }
+}

+ 27 - 0
src/com/mes/ui/MesClient.java

@@ -191,6 +191,9 @@ public class MesClient extends JFrame {
 
                     textFocus();
 
+                    // 预热打印模块(后台异步,不阻塞启动)
+                    com.mes.print.PrintHelper.warmUpAsync();
+
                 }catch (Exception e){
                     e.printStackTrace();
                 }
@@ -654,6 +657,30 @@ public class MesClient extends JFrame {
         });
         settingMenu.add(resetTcpMenu_1_1sd);
 
+        // 打印机设置菜单
+        JMenuItem printerSettingMenu = new JMenuItem("打印机设置");
+        printerSettingMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")));
+        printerSettingMenu.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
+        printerSettingMenu.addMouseListener(new MouseAdapter() {
+            @Override
+            public void mousePressed(MouseEvent e) {
+                super.mouseClicked(e);
+                try {
+                    // 调用打印机设置对话框
+                    com.mes.print.ui.PrinterSettingDialog dialog =
+                        new com.mes.print.ui.PrinterSettingDialog(MesClient.this);
+                    dialog.setVisible(true);
+                } catch (Exception ex) {
+                    JOptionPane.showMessageDialog(MesClient.this,
+                        "打开打印机设置失败: " + ex.getMessage(),
+                        "错误",
+                        JOptionPane.ERROR_MESSAGE);
+                    ex.printStackTrace();
+                }
+            }
+        });
+        settingMenu.add(printerSettingMenu);
+
         contentPane = new JPanel();
         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
         setContentPane(contentPane);

+ 20 - 0
src/com/mes/ui/MesRevice.java

@@ -120,9 +120,29 @@ public class MesRevice {
             if(processMsgRet.equalsIgnoreCase("OK")) {
                 if(sn.equals(MesClient.product_sn2.getText())){
                     MesClient.pxstatus2.setText("B:提交成功");
+
+                    // 自动打印(后台异步)
+                    new Thread(() -> {
+                        try {
+                            com.mes.print.PrintHelper.autoPrint(sn);
+                        } catch (Exception e) {
+                            System.err.println("[B面] 打印失败: " + e.getMessage());
+                        }
+                    }, "print-B").start();
+
                     MesClient.resetScanB();
                 }else{
                     MesClient.pxstatus1.setText("A:提交成功");
+
+                    // 自动打印(后台异步)
+                    new Thread(() -> {
+                        try {
+                            com.mes.print.PrintHelper.autoPrint(sn);
+                        } catch (Exception e) {
+                            System.err.println("[A面] 打印失败: " + e.getMessage());
+                        }
+                    }, "print-A").start();
+
                     MesClient.resetScanA();
                 }
             }else{