|
|
|
@ -15,6 +15,7 @@ import javax.print.*;
|
|
|
|
|
import java.awt.print.*;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
@ -30,20 +31,6 @@ public class HwPrintUtil {
|
|
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(HwPrintUtil.class);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 打印机映射路径
|
|
|
|
|
*/
|
|
|
|
|
public static String localPrintPath;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* PDF模板路径
|
|
|
|
|
*/
|
|
|
|
|
public static String pdfTemplatePath;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成PDF文件路径
|
|
|
|
|
*/
|
|
|
|
|
public static String generatePath;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 打印机List
|
|
|
|
@ -55,6 +42,33 @@ public class HwPrintUtil {
|
|
|
|
|
*/
|
|
|
|
|
private static PrintService printService = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 导出为金山PDF
|
|
|
|
|
* 导出为WPS PDF
|
|
|
|
|
* 发送至 OneNote 2013
|
|
|
|
|
* Microsoft XPS Document Writer
|
|
|
|
|
* Microsoft Print to PDF
|
|
|
|
|
* Fax
|
|
|
|
|
* \\10.11.43.159\HP LaserJet MFP M129-M134
|
|
|
|
|
* \\10.11.43.159\HP Laser NS 1020
|
|
|
|
|
*/
|
|
|
|
|
public static void initPrintService(String localPrintPath) {
|
|
|
|
|
// localPrintPath = "导出";
|
|
|
|
|
// localPrintPath = "ZD";
|
|
|
|
|
for (PrintService service : printServices) {
|
|
|
|
|
System.out.println(service.getName());
|
|
|
|
|
if (service.getName().contains(localPrintPath)) {
|
|
|
|
|
|
|
|
|
|
printService = service;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (printService == null) {
|
|
|
|
|
throw new ServiceException("打印机配置错误!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 项目启动时,初始化打印机参数
|
|
|
|
|
*/
|
|
|
|
@ -72,15 +86,15 @@ public class HwPrintUtil {
|
|
|
|
|
*/
|
|
|
|
|
public static String printBarCode(HashMap<String, String> params) {
|
|
|
|
|
//解析参数生成打印图片
|
|
|
|
|
localPrintPath = params.get("localPrintPath");
|
|
|
|
|
pdfTemplatePath = params.get("pdfTemplatePath");
|
|
|
|
|
generatePath = params.get("generatePath");
|
|
|
|
|
String localPrintPath = params.get("localPrintPath");
|
|
|
|
|
String pdfTemplatePath = params.get("pdfTemplatePath");
|
|
|
|
|
String generatePath = params.get("generatePath");
|
|
|
|
|
String barcodeInfo = params.get("barcodeInfo");
|
|
|
|
|
String batchCode = params.get("batchCode");
|
|
|
|
|
String materialName = params.get("materialName");
|
|
|
|
|
String date = params.get("date");
|
|
|
|
|
initPrintService();
|
|
|
|
|
File outputFile = printQRCode(barcodeInfo, batchCode, materialName, date);
|
|
|
|
|
initPrintService(localPrintPath);
|
|
|
|
|
File outputFile = printQRCode(barcodeInfo, batchCode, materialName, date,pdfTemplatePath,generatePath);
|
|
|
|
|
try {
|
|
|
|
|
printPdf(outputFile);
|
|
|
|
|
log.info("打印条码printBarCode方法,条码内容:" + barcodeInfo);
|
|
|
|
@ -100,7 +114,7 @@ public class HwPrintUtil {
|
|
|
|
|
* @param date 日期
|
|
|
|
|
* @return 生成的PDF文件
|
|
|
|
|
*/
|
|
|
|
|
public static File printQRCode(String barcodeInfo, String batchCode, String materialName, String date) {
|
|
|
|
|
public static File printQRCode(String barcodeInfo, String batchCode, String materialName, String date,String pdfTemplatePath,String generatePath) {
|
|
|
|
|
try {
|
|
|
|
|
// 生成PDF文件
|
|
|
|
|
File generateFile = new File(generatePath);
|
|
|
|
@ -160,7 +174,7 @@ public class HwPrintUtil {
|
|
|
|
|
* @param date 日期
|
|
|
|
|
* @return 生成的PDF文件
|
|
|
|
|
*/
|
|
|
|
|
public static File printBarCode(String barcodeInfo, String batchCode, String materialName, String date) {
|
|
|
|
|
public static File printBarCode(String barcodeInfo, String batchCode, String materialName, String date,String pdfTemplatePath,String generatePath) {
|
|
|
|
|
try {
|
|
|
|
|
// 生成PDF文件
|
|
|
|
|
File generateFile = new File(generatePath);
|
|
|
|
@ -256,26 +270,166 @@ public class HwPrintUtil {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 导出为金山PDF
|
|
|
|
|
* 导出为WPS PDF
|
|
|
|
|
* 发送至 OneNote 2013
|
|
|
|
|
* Microsoft XPS Document Writer
|
|
|
|
|
* Microsoft Print to PDF
|
|
|
|
|
* Fax
|
|
|
|
|
* \\10.11.43.159\HP LaserJet MFP M129-M134
|
|
|
|
|
* \\10.11.43.159\HP Laser NS 1020
|
|
|
|
|
* 打印方法
|
|
|
|
|
*
|
|
|
|
|
* @param params
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static void initPrintService() {
|
|
|
|
|
for (PrintService service : printServices) {
|
|
|
|
|
System.out.println(service.getName());
|
|
|
|
|
if (service.getName().contains(localPrintPath)) {
|
|
|
|
|
printService = service;
|
|
|
|
|
break;
|
|
|
|
|
public static String printLocationQRCode(String localPrintPath, String locationLabelTemplatePath,
|
|
|
|
|
String generateLocationLabelPath,
|
|
|
|
|
HashMap<String, String> params) {
|
|
|
|
|
//解析参数生成打印图片
|
|
|
|
|
String qrCode = params.get("qrCode");
|
|
|
|
|
initPrintService(localPrintPath);
|
|
|
|
|
File outputFile = printLocationLabelContent(params,locationLabelTemplatePath,generateLocationLabelPath);
|
|
|
|
|
try {
|
|
|
|
|
printQRPdf(outputFile);
|
|
|
|
|
log.info("打印条码,条码内容:" + qrCode);
|
|
|
|
|
return localPrintPath;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("打印条码[" + qrCode + "]异常:" + e);
|
|
|
|
|
throw new ServiceException("打印条码[" + qrCode + "]异常:" + e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (printService == null) {
|
|
|
|
|
throw new ServiceException("打印机配置错误!");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 打印二维码到文件
|
|
|
|
|
*
|
|
|
|
|
* @param params 二维码信息
|
|
|
|
|
* @return 生成的文件
|
|
|
|
|
*/
|
|
|
|
|
public static File printLocationLabelContent(HashMap<String, String> params,String locationLabelTemplatePath,String generateLocationLabelPath) {
|
|
|
|
|
try {
|
|
|
|
|
// 生成PDF文件
|
|
|
|
|
File generateFile = new File(generateLocationLabelPath);
|
|
|
|
|
FileOutputStream out = new FileOutputStream(generateFile);
|
|
|
|
|
// 打印模板PDF
|
|
|
|
|
PdfReader reader = new PdfReader(locationLabelTemplatePath);
|
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
|
// 修改PDF文档的PdfStamper实例,将修改后的内容输出到内存中的字节数组
|
|
|
|
|
PdfStamper stamper = new PdfStamper(reader, bos);
|
|
|
|
|
// 动态地将变量的值填充到PDF表单相应字段
|
|
|
|
|
AcroFields form = stamper.getAcroFields();
|
|
|
|
|
|
|
|
|
|
form.setField("text", params.get("text"));
|
|
|
|
|
|
|
|
|
|
String qrCode = params.get("qrCode");
|
|
|
|
|
//获取位置(左上右下)
|
|
|
|
|
AcroFields.FieldPosition fieldPosition = form.getFieldPositions("qrcode").get(0);
|
|
|
|
|
// 绘制二维码
|
|
|
|
|
float width = fieldPosition.position.getRight() - fieldPosition.position.getLeft();
|
|
|
|
|
float height = fieldPosition.position.getTop() - fieldPosition.position.getBottom();
|
|
|
|
|
BarcodeQRCode pdf417 = new BarcodeQRCode(qrCode, (int) width, (int) width, null);
|
|
|
|
|
// 生成二维码图像
|
|
|
|
|
Image image128 = pdf417.getImage();
|
|
|
|
|
//而PDF的标准单位是点(pt)。1点等于1/72英寸,1英寸等于2.54厘米,大约1点等于0.03527厘米
|
|
|
|
|
// System.out.println("left:" + fieldPosition.position.getLeft()
|
|
|
|
|
// + "right:" + fieldPosition.position.getRight()
|
|
|
|
|
// + "top:" + fieldPosition.position.getTop()
|
|
|
|
|
// + "bottom:" + fieldPosition.position.getBottom()
|
|
|
|
|
// + "width:" + width
|
|
|
|
|
// + "height:" + height
|
|
|
|
|
//
|
|
|
|
|
// );
|
|
|
|
|
// 条码位置
|
|
|
|
|
image128.setAbsolutePosition(fieldPosition.position.getLeft(),
|
|
|
|
|
fieldPosition.position.getBottom()-15);
|
|
|
|
|
// 获取PDF的第一页
|
|
|
|
|
PdfContentByte cb = stamper.getOverContent(1);
|
|
|
|
|
cb.addImage(image128);
|
|
|
|
|
// 设置表单不可编辑(即将表单字段内容固定到PDF中)
|
|
|
|
|
stamper.setFormFlattening(true);
|
|
|
|
|
// 关闭PdfStamper
|
|
|
|
|
stamper.close();
|
|
|
|
|
// 创建一个新的文档对象
|
|
|
|
|
Document doc = new Document();
|
|
|
|
|
// 创建 PdfCopy 对象,关联到输出流
|
|
|
|
|
PdfCopy copy = new PdfCopy(doc, out);
|
|
|
|
|
// 打开文档
|
|
|
|
|
doc.open();
|
|
|
|
|
// 从内存中的字节数组创建一个 PdfImportedPage 对象
|
|
|
|
|
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
|
|
|
|
|
// 将导入的页面添加到新文档中
|
|
|
|
|
copy.addPage(importPage);
|
|
|
|
|
// 关闭文档
|
|
|
|
|
doc.close();
|
|
|
|
|
return generateFile;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("打印条码生成PDF异常:" + e);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 打印PDF
|
|
|
|
|
*
|
|
|
|
|
* @param pdfFile 要打印的PDF文件
|
|
|
|
|
* @throws IOException 如果PDF加载或打印失败
|
|
|
|
|
*/
|
|
|
|
|
private static void printQRPdf(File pdfFile) throws IOException {
|
|
|
|
|
PDDocument document = null;
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
// 定义毫米到英寸的转换系数
|
|
|
|
|
double mmToInch = 1 / 25.4;
|
|
|
|
|
|
|
|
|
|
// 定义英寸到点的转换系数
|
|
|
|
|
double inchToPoint = 72;
|
|
|
|
|
|
|
|
|
|
// 定义纸张尺寸(单位:毫米)
|
|
|
|
|
double paperWidthMm = 40;
|
|
|
|
|
double paperHeightMm = 30;
|
|
|
|
|
|
|
|
|
|
// 将纸张尺寸转换为点
|
|
|
|
|
double paperWidthPoints = paperWidthMm * mmToInch * inchToPoint;
|
|
|
|
|
double paperHeightPoints = paperHeightMm * mmToInch * inchToPoint;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
document = PDDocument.load(pdfFile);
|
|
|
|
|
//打印的页面的大小和方向
|
|
|
|
|
PageFormat pageFormat = new PageFormat();
|
|
|
|
|
//设置打印方向 PORTRAIT 竖向 REVERSE_LANDSCAPE 横向
|
|
|
|
|
pageFormat.setOrientation(PageFormat.PORTRAIT);
|
|
|
|
|
Paper paper = new Paper();
|
|
|
|
|
// 纸的大小
|
|
|
|
|
paper.setSize(paperWidthPoints, paperHeightPoints);
|
|
|
|
|
// Print 区域
|
|
|
|
|
paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
|
|
|
|
|
pageFormat.setPaper(paper);
|
|
|
|
|
Book book = new Book();
|
|
|
|
|
PDFPrintable printable = new PDFPrintable(document, Scaling.SCALE_TO_FIT);
|
|
|
|
|
book.append(printable, pageFormat, document.getNumberOfPages());
|
|
|
|
|
PrinterJob job = PrinterJob.getPrinterJob();
|
|
|
|
|
job.setPrintService(printService);
|
|
|
|
|
job.setPageable(book);
|
|
|
|
|
job.defaultPage();
|
|
|
|
|
try {
|
|
|
|
|
job.print();
|
|
|
|
|
} catch (PrinterException e) {
|
|
|
|
|
throw new IOException("PDF打印失败:", e);
|
|
|
|
|
}
|
|
|
|
|
} catch (PrinterException e) {
|
|
|
|
|
throw new ServiceException("打印机服务错误:" + e);
|
|
|
|
|
} finally {
|
|
|
|
|
if (document != null) {
|
|
|
|
|
try {
|
|
|
|
|
document.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// 处理关闭文档时可能出现的异常
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|