From 2986fa0c141eeed150d5a7c03f72c7d5d7a24ce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E5=A4=B4=E4=BA=BA?= <3076767823@qq.com> Date: Mon, 20 Jan 2025 14:31:21 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9AgenerateReceiptImage=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=A2=9E=E5=8A=A0=E5=9B=BE=E7=89=87=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E4=BF=9D=E5=AD=98=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/TemplateController.java | 14 +++++--- .../service/impl/TemplateService.java | 34 +++++++++++-------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/dpkj/modules/autoReplyPrint/controller/TemplateController.java b/src/main/java/com/dpkj/modules/autoReplyPrint/controller/TemplateController.java index 07eeeff..509f16d 100644 --- a/src/main/java/com/dpkj/modules/autoReplyPrint/controller/TemplateController.java +++ b/src/main/java/com/dpkj/modules/autoReplyPrint/controller/TemplateController.java @@ -23,13 +23,19 @@ public class TemplateController { @GetMapping("/template/{templateName}") - public Result testTemplate(@RequestParam Integer width, @RequestParam Integer height, String jsonData, @PathVariable String templateName) { - this.templateService.generateReceiptImage(JSONObject.parseObject(jsonData), templateName, width, height); + public Result testTemplate(@RequestParam String jsonData, + @RequestParam Integer width, + @RequestParam Integer height, + @PathVariable String templateName, + @RequestParam(defaultValue = "E:\\images") String saveDir) { + this.templateService.generateReceiptImage(JSONObject.parseObject(jsonData), templateName, width, height, saveDir); return Result.ok("模板生成成功"); } @GetMapping("/template") - public Result testTemplate(String jsonData, @RequestParam Integer width, @RequestParam Integer height) { + public Result testTemplate(@RequestParam String jsonData, + @RequestParam Integer width, + @RequestParam Integer height, @RequestParam(defaultValue = "E:\\images") String saveDir) { String html = "\n" + "\n" + "Test\n" + @@ -38,7 +44,7 @@ public class TemplateController { "

\n" + "\n" + ""; - this.templateService.generateReceiptImage(JSONObject.parseObject(jsonData), html, width, height); + this.templateService.generateReceiptImage(JSONObject.parseObject(jsonData), html, width, height, saveDir); return Result.ok("模板生成成功"); } diff --git a/src/main/java/com/dpkj/modules/autoReplyPrint/service/impl/TemplateService.java b/src/main/java/com/dpkj/modules/autoReplyPrint/service/impl/TemplateService.java index 8c5b077..834f51f 100644 --- a/src/main/java/com/dpkj/modules/autoReplyPrint/service/impl/TemplateService.java +++ b/src/main/java/com/dpkj/modules/autoReplyPrint/service/impl/TemplateService.java @@ -56,13 +56,15 @@ public class TemplateService { /** * 生成小票图片 - * @param data json数据,用来填充模板 + * + * @param data json数据,用来填充模板 * @param template 模板(html字符串或者模板名称) - * @param width 图片宽度 - * @param height 图片高度 + * @param width 图片宽度 + * @param height 图片高度 + * @param saveDir 图片的保存路径,如果为空,那么不进行图片的保存 * @return 图片字节数组 */ - public byte[] generateReceiptImage(JSONObject data, String template, int width, int height) { + public byte[] generateReceiptImage(JSONObject data, String template, int width, int height, String saveDir) { try { // 获取模板上下文 Context context = this.getContext(data); @@ -86,7 +88,17 @@ public class TemplateService { // 渲染模板 String html = templateEngine.process(template, context); - return this.generate(html, width, height); + BufferedImage image = this.generate(html, width, height); + + // 保存图片 + if (saveDir != null && !"".equals(saveDir)) { + String outputPath = saveDir + "\\genera_image_" + System.currentTimeMillis() + ".png"; + ImageIO.write(image, "PNG", new File(outputPath)); + } + + ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); + ImageIO.write(image, "PNG", byteOutputStream); + return byteOutputStream.toByteArray(); } catch (Exception e) { e.printStackTrace(); throw new RRException("图片打印失败"); @@ -98,7 +110,7 @@ public class TemplateService { * 生成图片 * @param html html内容 */ - private byte[] generate(String html, int width, int height){ + private BufferedImage generate(String html, int width, int height){ try { // 转换为xhtml String xhtml = this.htmlToXhtml(html); @@ -107,15 +119,7 @@ public class TemplateService { Document document = this.xhtmlToDocument(xhtml); // 生成图片 - BufferedImage image = createImageToDocument(document, width, height); - - // 保存图片 - String outputPath = "E:\\images\\test_" + System.currentTimeMillis() + ".png"; - ImageIO.write(image, "PNG", new File(outputPath)); - - ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); - ImageIO.write(image, "PNG", byteOutputStream); - return byteOutputStream.toByteArray(); + return createImageToDocument(document, width, height); } catch (Exception e) { e.printStackTrace(); throw new RRException("图片打印失败");