|
|
@@ -20,90 +20,95 @@ import com.mes.print.util.PrinterUtil;
|
|
|
import com.mes.print.monitor.PrinterMonitor;
|
|
|
|
|
|
/**
|
|
|
- * HP 3004dn 及同类激光打印机:走 Java2D + PrinterJob
|
|
|
- * 支持 SNMP 监控物理打印完成状态(可选)
|
|
|
+ * HP 3004dn 激光打印机
|
|
|
+ * 纸张:读 config 的 printer.paper (A4/A5),默认 A5(模板就是 A5 设计)
|
|
|
*/
|
|
|
public class HpPrinter implements PrinterImpl {
|
|
|
|
|
|
- private PrinterMonitor monitor;
|
|
|
- private Runnable onPrintCompleteCallback;
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置打印完成回调(可选)
|
|
|
- * 如果设置了打印机 IP,会通过 SNMP 监控物理打印完成
|
|
|
- */
|
|
|
- public void setOnPrintComplete(Runnable callback) {
|
|
|
- this.onPrintCompleteCallback = callback;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 配置 SNMP 监控(可选)
|
|
|
- * @param printerIp 打印机 IP 地址(如 192.168.1.100)
|
|
|
- */
|
|
|
- public void enableSnmpMonitor(String printerIp) {
|
|
|
- if (printerIp == null || printerIp.trim().isEmpty()) {
|
|
|
- return;
|
|
|
- }
|
|
|
- try {
|
|
|
- monitor = new PrinterMonitor(printerIp.trim());
|
|
|
- monitor.init();
|
|
|
- System.out.println("[HpPrinter] SNMP 监控已启用: " + printerIp);
|
|
|
- } catch (Exception e) {
|
|
|
- System.err.println("[HpPrinter] SNMP 监控启动失败: " + e.getMessage());
|
|
|
- monitor = null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void printFlowCard(FlowCardData data) {
|
|
|
- if (data == null) throw new PrinterException("流转卡数据为空");
|
|
|
-
|
|
|
- String name = PrinterConfig.get("printer.name");
|
|
|
- PrintService svc = PrinterUtil.find(name);
|
|
|
-
|
|
|
- try {
|
|
|
- PrinterJob job = PrinterJob.getPrinterJob();
|
|
|
- job.setPrintService(svc);
|
|
|
-
|
|
|
- Printable page = FlowCardRenderer.render(data);
|
|
|
- PageFormat pf = job.defaultPage();
|
|
|
- // A4 纸,边距 0(打印机自身有物理边距)
|
|
|
- Paper paper = pf.getPaper();
|
|
|
- paper.setImageableArea(20, 20, paper.getWidth() - 40, paper.getHeight() - 40);
|
|
|
- pf.setPaper(paper);
|
|
|
-
|
|
|
- job.setPrintable(page, pf);
|
|
|
-
|
|
|
- PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
|
|
|
- attrs.add(MediaSizeName.ISO_A4);
|
|
|
- attrs.add(new Copies(1));
|
|
|
-
|
|
|
- // 提交打印任务(阻塞,直到任务进入打印队列)
|
|
|
- job.print(attrs);
|
|
|
-
|
|
|
- // 如果启用了 SNMP 监控,等待物理打印完成
|
|
|
- if (monitor != null && onPrintCompleteCallback != null) {
|
|
|
- monitor.waitForPrintComplete(1, onPrintCompleteCallback, 60);
|
|
|
- } else if (onPrintCompleteCallback != null) {
|
|
|
- // 未启用 SNMP 时,立即回调(队列提交完成 = 打印完成)
|
|
|
- onPrintCompleteCallback.run();
|
|
|
- }
|
|
|
-
|
|
|
- } catch (PrinterException e) {
|
|
|
- throw e;
|
|
|
- } catch (Exception e) {
|
|
|
- throw new PrinterException("流转卡打印失败: " + e.getMessage(), e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void printLabel(LabelData data) {
|
|
|
- throw new PrinterException("HP 打印机不支持标签打印,请配置 TSC 打印机");
|
|
|
- }
|
|
|
-
|
|
|
- public void close() {
|
|
|
- if (monitor != null) {
|
|
|
- monitor.close();
|
|
|
- }
|
|
|
- }
|
|
|
+ private static final double MM_TO_PT = 2.834645669d;
|
|
|
+
|
|
|
+ private PrinterMonitor monitor;
|
|
|
+ private Runnable onPrintCompleteCallback;
|
|
|
+
|
|
|
+ public void setOnPrintComplete(Runnable callback) {
|
|
|
+ this.onPrintCompleteCallback = callback;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void enableSnmpMonitor(String printerIp) {
|
|
|
+ if (printerIp == null || printerIp.trim().isEmpty()) return;
|
|
|
+ try {
|
|
|
+ monitor = new PrinterMonitor(printerIp.trim());
|
|
|
+ monitor.init();
|
|
|
+ System.out.println("[HpPrinter] SNMP monitor enabled: " + printerIp);
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.err.println("[HpPrinter] SNMP monitor init failed: " + e.getMessage());
|
|
|
+ monitor = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void printFlowCard(FlowCardData data) {
|
|
|
+ if (data == null) throw new PrinterException("流转卡数据为空");
|
|
|
+
|
|
|
+ String name = PrinterConfig.get("printer.name");
|
|
|
+ PrintService svc = PrinterUtil.find(name);
|
|
|
+
|
|
|
+ try {
|
|
|
+ PrinterJob job = PrinterJob.getPrinterJob();
|
|
|
+ job.setPrintService(svc);
|
|
|
+
|
|
|
+ Printable page = FlowCardRenderer.render(data);
|
|
|
+
|
|
|
+ String paperCfg = PrinterConfig.get("printer.paper", "A5").trim().toUpperCase();
|
|
|
+ double pageW, pageH;
|
|
|
+ MediaSizeName media;
|
|
|
+ if ("A4".equals(paperCfg)) {
|
|
|
+ pageW = 210 * MM_TO_PT;
|
|
|
+ pageH = 297 * MM_TO_PT;
|
|
|
+ media = MediaSizeName.ISO_A4;
|
|
|
+ } else {
|
|
|
+ pageW = 148 * MM_TO_PT;
|
|
|
+ pageH = 210 * MM_TO_PT;
|
|
|
+ media = MediaSizeName.ISO_A5;
|
|
|
+ }
|
|
|
+ System.out.println("[HpPrinter] paper=" + paperCfg + " (" + Math.round(pageW) + "x" + Math.round(pageH) + " pt)");
|
|
|
+
|
|
|
+ Paper paper = new Paper();
|
|
|
+ paper.setSize(pageW, pageH);
|
|
|
+ double margin = 15d;
|
|
|
+ paper.setImageableArea(margin, margin, pageW - 2 * margin, pageH - 2 * margin);
|
|
|
+
|
|
|
+ PageFormat pf = new PageFormat();
|
|
|
+ pf.setPaper(paper);
|
|
|
+ pf = job.validatePage(pf);
|
|
|
+
|
|
|
+ job.setPrintable(page, pf);
|
|
|
+
|
|
|
+ PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
|
|
|
+ attrs.add(media);
|
|
|
+ attrs.add(new Copies(1));
|
|
|
+
|
|
|
+ job.print(attrs);
|
|
|
+
|
|
|
+ if (monitor != null && onPrintCompleteCallback != null) {
|
|
|
+ monitor.waitForPrintComplete(1, onPrintCompleteCallback, 60);
|
|
|
+ } else if (onPrintCompleteCallback != null) {
|
|
|
+ onPrintCompleteCallback.run();
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (PrinterException e) {
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new PrinterException("流转卡打印失败: " + e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void printLabel(LabelData data) {
|
|
|
+ throw new PrinterException("HP 打印机不支持标签打印");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void close() {
|
|
|
+ if (monitor != null) monitor.close();
|
|
|
+ }
|
|
|
}
|