From 9e38595fb01cd622b421b8efba943f21332e0441 Mon Sep 17 00:00:00 2001 From: xs Date: Tue, 30 Jul 2024 19:59:27 +0800 Subject: [PATCH] =?UTF-8?q?2.7.5=20printer=EF=BC=9A=E5=B1=80=E5=9F=9F?= =?UTF-8?q?=E7=BD=91=E6=89=93=E5=8D=B0=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../printer/controller/PrinterController.java | 10 ++-- .../hw/printer/service/IPrinterService.java | 8 ++-- .../printer/service/PrinterServiceImpl.java | 26 ++++++---- .../com/hw/printer/utils/HwPrintUtil.java | 47 ++++++++++++------- 4 files changed, 56 insertions(+), 35 deletions(-) diff --git a/hw-modules/hw-printer/src/main/java/com/hw/printer/controller/PrinterController.java b/hw-modules/hw-printer/src/main/java/com/hw/printer/controller/PrinterController.java index 2c7ad46b..32cab355 100644 --- a/hw-modules/hw-printer/src/main/java/com/hw/printer/controller/PrinterController.java +++ b/hw-modules/hw-printer/src/main/java/com/hw/printer/controller/PrinterController.java @@ -35,13 +35,13 @@ public class PrinterController { @PostMapping("/printBarcodes") public R printBarcodes(@RequestBody PrinterVo printerVo) { try { - String hostIp = IpUtils.getHostIp(); + String hostIp = IpUtils.getIpAddr(); String printType = printerVo.getPrintType(); - List> printContents = printerVo.getPrintContents(); + List printContentVos = printerVo.getPrintContentVos(); if(printType.equals(PrinterVo.PRINT_TYPE_RAW_LABEL)){ - printerService.printBarcodes(printContents,hostIp); + printerService.printBarcodes(printContentVos,hostIp); }else if(printType.equals(PrinterVo.PRINT_TYPE_PRODUCT_LABEL)){ - printerService.printProductBarcodes(printContents,hostIp); + printerService.printProductBarcodes(printContentVos,hostIp); } return R.ok(); @@ -59,7 +59,7 @@ public class PrinterController { @PostMapping("/printLocationLabel") public R printLocationLabel(@RequestBody HashMap params) { try { - String hostIp = IpUtils.getHostIp(); + String hostIp = IpUtils.getIpAddr(); printerService.printLocationLabel(params,hostIp); return R.ok(); } catch (Exception e) { diff --git a/hw-modules/hw-printer/src/main/java/com/hw/printer/service/IPrinterService.java b/hw-modules/hw-printer/src/main/java/com/hw/printer/service/IPrinterService.java index 4a00eb36..3fa57272 100644 --- a/hw-modules/hw-printer/src/main/java/com/hw/printer/service/IPrinterService.java +++ b/hw-modules/hw-printer/src/main/java/com/hw/printer/service/IPrinterService.java @@ -17,23 +17,23 @@ public interface IPrinterService { /** * 打印原材料条码接口 * - * @param printContents + * @param printContentVos * @param ipAddress 客户端IP地址 * @return * @throws Exception */ - public void printBarcodes(List> printContents,String ipAddress) throws Exception; + public void printBarcodes(List printContentVos,String ipAddress) throws Exception; /** * 打印成品条码接口 * - * @param printContents + * @param printContentVos * @param ipAddress 客户端IP地址 * @return * @throws Exception */ - public void printProductBarcodes(List> printContents,String ipAddress) throws Exception; + public void printProductBarcodes(List printContentVos,String ipAddress) throws Exception; /** * 打印库位条码接口 diff --git a/hw-modules/hw-printer/src/main/java/com/hw/printer/service/PrinterServiceImpl.java b/hw-modules/hw-printer/src/main/java/com/hw/printer/service/PrinterServiceImpl.java index 3a173b3a..c1a83b2f 100644 --- a/hw-modules/hw-printer/src/main/java/com/hw/printer/service/PrinterServiceImpl.java +++ b/hw-modules/hw-printer/src/main/java/com/hw/printer/service/PrinterServiceImpl.java @@ -4,6 +4,8 @@ import com.hw.common.core.utils.DateUtils; import com.hw.mes.api.domain.MesBaseBarcodeInfo; import com.hw.printer.api.domain.vo.PrintContentVo; import com.hw.printer.utils.HwPrintUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Value; @@ -19,6 +21,9 @@ import java.util.List; @Service public class PrinterServiceImpl implements IPrinterService { + private static final Logger log = LoggerFactory.getLogger(PrinterServiceImpl.class); + + /** * 打印机映射路径 */ @@ -67,35 +72,36 @@ public class PrinterServiceImpl implements IPrinterService { /** * 打印原材料条码接口 * - * @param printContents + * @param printContentVos * @param ipAddress 客户端IP地址 * @return * @throws Exception */ @Override - public void printBarcodes(List> printContents,String ipAddress) throws Exception { + public void printBarcodes(List printContentVos,String ipAddress) throws Exception { // HashMap params = new HashMap<>(); // params.put("localPrintPath", localPrintPath); // params.put("pdfTemplatePath", pdfTemplatePath); // params.put("generatePath", generatePath); // params.put("barcodeInfo", barcodeInfo.getBarcodeInfo()); -// params.put("batchCode", barcodeInfo.getBatchCode()); +// params.put("batchCode", barcodeInfo.getBatchCodeu()); // params.put("materialName", barcodeInfo.getMaterialName()); // params.put("date", DateUtils.getDate()); - HwPrintUtil.printBarcodes(ipAddress, pdfTemplatePath, - generatePath, printContents); + log.info("ipaddress:"+ipAddress); + HwPrintUtil.printBarcode(ipAddress, pdfTemplatePath, + generatePath, printContentVos); } /** * 打印成品条码接口 * - * @param printContents + * @param printContentVos * @param ipAddress 客户端IP地址 * @return * @throws Exception */ @Override - public void printProductBarcodes(List> printContents,String ipAddress) throws Exception { + public void printProductBarcodes(List printContentVos,String ipAddress) throws Exception { // HashMap params = new HashMap<>(); // params.put("localPrintPath", localPrintPath); // params.put("pdfTemplatePath", pdfTemplatePath); @@ -104,8 +110,9 @@ public class PrinterServiceImpl implements IPrinterService { // params.put("batchCode", barcodeInfo.getBatchCode()); // params.put("materialName", barcodeInfo.getMaterialName()); // params.put("date", DateUtils.getDate()); - HwPrintUtil.printBarcodes(ipAddress, productLabelTemplatePath, - generateProductLabelPath, printContents); + log.info("ipaddress:"+ipAddress); + HwPrintUtil.printBarcode(ipAddress, productLabelTemplatePath, + generateProductLabelPath, printContentVos); } @@ -118,6 +125,7 @@ public class PrinterServiceImpl implements IPrinterService { */ @Override public void printLocationLabel(HashMap params,String ipAddress) throws IOException { + log.info("ipaddress:"+ipAddress); HwPrintUtil.printLocationQRCode(ipAddress, locationLabelTemplatePath, generateLocationLabelPath, params); } diff --git a/hw-modules/hw-printer/src/main/java/com/hw/printer/utils/HwPrintUtil.java b/hw-modules/hw-printer/src/main/java/com/hw/printer/utils/HwPrintUtil.java index 72074031..cac5a993 100644 --- a/hw-modules/hw-printer/src/main/java/com/hw/printer/utils/HwPrintUtil.java +++ b/hw-modules/hw-printer/src/main/java/com/hw/printer/utils/HwPrintUtil.java @@ -8,24 +8,23 @@ import com.itextpdf.text.Image; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.*; import org.apache.pdfbox.pdmodel.PDDocument; - -import org.apache.pdfbox.pdmodel.interactive.form.PDCheckBox; import org.apache.pdfbox.printing.PDFPrintable; import org.apache.pdfbox.printing.Scaling; - -import javax.annotation.PostConstruct; -import javax.print.*; - -import java.awt.print.*; -import java.io.*; -import java.util.HashMap; -import java.util.List; -import java.util.Set; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; +import javax.annotation.PostConstruct; +import javax.print.PrintService; +import javax.print.PrintServiceLookup; +import java.awt.print.*; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; + /** * @author wanghao @@ -58,14 +57,20 @@ public class HwPrintUtil { * \\10.11.43.159\HP Laser NS 1020 */ public static void initPrintService(String localPrintPath) { -// localPrintPath = "导出"; +// localPrintPath = "192.168.2.26"; // localPrintPath = "ZD"; + //必须每次打印前重新初始化,主要是每次打印的打印机不一定相同 + log.info("localPrintPath:" + localPrintPath); + log.info("printService:"+printService); for (PrintService service : printServices) { + log.info("service:"+service.getName()); if (service.getName().contains(localPrintPath)) { + log.info("printservicefind:"+service.getName()); printService = service; break; } } + if (printService == null) { throw new ServiceException("打印机配置错误!"); } @@ -78,6 +83,7 @@ public class HwPrintUtil { @PostConstruct public static void findPrinters() { printServices = PrintServiceLookup.lookupPrintServices(null, null); + log.info("printservices:"+printServices.toString()); System.out.println("Bean已初始化,执行@PostConstruct注解的方法"); } @@ -92,7 +98,7 @@ public class HwPrintUtil { public static void printBarcodes(String localPrintPath, String rawLabelTemplatePath, String generateRawLabelPath, List> printContents) throws IOException { for (List printContentVos : printContents) { - printBarCode(localPrintPath, rawLabelTemplatePath, + printBarcode(localPrintPath, rawLabelTemplatePath, generateRawLabelPath, printContentVos); } } @@ -106,7 +112,7 @@ public class HwPrintUtil { * @param generateRawLabelPath * @param printContentVos */ - public static void printBarCode(String localPrintPath, String rawLabelTemplatePath, + public static void printBarcode(String localPrintPath, String rawLabelTemplatePath, String generateRawLabelPath, List printContentVos) throws IOException { //解析参数生成打印图片 // String localPrintPath = params.get("localPrintPath"); @@ -149,7 +155,7 @@ public class HwPrintUtil { if (printContentVo.getType() == PrintContentVo.TYPE_TEXT) { form.setField(printContentVo.getKey(), printContentVo.getValue()); } else if (printContentVo.getType() == PrintContentVo.TYPE_CHECKBOX) { - setCheckBox(form, stamper, printContentVo.getKey()); +// setCheckBox(form, stamper, printContentVo.getKey()); } else if (printContentVo.getType() == PrintContentVo.TYPE_QRCODE) { setImage(form, stamper, printContentVo.getKey(), printContentVo.getValue()); } @@ -177,6 +183,7 @@ public class HwPrintUtil { doc.close(); return generateFile; } catch (Exception e) { + e.printStackTrace(); log.error("打印条码生成PDF异常:" + e); } return null; @@ -307,8 +314,11 @@ public class HwPrintUtil { throw new IOException("PDF打印失败:", e); } } catch (PrinterException e) { - throw new ServiceException("打印机服务错误:" + e); + throw new ServiceException("打印机服务错误:" + e + "," + e.getMessage()); } finally { + if (printService != null) { + printService = null; + } if (document != null) { try { document.close(); @@ -460,6 +470,9 @@ public class HwPrintUtil { } catch (PrinterException e) { throw new ServiceException("打印机服务错误:" + e); } finally { + if (printService != null) { + printService = null; + } if (document != null) { try { document.close();