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

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

@ -17,23 +17,23 @@ public interface IPrinterService {
/**
*
*
* @param printContents
* @param printContentVos
* @param ipAddress IP
* @return
* @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
* @return
* @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.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<List<PrintContentVo>> printContents,String ipAddress) throws Exception {
public void printBarcodes(List<PrintContentVo> printContentVos,String ipAddress) throws Exception {
// HashMap<String, String> 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<List<PrintContentVo>> printContents,String ipAddress) throws Exception {
public void printProductBarcodes(List<PrintContentVo> printContentVos,String ipAddress) throws Exception {
// HashMap<String, String> 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<String, String> params,String ipAddress) throws IOException {
log.info("ipaddress:"+ipAddress);
HwPrintUtil.printLocationQRCode(ipAddress, locationLabelTemplatePath,
generateLocationLabelPath, params);
}

@ -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<List<PrintContentVo>> printContents) throws IOException {
for (List<PrintContentVo> 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<PrintContentVo> 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();

Loading…
Cancel
Save