printer:局域网打印优化
master
xs 11 months ago
parent 052a701266
commit 9e38595fb0

@ -35,13 +35,13 @@ public class PrinterController {
@PostMapping("/printBarcodes") @PostMapping("/printBarcodes")
public R<MesBaseBarcodeInfo> printBarcodes(@RequestBody PrinterVo printerVo) { public R<MesBaseBarcodeInfo> printBarcodes(@RequestBody PrinterVo printerVo) {
try { try {
String hostIp = IpUtils.getHostIp(); String hostIp = IpUtils.getIpAddr();
String printType = printerVo.getPrintType(); String printType = printerVo.getPrintType();
List<List<PrintContentVo>> printContents = printerVo.getPrintContents(); List<PrintContentVo> printContentVos = printerVo.getPrintContentVos();
if(printType.equals(PrinterVo.PRINT_TYPE_RAW_LABEL)){ 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)){ }else if(printType.equals(PrinterVo.PRINT_TYPE_PRODUCT_LABEL)){
printerService.printProductBarcodes(printContents,hostIp); printerService.printProductBarcodes(printContentVos,hostIp);
} }
return R.ok(); return R.ok();
@ -59,7 +59,7 @@ public class PrinterController {
@PostMapping("/printLocationLabel") @PostMapping("/printLocationLabel")
public R<?> printLocationLabel(@RequestBody HashMap<String, String> params) { public R<?> printLocationLabel(@RequestBody HashMap<String, String> params) {
try { try {
String hostIp = IpUtils.getHostIp(); String hostIp = IpUtils.getIpAddr();
printerService.printLocationLabel(params,hostIp); printerService.printLocationLabel(params,hostIp);
return R.ok(); return R.ok();
} catch (Exception e) { } catch (Exception e) {

@ -17,23 +17,23 @@ public interface IPrinterService {
/** /**
* *
* *
* @param printContents * @param printContentVos
* @param ipAddress IP * @param ipAddress IP
* @return * @return
* @throws Exception * @throws Exception
*/ */
public void printBarcodes(List<List<PrintContentVo>> printContents,String ipAddress) throws Exception; public void printBarcodes(List<PrintContentVo> printContentVos,String ipAddress) throws Exception;
/** /**
* *
* *
* @param printContents * @param printContentVos
* @param ipAddress IP * @param ipAddress IP
* @return * @return
* @throws Exception * @throws Exception
*/ */
public void printProductBarcodes(List<List<PrintContentVo>> printContents,String ipAddress) throws Exception; public void printProductBarcodes(List<PrintContentVo> printContentVos,String ipAddress) throws Exception;
/** /**
* *

@ -4,6 +4,8 @@ import com.hw.common.core.utils.DateUtils;
import com.hw.mes.api.domain.MesBaseBarcodeInfo; import com.hw.mes.api.domain.MesBaseBarcodeInfo;
import com.hw.printer.api.domain.vo.PrintContentVo; import com.hw.printer.api.domain.vo.PrintContentVo;
import com.hw.printer.utils.HwPrintUtil; import com.hw.printer.utils.HwPrintUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -19,6 +21,9 @@ import java.util.List;
@Service @Service
public class PrinterServiceImpl implements IPrinterService { 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 * @param ipAddress IP
* @return * @return
* @throws Exception * @throws Exception
*/ */
@Override @Override
public void printBarcodes(List<List<PrintContentVo>> printContents,String ipAddress) throws Exception { public void printBarcodes(List<PrintContentVo> printContentVos,String ipAddress) throws Exception {
// HashMap<String, String> params = new HashMap<>(); // HashMap<String, String> params = new HashMap<>();
// params.put("localPrintPath", localPrintPath); // params.put("localPrintPath", localPrintPath);
// params.put("pdfTemplatePath", pdfTemplatePath); // params.put("pdfTemplatePath", pdfTemplatePath);
// params.put("generatePath", generatePath); // params.put("generatePath", generatePath);
// params.put("barcodeInfo", barcodeInfo.getBarcodeInfo()); // params.put("barcodeInfo", barcodeInfo.getBarcodeInfo());
// params.put("batchCode", barcodeInfo.getBatchCode()); // params.put("batchCode", barcodeInfo.getBatchCodeu());
// params.put("materialName", barcodeInfo.getMaterialName()); // params.put("materialName", barcodeInfo.getMaterialName());
// params.put("date", DateUtils.getDate()); // params.put("date", DateUtils.getDate());
HwPrintUtil.printBarcodes(ipAddress, pdfTemplatePath, log.info("ipaddress:"+ipAddress);
generatePath, printContents); HwPrintUtil.printBarcode(ipAddress, pdfTemplatePath,
generatePath, printContentVos);
} }
/** /**
* *
* *
* @param printContents * @param printContentVos
* @param ipAddress IP * @param ipAddress IP
* @return * @return
* @throws Exception * @throws Exception
*/ */
@Override @Override
public void printProductBarcodes(List<List<PrintContentVo>> printContents,String ipAddress) throws Exception { public void printProductBarcodes(List<PrintContentVo> printContentVos,String ipAddress) throws Exception {
// HashMap<String, String> params = new HashMap<>(); // HashMap<String, String> params = new HashMap<>();
// params.put("localPrintPath", localPrintPath); // params.put("localPrintPath", localPrintPath);
// params.put("pdfTemplatePath", pdfTemplatePath); // params.put("pdfTemplatePath", pdfTemplatePath);
@ -104,8 +110,9 @@ public class PrinterServiceImpl implements IPrinterService {
// params.put("batchCode", barcodeInfo.getBatchCode()); // params.put("batchCode", barcodeInfo.getBatchCode());
// params.put("materialName", barcodeInfo.getMaterialName()); // params.put("materialName", barcodeInfo.getMaterialName());
// params.put("date", DateUtils.getDate()); // params.put("date", DateUtils.getDate());
HwPrintUtil.printBarcodes(ipAddress, productLabelTemplatePath, log.info("ipaddress:"+ipAddress);
generateProductLabelPath, printContents); HwPrintUtil.printBarcode(ipAddress, productLabelTemplatePath,
generateProductLabelPath, printContentVos);
} }
@ -118,6 +125,7 @@ public class PrinterServiceImpl implements IPrinterService {
*/ */
@Override @Override
public void printLocationLabel(HashMap<String, String> params,String ipAddress) throws IOException { public void printLocationLabel(HashMap<String, String> params,String ipAddress) throws IOException {
log.info("ipaddress:"+ipAddress);
HwPrintUtil.printLocationQRCode(ipAddress, locationLabelTemplatePath, HwPrintUtil.printLocationQRCode(ipAddress, locationLabelTemplatePath,
generateLocationLabelPath, params); generateLocationLabelPath, params);
} }

@ -8,24 +8,23 @@ import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle; import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*; import com.itextpdf.text.pdf.*;
import org.apache.pdfbox.pdmodel.PDDocument; 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.PDFPrintable;
import org.apache.pdfbox.printing.Scaling; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; 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 * @author wanghao
@ -58,14 +57,20 @@ public class HwPrintUtil {
* \\10.11.43.159\HP Laser NS 1020 * \\10.11.43.159\HP Laser NS 1020
*/ */
public static void initPrintService(String localPrintPath) { public static void initPrintService(String localPrintPath) {
// localPrintPath = "导出"; // localPrintPath = "192.168.2.26";
// localPrintPath = "ZD"; // localPrintPath = "ZD";
//必须每次打印前重新初始化,主要是每次打印的打印机不一定相同
log.info("localPrintPath:" + localPrintPath);
log.info("printService:"+printService);
for (PrintService service : printServices) { for (PrintService service : printServices) {
log.info("service:"+service.getName());
if (service.getName().contains(localPrintPath)) { if (service.getName().contains(localPrintPath)) {
log.info("printservicefind:"+service.getName());
printService = service; printService = service;
break; break;
} }
} }
if (printService == null) { if (printService == null) {
throw new ServiceException("打印机配置错误!"); throw new ServiceException("打印机配置错误!");
} }
@ -78,6 +83,7 @@ public class HwPrintUtil {
@PostConstruct @PostConstruct
public static void findPrinters() { public static void findPrinters() {
printServices = PrintServiceLookup.lookupPrintServices(null, null); printServices = PrintServiceLookup.lookupPrintServices(null, null);
log.info("printservices:"+printServices.toString());
System.out.println("Bean已初始化执行@PostConstruct注解的方法"); System.out.println("Bean已初始化执行@PostConstruct注解的方法");
} }
@ -92,7 +98,7 @@ public class HwPrintUtil {
public static void printBarcodes(String localPrintPath, String rawLabelTemplatePath, public static void printBarcodes(String localPrintPath, String rawLabelTemplatePath,
String generateRawLabelPath, List<List<PrintContentVo>> printContents) throws IOException { String generateRawLabelPath, List<List<PrintContentVo>> printContents) throws IOException {
for (List<PrintContentVo> printContentVos : printContents) { for (List<PrintContentVo> printContentVos : printContents) {
printBarCode(localPrintPath, rawLabelTemplatePath, printBarcode(localPrintPath, rawLabelTemplatePath,
generateRawLabelPath, printContentVos); generateRawLabelPath, printContentVos);
} }
} }
@ -106,7 +112,7 @@ public class HwPrintUtil {
* @param generateRawLabelPath * @param generateRawLabelPath
* @param printContentVos * @param printContentVos
*/ */
public static void printBarCode(String localPrintPath, String rawLabelTemplatePath, public static void printBarcode(String localPrintPath, String rawLabelTemplatePath,
String generateRawLabelPath, List<PrintContentVo> printContentVos) throws IOException { String generateRawLabelPath, List<PrintContentVo> printContentVos) throws IOException {
//解析参数生成打印图片 //解析参数生成打印图片
// String localPrintPath = params.get("localPrintPath"); // String localPrintPath = params.get("localPrintPath");
@ -149,7 +155,7 @@ public class HwPrintUtil {
if (printContentVo.getType() == PrintContentVo.TYPE_TEXT) { if (printContentVo.getType() == PrintContentVo.TYPE_TEXT) {
form.setField(printContentVo.getKey(), printContentVo.getValue()); form.setField(printContentVo.getKey(), printContentVo.getValue());
} else if (printContentVo.getType() == PrintContentVo.TYPE_CHECKBOX) { } else if (printContentVo.getType() == PrintContentVo.TYPE_CHECKBOX) {
setCheckBox(form, stamper, printContentVo.getKey()); // setCheckBox(form, stamper, printContentVo.getKey());
} else if (printContentVo.getType() == PrintContentVo.TYPE_QRCODE) { } else if (printContentVo.getType() == PrintContentVo.TYPE_QRCODE) {
setImage(form, stamper, printContentVo.getKey(), printContentVo.getValue()); setImage(form, stamper, printContentVo.getKey(), printContentVo.getValue());
} }
@ -177,6 +183,7 @@ public class HwPrintUtil {
doc.close(); doc.close();
return generateFile; return generateFile;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
log.error("打印条码生成PDF异常" + e); log.error("打印条码生成PDF异常" + e);
} }
return null; return null;
@ -307,8 +314,11 @@ public class HwPrintUtil {
throw new IOException("PDF打印失败:", e); throw new IOException("PDF打印失败:", e);
} }
} catch (PrinterException e) { } catch (PrinterException e) {
throw new ServiceException("打印机服务错误:" + e); throw new ServiceException("打印机服务错误:" + e + "," + e.getMessage());
} finally { } finally {
if (printService != null) {
printService = null;
}
if (document != null) { if (document != null) {
try { try {
document.close(); document.close();
@ -460,6 +470,9 @@ public class HwPrintUtil {
} catch (PrinterException e) { } catch (PrinterException e) {
throw new ServiceException("打印机服务错误:" + e); throw new ServiceException("打印机服务错误:" + e);
} finally { } finally {
if (printService != null) {
printService = null;
}
if (document != null) { if (document != null) {
try { try {
document.close(); document.close();

Loading…
Cancel
Save