change - 添加打印机服务条形码逻辑

master
yinq 8 months ago
parent 26b2a5131f
commit 4b3bf2e610

@ -53,7 +53,7 @@ public class PrinterServiceImpl implements IPrinterService {
params.put("materialName", barcodeInfo.getMaterialName());
params.put("date", DateUtils.getDate());
String machineName = HwPrintUtil.printBarCode(params);
barcodeInfo.setMaterialName(machineName);
barcodeInfo.setMachineName(machineName);
return barcodeInfo;
}

@ -80,7 +80,7 @@ public class HwPrintUtil {
String materialName = params.get("materialName");
String date = params.get("date");
initPrintService();
File outputFile = printBarCode(barcodeInfo, batchCode, materialName, date);
File outputFile = printQRCode(barcodeInfo, batchCode, materialName, date);
try {
printPdf(outputFile);
log.info("打印条码printBarCode方法,条码内容:" + barcodeInfo);
@ -91,8 +91,16 @@ public class HwPrintUtil {
}
}
public static File printBarCode(String barcodeInfo, String batchCode, String materialName, String date) {
/**
* PDF
*
* @param barcodeInfo
* @param batchCode
* @param materialName
* @param date
* @return PDF
*/
public static File printQRCode(String barcodeInfo, String batchCode, String materialName, String date) {
try {
// 生成PDF文件
File generateFile = new File(generatePath);
@ -143,6 +151,64 @@ public class HwPrintUtil {
return null;
}
/**
* PDF
*
* @param barcodeInfo
* @param batchCode
* @param materialName
* @param date
* @return PDF
*/
public static File printBarCode(String barcodeInfo, String batchCode, String materialName, String date) {
try {
// 生成PDF文件
File generateFile = new File(generatePath);
FileOutputStream out = new FileOutputStream(generateFile);
// 加载PDF模板
PdfReader reader = new PdfReader(pdfTemplatePath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// 创建PdfStamper以修改PDF并将输出到字节数组
PdfStamper stamper = new PdfStamper(reader, bos);
// 动态填充表单字段
AcroFields form = stamper.getAcroFields();
form.setField("code", barcodeInfo);
form.setField("name", materialName);
form.setField("spe", batchCode);
form.setField("data", date);
// 获取条形码字段的位置
AcroFields.FieldPosition fieldPosition = form.getFieldPositions("qrcode").get(0);
// 绘制条形码
float width = fieldPosition.position.getRight() - fieldPosition.position.getLeft();
Barcode39 barcode39 = new Barcode39();
barcode39.setCode(barcodeInfo);
barcode39.setBarHeight(50f); // 根据需要调整条形码大小
Image barcodeImage = barcode39.createImageWithBarcode(stamper.getOverContent(1), null, null);
barcodeImage.setAbsolutePosition(fieldPosition.position.getLeft(), fieldPosition.position.getBottom());
// 将条形码图像添加到第一页
stamper.getOverContent(1).addImage(barcodeImage);
// 将表单平铺(使其不可编辑)
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) {
e.printStackTrace();
}
return null;
}
/**
* PDF

Loading…
Cancel
Save