|
|
@ -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();
|
|
|
|