diff --git a/hw-api/hw-api-mes/src/main/java/com/hw/mes/api/domain/MesBaseBarcodeInfo.java b/hw-api/hw-api-mes/src/main/java/com/hw/mes/api/domain/MesBaseBarcodeInfo.java index cb234d51..ece11aa0 100644 --- a/hw-api/hw-api-mes/src/main/java/com/hw/mes/api/domain/MesBaseBarcodeInfo.java +++ b/hw-api/hw-api-mes/src/main/java/com/hw/mes/api/domain/MesBaseBarcodeInfo.java @@ -59,6 +59,10 @@ public class MesBaseBarcodeInfo extends BaseEntity @Excel(name = "物料ID") private Long materialId; + /** 物料名称 */ + @Excel(name = "物料ID") + private String materialName; + /** 供应商ID */ @Excel(name = "供应商ID") private Long manufacturerId; @@ -131,6 +135,14 @@ public class MesBaseBarcodeInfo extends BaseEntity @Excel(name = "绑定托盘时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss") private Date bindTime; + public String getMaterialName() { + return materialName; + } + + public void setMaterialName(String materialName) { + this.materialName = materialName; + } + public String getPrintFlag() { return printFlag; } diff --git a/hw-api/hw-api-printer/pom.xml b/hw-api/hw-api-printer/pom.xml index c6d1b0cc..0c7ba419 100644 --- a/hw-api/hw-api-printer/pom.xml +++ b/hw-api/hw-api-printer/pom.xml @@ -20,7 +20,7 @@ com.hw - hw-common-core + hw-api-mes diff --git a/hw-api/hw-api-printer/src/main/java/com/hw/printer/api/RemotePrinterService.java b/hw-api/hw-api-printer/src/main/java/com/hw/printer/api/RemotePrinterService.java new file mode 100644 index 00000000..e01f6340 --- /dev/null +++ b/hw-api/hw-api-printer/src/main/java/com/hw/printer/api/RemotePrinterService.java @@ -0,0 +1,29 @@ +package com.hw.printer.api; + +import com.hw.common.core.constant.SecurityConstants; +import com.hw.common.core.constant.ServiceNameConstants; +import com.hw.common.core.domain.R; +import com.hw.mes.api.domain.MesBaseBarcodeInfo; +import com.hw.printer.api.factory.RemotePrinterFallbackFactory; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.*; + + +@FeignClient(contextId = "remotePrinterService", value = ServiceNameConstants.PRINTER_SERVICE, fallbackFactory = RemotePrinterFallbackFactory.class) +public interface RemotePrinterService { + + /** + * 通过条码查询条码信息 + * + * @param barcodeInfo 条码信息 + * @param source 请求来源 + * @return 结果 + */ + @PostMapping("/printService/printBarCode") + public MesBaseBarcodeInfo printBarCode(@RequestBody MesBaseBarcodeInfo barcodeInfo, @RequestHeader(SecurityConstants.FROM_SOURCE) String source); + + + + + +} diff --git a/hw-api/hw-api-printer/src/main/java/com/hw/printer/api/factory/RemotePrinterFallbackFactory.java b/hw-api/hw-api-printer/src/main/java/com/hw/printer/api/factory/RemotePrinterFallbackFactory.java new file mode 100644 index 00000000..2946547a --- /dev/null +++ b/hw-api/hw-api-printer/src/main/java/com/hw/printer/api/factory/RemotePrinterFallbackFactory.java @@ -0,0 +1,32 @@ +package com.hw.printer.api.factory; + +import com.hw.common.core.domain.R; + +import com.hw.common.core.exception.ServiceException; +import com.hw.printer.api.RemotePrinterService; +import com.hw.mes.api.domain.MesBaseBarcodeInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cloud.openfeign.FallbackFactory; +import org.springframework.stereotype.Component; + +/** + * 打印机服务降级处理 + * + * @author ruoyi + */ +@Component +public class RemotePrinterFallbackFactory implements FallbackFactory { + private static final Logger log = LoggerFactory.getLogger(RemotePrinterFallbackFactory.class); + + @Override + public RemotePrinterService create(final Throwable throwable) { + log.error("打印条码服务调用失败:{}", throwable.getMessage()); + return new RemotePrinterService() { + @Override + public MesBaseBarcodeInfo printBarCode(MesBaseBarcodeInfo barcodeInfo, String source) { + throw new ServiceException("打印条码服务调用异常:" + throwable.getMessage()); + } + }; + } +} diff --git a/hw-api/hw-api-printer/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/hw-api/hw-api-printer/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..88431601 --- /dev/null +++ b/hw-api/hw-api-printer/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.hw.mes.api.factory.RemoteMesFallbackFactory diff --git a/hw-common/hw-common-core/src/main/java/com/hw/common/core/constant/ServiceNameConstants.java b/hw-common/hw-common-core/src/main/java/com/hw/common/core/constant/ServiceNameConstants.java index 0c0b5234..60006cf9 100644 --- a/hw-common/hw-common-core/src/main/java/com/hw/common/core/constant/ServiceNameConstants.java +++ b/hw-common/hw-common-core/src/main/java/com/hw/common/core/constant/ServiceNameConstants.java @@ -47,4 +47,9 @@ public class ServiceNameConstants * System下common服务的serviceid * */ public static final String SYS_COMMON_SERVICE = "hw-system"; + + /** + * 打印机服务的serviceid + * */ + public static final String PRINTER_SERVICE = "hw-printer"; } diff --git a/hw-modules/hw-mes/pom.xml b/hw-modules/hw-mes/pom.xml index 551fb952..eef78e25 100644 --- a/hw-modules/hw-mes/pom.xml +++ b/hw-modules/hw-mes/pom.xml @@ -81,6 +81,12 @@ com.hw hw-api-mes + + com.hw + hw-api-printer + 3.6.3 + compile + diff --git a/hw-modules/hw-mes/src/main/java/com/hw/mes/controller/MesBaseBarcodeInfoController.java b/hw-modules/hw-mes/src/main/java/com/hw/mes/controller/MesBaseBarcodeInfoController.java index fa96b2e3..9e0c7d4f 100644 --- a/hw-modules/hw-mes/src/main/java/com/hw/mes/controller/MesBaseBarcodeInfoController.java +++ b/hw-modules/hw-mes/src/main/java/com/hw/mes/controller/MesBaseBarcodeInfoController.java @@ -92,7 +92,6 @@ public class MesBaseBarcodeInfoController extends BaseController @PutMapping public AjaxResult edit(@RequestBody MesBaseBarcodeInfo mesBaseBarcodeInfo) { - mesBaseBarcodeInfo.setUpdateBy(SecurityUtils.getLoginUser().getUsername()); return toAjax(mesBaseBarcodeInfoService.updateMesBaseBarcodeInfo(mesBaseBarcodeInfo)); } @@ -108,6 +107,16 @@ public class MesBaseBarcodeInfoController extends BaseController } + /** + * 打印条码 + * @param barcodeIds + * @return + */ + @GetMapping(value = "/printBarCode/{barcodeIds}") + public AjaxResult printBarCode(@PathVariable Long[] barcodeIds) + { + return success(mesBaseBarcodeInfoService.printBarCode(barcodeIds)); + } /** diff --git a/hw-modules/hw-mes/src/main/java/com/hw/mes/service/IMesBaseBarcodeInfoService.java b/hw-modules/hw-mes/src/main/java/com/hw/mes/service/IMesBaseBarcodeInfoService.java index d4a70c3d..ba73a4bf 100644 --- a/hw-modules/hw-mes/src/main/java/com/hw/mes/service/IMesBaseBarcodeInfoService.java +++ b/hw-modules/hw-mes/src/main/java/com/hw/mes/service/IMesBaseBarcodeInfoService.java @@ -68,4 +68,12 @@ public interface IMesBaseBarcodeInfoService * @return 结果 */ public int deleteMesBaseBarcodeInfoByBarcodeId(Long barcodeId); + + /** + * 打印条码 + * @param barcodeIds + * @return + */ + public int printBarCode(Long[] barcodeIds); + } diff --git a/hw-modules/hw-mes/src/main/java/com/hw/mes/service/impl/MesBaseBarcodeInfoServiceImpl.java b/hw-modules/hw-mes/src/main/java/com/hw/mes/service/impl/MesBaseBarcodeInfoServiceImpl.java index e852306c..499c2a16 100644 --- a/hw-modules/hw-mes/src/main/java/com/hw/mes/service/impl/MesBaseBarcodeInfoServiceImpl.java +++ b/hw-modules/hw-mes/src/main/java/com/hw/mes/service/impl/MesBaseBarcodeInfoServiceImpl.java @@ -4,9 +4,15 @@ import java.math.BigDecimal; import java.util.List; import com.hw.common.core.constant.MesConstants; +import com.hw.common.core.constant.SecurityConstants; +import com.hw.common.core.domain.R; +import com.hw.common.core.exception.ServiceException; import com.hw.common.core.utils.DateUtils; +import com.hw.common.core.utils.StringUtils; import com.hw.common.core.utils.uuid.Seq; +import com.hw.common.security.utils.SecurityUtils; import com.hw.mes.api.domain.MesBaseBarcodeInfo; +import com.hw.printer.api.RemotePrinterService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.hw.mes.mapper.MesBaseBarcodeInfoMapper; @@ -23,6 +29,9 @@ public class MesBaseBarcodeInfoServiceImpl implements IMesBaseBarcodeInfoService @Autowired private MesBaseBarcodeInfoMapper mesBaseBarcodeInfoMapper; + @Autowired + private RemotePrinterService remotePrinterService; + /** * 查询条码信息 * @@ -94,6 +103,7 @@ public class MesBaseBarcodeInfoServiceImpl implements IMesBaseBarcodeInfoService */ @Override public int updateMesBaseBarcodeInfo(MesBaseBarcodeInfo mesBaseBarcodeInfo) { + mesBaseBarcodeInfo.setUpdateBy(SecurityUtils.getUsername()); mesBaseBarcodeInfo.setUpdateTime(DateUtils.getNowDate()); return mesBaseBarcodeInfoMapper.updateMesBaseBarcodeInfo(mesBaseBarcodeInfo); } @@ -119,4 +129,39 @@ public class MesBaseBarcodeInfoServiceImpl implements IMesBaseBarcodeInfoService public int deleteMesBaseBarcodeInfoByBarcodeId(Long barcodeId) { return mesBaseBarcodeInfoMapper.deleteMesBaseBarcodeInfoByBarcodeId(barcodeId); } + + /** + * 打印条码 + * + * @param barcodeIds + * @return + */ + @Override + public int printBarCode(Long[] barcodeIds) { + StringBuilder buffer = new StringBuilder(); + //获取条码List + for (Long barcodeId : barcodeIds) { + MesBaseBarcodeInfo mesBaseBarcodeInfo = mesBaseBarcodeInfoMapper.selectMesBaseBarcodeInfoByBarcodeId(barcodeId); + try { + //调用的打印接口 + MesBaseBarcodeInfo barCodeInfo = remotePrinterService.printBarCode(mesBaseBarcodeInfo, SecurityConstants.INNER); + MesBaseBarcodeInfo baseBarcodeInfo = new MesBaseBarcodeInfo(); + baseBarcodeInfo.setBarcodeId(barcodeId); + baseBarcodeInfo.setMachineName(barCodeInfo.getMachineName()); + baseBarcodeInfo.setPrintTime(DateUtils.getNowDate()); + baseBarcodeInfo.setPrintPerson(SecurityUtils.getUsername()); + baseBarcodeInfo.setPrintFlag("1"); + baseBarcodeInfo.setPrintNumber(StringUtils.isNull(baseBarcodeInfo.getPrintNumber()) ? 1 : baseBarcodeInfo.getPrintNumber() + 1); + baseBarcodeInfo.setAcceptedDate(DateUtils.getNowDate()); + this.updateMesBaseBarcodeInfo(baseBarcodeInfo); + } catch (Exception e) { + buffer.append("打印条码报错——条码内容:").append(mesBaseBarcodeInfo.getBarcodeInfo()).append(e.getMessage()); + } + } + if (buffer.length() == 0) { + return 1; + } else { + throw new ServiceException(buffer.toString()); + } + } } diff --git a/hw-modules/hw-mes/src/main/resources/mapper/mes/MesBaseBarcodeInfoMapper.xml b/hw-modules/hw-mes/src/main/resources/mapper/mes/MesBaseBarcodeInfoMapper.xml index c65137a7..408dfb33 100644 --- a/hw-modules/hw-mes/src/main/resources/mapper/mes/MesBaseBarcodeInfoMapper.xml +++ b/hw-modules/hw-mes/src/main/resources/mapper/mes/MesBaseBarcodeInfoMapper.xml @@ -35,81 +35,84 @@ + - select barcode_id, - print_time, - print_person, - batch_flag, - barcode_type, - barcode_info, - batch_code, - pallet_info_code, - material_id, - manufacturer_id, - amount, - machine_name, - print_number, - po_no, - production_date, - accepted_date, - last_outstock_date, - plan_code, - plan_detail_code, - sale_order_id, - saleorder_code, - project_no, - print_flag, - serial_number, - remark, - bind_status, - bind_by, - bind_time, - update_by, - update_time - from mes_base_barcode_info + select bbi.barcode_id, + bbi.print_time, + bbi.print_person, + bbi.batch_flag, + bbi.barcode_type, + bbi.barcode_info, + bbi.batch_code, + bbi.pallet_info_code, + bbi.material_id, + bmi.material_name, + bbi.manufacturer_id, + bbi.amount, + bbi.machine_name, + bbi.print_number, + bbi.po_no, + bbi.production_date, + bbi.accepted_date, + bbi.last_outstock_date, + bbi.plan_code, + bbi.plan_detail_code, + bbi.sale_order_id, + bbi.saleorder_code, + bbi.project_no, + bbi.print_flag, + bbi.serial_number, + bbi.remark, + bbi.bind_status, + bbi.bind_by, + bbi.bind_time, + bbi.update_by, + bbi.update_time + from mes_base_barcode_info bbi + left join mes_base_material_info bmi on bmi.material_id = bbi.material_id - where barcode_info = #{barcodeInfo} limit 1 + where bbi.barcode_info = #{barcodeInfo} limit 1 \ No newline at end of file diff --git a/hw-modules/hw-printer/pom.xml b/hw-modules/hw-printer/pom.xml index f8152bb8..c129ee68 100644 --- a/hw-modules/hw-printer/pom.xml +++ b/hw-modules/hw-printer/pom.xml @@ -41,8 +41,76 @@ spring-boot-starter-actuator - + + + com.hw + hw-api-mes + + + + org.apache.pdfbox + pdfbox + 2.0.1 + + + org.apache.pdfbox + fontbox + 2.0.0 + + + org.apache.pdfbox + jempbox + 1.8.11 + + + org.apache.pdfbox + xmpbox + 2.0.0 + + + org.apache.pdfbox + preflight + 2.0.0 + + + org.apache.pdfbox + pdfbox-tools + 2.0.0 + + + + + com.itextpdf + itextpdf + 5.5.5 + + + + com.itextpdf + itext-asian + 5.2.0 + + + + com.hw + hw-common-swagger + + + + + com.google.zxing + core + 3.5.1 + + + + com.google.zxing + javase + 3.5.1 + + + ${project.artifactId} diff --git a/hw-modules/hw-printer/src/main/java/com/hw/Main.java b/hw-modules/hw-printer/src/main/java/com/hw/Main.java deleted file mode 100644 index 0dcc1da8..00000000 --- a/hw-modules/hw-printer/src/main/java/com/hw/Main.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.hw; - -// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`, -// then press Enter. You can now see whitespace characters in your code. -public class Main { - public static void main(String[] args) { - // Press Alt+Enter with your caret at the highlighted text to see how - // IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); - - // Press Shift+F10 or click the green arrow button in the gutter to run the code. - for (int i = 1; i <= 5; i++) { - - // Press Shift+F9 to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing Ctrl+F8. - System.out.println("i = " + i); - } - } -} \ No newline at end of file diff --git a/hw-modules/hw-printer/src/main/java/com/hw/printer/RuoYiPrinterApplication.java b/hw-modules/hw-printer/src/main/java/com/hw/printer/RuoYiPrinterApplication.java new file mode 100644 index 00000000..0e0da931 --- /dev/null +++ b/hw-modules/hw-printer/src/main/java/com/hw/printer/RuoYiPrinterApplication.java @@ -0,0 +1,31 @@ +package com.hw.printer; + +import com.hw.common.swagger.annotation.EnableCustomSwagger2; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; + +/** + * 文件服务 + * + * @author ruoyi + */ +@EnableCustomSwagger2 +@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class }) +public class RuoYiPrinterApplication +{ + public static void main(String[] args) + { + SpringApplication.run(RuoYiPrinterApplication.class, args); + System.out.println("(♥◠‿◠)ノ゙ 文件服务模块启动成功 ლ(´ڡ`ლ)゙ \n" + + " .-------. ____ __ \n" + + " | _ _ \\ \\ \\ / / \n" + + " | ( ' ) | \\ _. / ' \n" + + " |(_ o _) / _( )_ .' \n" + + " | (_,_).' __ ___(_ o _)' \n" + + " | |\\ \\ | || |(_,_)' \n" + + " | | \\ `' /| `-' / \n" + + " | | \\ / \\ / \n" + + " ''-' `'-' `-..-' "); + } +} diff --git a/hw-modules/hw-printer/src/main/java/com/hw/printer/controller/PrinterController.java b/hw-modules/hw-printer/src/main/java/com/hw/printer/controller/PrinterController.java new file mode 100644 index 00000000..24230840 --- /dev/null +++ b/hw-modules/hw-printer/src/main/java/com/hw/printer/controller/PrinterController.java @@ -0,0 +1,42 @@ +package com.hw.printer.controller; + +import com.hw.common.core.domain.R; +import com.hw.mes.api.domain.MesBaseBarcodeInfo; +import com.hw.printer.service.IPrinterService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * 打印机请求处理 + * + * @author Yinq + */ +@RestController +@RequestMapping("/printService") +public class PrinterController { + private static final Logger log = LoggerFactory.getLogger(PrinterController.class); + + @Autowired + private IPrinterService printerService; + + /** + * 打印条码接口 + * @param barcodeInfo + * @return + */ + @PostMapping("/printBarCode") + public R printBarCode(@RequestBody MesBaseBarcodeInfo barcodeInfo) { + try { + return R.ok(printerService.printBarCode(barcodeInfo)); + } catch (Exception e) { + log.error("打印条码失败", e); + return R.fail(e.getMessage()); + } + } + +} \ No newline at end of file diff --git a/hw-modules/hw-printer/src/main/java/com/hw/printer/service/IPrinterService.java b/hw-modules/hw-printer/src/main/java/com/hw/printer/service/IPrinterService.java new file mode 100644 index 00000000..4c242b05 --- /dev/null +++ b/hw-modules/hw-printer/src/main/java/com/hw/printer/service/IPrinterService.java @@ -0,0 +1,19 @@ +package com.hw.printer.service; + +import com.hw.mes.api.domain.MesBaseBarcodeInfo; + +/** + * 打印机服务接口 + * + * @author Yinq + */ +public interface IPrinterService { + + /** + * 打印条码接口 + * @param barcodeInfo + * @return + * @throws Exception + */ + MesBaseBarcodeInfo printBarCode(MesBaseBarcodeInfo barcodeInfo) throws Exception; +} diff --git a/hw-modules/hw-printer/src/main/java/com/hw/printer/service/PrinterServiceImpl.java b/hw-modules/hw-printer/src/main/java/com/hw/printer/service/PrinterServiceImpl.java new file mode 100644 index 00000000..a2119296 --- /dev/null +++ b/hw-modules/hw-printer/src/main/java/com/hw/printer/service/PrinterServiceImpl.java @@ -0,0 +1,61 @@ +package com.hw.printer.service; + +import com.hw.common.core.utils.DateUtils; +import com.hw.common.core.utils.StringUtils; +import com.hw.mes.api.domain.MesBaseBarcodeInfo; +import com.hw.printer.utils.HwPrintUtil; +import org.springframework.stereotype.Service; +import org.springframework.beans.factory.annotation.Value; + +import java.util.HashMap; + +/** + * 打印机服务Service + * + * @author Yinq + */ +@Service +public class PrinterServiceImpl implements IPrinterService { + + /** + * 打印机映射路径 + */ + @Value("${print.path}") + public String localPrintPath; + + /** + * PDF模板路径 + */ + @Value("${print.pdfTemplatePath}") + public String pdfTemplatePath; + + /** + * 生成PDF文件路径 + */ + @Value("${print.generatePath}") + public String generatePath; + + /** + * 打印条码接口 + * + * @param barcodeInfo + * @return + * @throws Exception + */ + @Override + public MesBaseBarcodeInfo printBarCode(MesBaseBarcodeInfo barcodeInfo) throws Exception { + HashMap 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("materialName", barcodeInfo.getMaterialName()); + params.put("date", DateUtils.getDate()); + String machineName = HwPrintUtil.printBarCode(params); + barcodeInfo.setMaterialName(machineName); + return barcodeInfo; + } + + +} diff --git a/hw-modules/hw-printer/src/main/java/com/hw/printer/utils/HwPrintUtil.java b/hw-modules/hw-printer/src/main/java/com/hw/printer/utils/HwPrintUtil.java new file mode 100644 index 00000000..891dfb7d --- /dev/null +++ b/hw-modules/hw-printer/src/main/java/com/hw/printer/utils/HwPrintUtil.java @@ -0,0 +1,215 @@ +package com.hw.printer.utils; + +import com.hw.common.core.exception.ServiceException; +import com.itextpdf.text.Document; +import com.itextpdf.text.Image; +import com.itextpdf.text.pdf.*; +import org.apache.pdfbox.pdmodel.PDDocument; + +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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + + +/** + * @author wanghao + * @date 2022/4/24 14:52 + */ +@Component +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 + */ + private static PrintService[] printServices; + + /** + * 打印机服务 + */ + private static PrintService printService = null; + + /** + * 项目启动时,初始化打印机参数 + */ + @PostConstruct + public static void findPrinters() { + printServices = PrintServiceLookup.lookupPrintServices(null, null); + System.out.println("Bean已初始化,执行@PostConstruct注解的方法"); + } + + /** + * 打印方法 + * + * @param params + * @return + */ + public static String printBarCode(HashMap params) { + //解析参数生成打印图片 + localPrintPath = params.get("localPrintPath"); + pdfTemplatePath = params.get("pdfTemplatePath"); + 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 = printBarCode(barcodeInfo, batchCode, materialName, date); + try { + printPdf(outputFile); + log.info("打印条码printBarCode方法,条码内容:" + barcodeInfo); + return localPrintPath; + } catch (Exception e) { + log.error("打印条码printPdf异常:" + e); + throw new ServiceException("打印条码printPdf异常:" + e); + } + } + + + 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(); + // 修改PDF文档的PdfStamper实例,将修改后的内容输出到内存中的字节数组 + PdfStamper stamper = new PdfStamper(reader, bos); + // 动态地将变量的值填充到PDF表单相应字段 + 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(); + BarcodeQRCode pdf417 = new BarcodeQRCode(barcodeInfo, (int) width, (int) width, null); + // 生成二维码图像 + Image image128 = pdf417.getImage(); + // 条码位置 + image128.setAbsolutePosition(fieldPosition.position.getLeft(), fieldPosition.position.getBottom()); + // 获取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 printPdf(File pdfFile) throws IOException { + PDDocument document = null; + try { + document = PDDocument.load(pdfFile); + //打印的页面的大小和方向 + PageFormat pageFormat = new PageFormat(); + //设置打印方向 PORTRAIT 竖向 REVERSE_LANDSCAPE 横向 + pageFormat.setOrientation(PageFormat.PORTRAIT); + Paper paper = new Paper(); + // 纸的大小 + paper.setSize(221, 200); + // Print 区域 + paper.setImageableArea(2, -15, 221, 200); + 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(); + } + } + } + } + + /** + * 导出为金山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() { + for (PrintService service : printServices) { + System.out.println(service.getName()); + if (service.getName().contains(localPrintPath)) { + printService = service; + break; + } + } + if (printService == null) { + throw new ServiceException("打印机配置错误!"); + } + } +} diff --git a/hw-modules/hw-printer/src/main/resources/banner.txt b/hw-modules/hw-printer/src/main/resources/banner.txt index fbd45f53..8eeae464 100644 --- a/hw-modules/hw-printer/src/main/resources/banner.txt +++ b/hw-modules/hw-printer/src/main/resources/banner.txt @@ -1,10 +1,6 @@ Spring Boot Version: ${spring-boot.version} Spring Application Name: ${spring.application.name} - _ _ - (_) | | - _ __ _ _ ___ _ _ _ ______ ___ _ _ ___ | |_ ___ _ __ ___ -| '__|| | | | / _ \ | | | || ||______|/ __|| | | |/ __|| __| / _ \| '_ ` _ \ -| | | |_| || (_) || |_| || | \__ \| |_| |\__ \| |_ | __/| | | | | | -|_| \__,_| \___/ \__, ||_| |___/ \__, ||___/ \__| \___||_| |_| |_| - __/ | __/ | - |___/ |___/ \ No newline at end of file + / __ ___ __ ( ) __ __ ___ ___ __ + // ) ) // / / / / ____ // ) ) // ) ) / / // ) ) / / //___) ) // ) ) + // / / // / / / / //___/ / // / / // / / / / // // +// / / ((__( (__/ / // // / / // / / / / ((____ // \ No newline at end of file diff --git a/hw-modules/hw-printer/src/main/resources/files/Template.pdf b/hw-modules/hw-printer/src/main/resources/files/Template.pdf new file mode 100644 index 00000000..fad1ab7e Binary files /dev/null and b/hw-modules/hw-printer/src/main/resources/files/Template.pdf differ diff --git a/hw-modules/hw-printer/src/main/resources/files/generatefile.pdf b/hw-modules/hw-printer/src/main/resources/files/generatefile.pdf new file mode 100644 index 00000000..3ef6512c Binary files /dev/null and b/hw-modules/hw-printer/src/main/resources/files/generatefile.pdf differ diff --git a/hw-ui/src/views/mes/barcode/index.vue b/hw-ui/src/views/mes/barcode/index.vue index 0c862c5e..88b2c68a 100644 --- a/hw-ui/src/views/mes/barcode/index.vue +++ b/hw-ui/src/views/mes/barcode/index.vue @@ -103,7 +103,7 @@ - + @@ -542,9 +542,8 @@ export default { /** 打印条码按钮操作 */ handlePrintBarCode(row) { - const barcodeId = row.barcodeId || this.ids - printBarCode(barcodeId).then(response => { - + const barcodeIds = row.barcodeId || this.ids; + printBarCode(barcodeIds).then(response => { this.$modal.msgSuccess("打印条码成功"); }).catch(() => {