模板重构

This commit is contained in:
余文财 2025-05-20 16:42:50 +08:00
parent c937c4823f
commit 72288c08a4
6 changed files with 93 additions and 115 deletions

View File

@ -50,9 +50,9 @@ public class ReceiptPrintRequest implements Serializable {
private Integer width = 690;
/**
* 生成的模板的高度默认为1000
* 生成的模板的高度默认为1050
*/
private Integer height = 1000;
private Integer height = 1050;
}

View File

@ -14,6 +14,7 @@ import org.jsoup.Jsoup;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.StringTemplateResolver;
@ -42,8 +43,6 @@ import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import org.thymeleaf.context.Context;
/**
* 模板服务类
*
@ -77,7 +76,7 @@ public class TemplateService {
resolver.setTemplateMode("HTML");
// 设置模板引擎使用这个自定义的模板解析器
templateEngine.setTemplateResolver(resolver);
}else {
} else {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setPrefix("classpath:/templates/");
resolver.setSuffix(".html");
@ -111,9 +110,10 @@ public class TemplateService {
/**
* 生成图片
*
* @param html html内容
*/
private BufferedImage generate(String html, int width, int height){
private BufferedImage generate(String html, int width, int height) {
try {
// 转换为xhtml
String xhtml = this.htmlToXhtml(html);
@ -132,13 +132,14 @@ public class TemplateService {
/**
* 通过JsonObject进行context内容填充
*
* @param data jsonObject
* @return context
*/
private Context getContext(JSONObject data) {
// 创建Thymeleaf上下文
Context context = new Context();
if ( data != null) {
if (data != null) {
Set<String> keys = data.keySet();
for (String key : keys) {
// 判单是否有图片生成统一后面采用的是_2base64Type
@ -163,6 +164,7 @@ public class TemplateService {
/**
* 根据内容生成二维码
*
* @param content 转换内容
*/
private String generateQRCode(int type, String content, int width, int height) {
@ -173,15 +175,16 @@ public class TemplateService {
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符编码为 UTF-8
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 设置纠错级别
hints.put(EncodeHintType.MARGIN, 1); // // 添加边距控制设置边距为1
QRCodeWriter qrCodeWriter = new QRCodeWriter();
bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
qrImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}else if (type == 2) {
} else if (type == 2) {
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
hints.put(EncodeHintType.MARGIN, 1); // // 添加边距控制设置边距为1
Code128Writer barcodeWriter = new Code128Writer();
bitMatrix = barcodeWriter.encode(content, BarcodeFormat.CODE_128, width, height, hints);
@ -206,7 +209,7 @@ public class TemplateService {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(qrImage, "png", baos);
return Base64.getEncoder().encodeToString(baos.toByteArray());
}catch (Exception e){
} catch (Exception e) {
log.error("二维码/条形码生成失败", e);
throw new RRException("二维码/条形码生成失败");
}
@ -215,6 +218,7 @@ public class TemplateService {
/**
* xhtml 转换为 Document
*
* @param xhtml xhtml
* @return document
* @throws Exception e
@ -235,6 +239,7 @@ public class TemplateService {
/**
* 转换将html转换为xhtml
*
* @param html html内容
* @return xhtml
*/
@ -262,6 +267,7 @@ public class TemplateService {
/**
* 通过document转换为图片
*
* @param document doc
* @param width 图片的宽度
* @param height 图片的高度
@ -312,15 +318,16 @@ public class TemplateService {
/**
* 校验这个模板内容是不是html字符串而非模板名称
*
* @param template template
* @return 是否是html字符串
*/
private boolean checkIsHtml(String template){
private boolean checkIsHtml(String template) {
try {
String pattern = "<(\"[^\"]*\"|'[^']*'|[^'\">])*>";
Pattern r = Pattern.compile(pattern);
return r.matcher(template).find();
}catch (Exception e) {
} catch (Exception e) {
return false;
}
}
@ -328,6 +335,7 @@ public class TemplateService {
/**
* 将document对象转换为字符串
*
* @param doc document
* @return document转换为的字符串
*/

View File

@ -2,7 +2,7 @@
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8">
<title>门诊缴费凭证</title>
<title>门诊缴费凭证T2</title>
</head>
<body>
<div style="font-size: 24px;font-weight: bold;">
@ -12,16 +12,12 @@
<div style="text-align: center; font-size: 20px;">
****<span th:text="${registeTerminalName}"></span>****
</div>
<div style="font-size: 24px;text-align: center; margin-bottom: -10px; margin-top: -10px;">
-----------------------------------------------------------------------------
</div>
<div style="border-bottom: 1px dashed #000;margin: 3px 0;"></div>
<div style="text-align: center; font-size: 32px; "><span th:text="${registeType}"></span>
</div>
<div style="font-size: 24px;text-align: center; margin-top: -15px;">
-----------------------------------------------------------------------------
<div style="border-bottom: 1px dashed #000;margin: 3px 0;"></div>
</div>
</div>
<div style="font-size: 27px;word-break: break-all;margin: 10px 20px;">
<div style="font-size: 29px;word-break: break-all;margin: 10px 20px;">
<div style="display: flex;">
<span style="margin-right: 25px;">姓名:<span th:text="${name}"></span></span>
<span style="margin-right: 25px;">性别:<span th:text="${gender}"></span></span>
@ -37,10 +33,8 @@
<div style="margin-top: 10px;" th:if="${department}">就诊科室:<span th:text="${department}"></span>
</div>
</div>
<div style="text-align: center; font-size: 24px;">
-----------------------------------------------------------------------------
</div>
<div style="font-size: 27px; margin: 10px 20px">
<div style="border-bottom: 1px dashed #000;margin: 3px 0;"></div>
<div style="font-size: 29px; margin: 10px 20px">
<div style="word-break: break-all;">
<span style="margin-right: 25px;">费用总额:<span th:text="${totalFee}"></span></span>
<span>个人支付:<span th:text="${personalPayment}"></span></span>
@ -49,18 +43,16 @@
th:text="${actualReceiptAmount}"></span></div>
<div style="margin-top: 10px;"><span>实收金额:</span><span th:text="${actualReceiptAmountChinese}"></span></div>
</div>
<div style="text-align: center; font-size: 24px;">
-----------------------------------------------------------------------------
</div>
<div style="border-bottom: 1px dashed #000;margin: 3px 0;"></div>
<div style="margin: 10px 20px">
<table style="width: 100%; table-layout: fixed; border-collapse: collapse;">
<tr style="font-size: 27px;">
<tr style="font-size: 29px;">
<th style="width: 300px;text-align: left;">项目名称</th>
<th style="width: calc(25% - 5px);text-align: center;">数量</th>
<th style="width: calc(25% - 5px);text-align: center;">单价</th>
<th style="width: calc(25% - 5px);text-align: center;">小计</th>
</tr>
<tr style="font-size: 22px;word-break: break-all;" th:each="item : ${items}">
<tr style="font-size: 24px;word-break: break-all;" th:each="item : ${items}">
<td style="text-align: left;width: 300px;" th:text="${item.name}"></td>
<td style="text-align: center;" th:text="${item.quantity}"></td>
<td style="text-align: center;" th:text="${item.unitPrice}"></td>
@ -68,17 +60,11 @@
</tr>
</table>
</div>
<div style="text-align: center; font-size: 24px;">
-----------------------------------------------------------------------------
</div>
<div style="margin-left: 20px; font-size: 27px;">
<div>终端编号:<span th:text="${terminalNumber}"></span></div>
<div>打印时间:<span th:text="${printTime}"></span></div>
</div>
<div style="margin-left: 20px; font-size: 27px; margin-top: 20px;">
<span style="margin-top: 20px;">温馨提示</span><br>
<span>1.请取走全部凭条、并妥善保管</span><br>
<span>2.如果对缴费结算存在疑问,请到人工窗口咨询</span>
<div style="border-bottom: 1px dashed #000;margin: 3px 0;"></div>
<div style="margin-left: 20px;font-size: 29px;">终端编号:<span th:text="${terminalNumber}"></span></div>
<div style="margin-left: 20px;font-size: 29px;">打印时间:<span th:text="${printTime}"></span></div>
<div style="margin-left: 20px; margin-top: 10px;font-size: 27px;">
<span>温馨提示:请取走缴费凭证,并妥善保管,如果对缴费存在疑问,请到人工窗口咨询!</span>
</div>
</div>
</body>

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>挂号单</title>
<title>住院押金预缴</title>
</head>
<body>
<div style="font-size: 24px;font-weight: bold;">
@ -11,32 +11,28 @@
<div style="text-align: center; font-size: 20px;">
****<span th:text="${registeTerminalName}"></span>****
</div>
<div style="font-size: 24px;text-align: center; margin-bottom: -10px; margin-top: -10px;">
-----------------------------------------------------------------------------
</div>
<div style="border-bottom: 1px dashed #000;margin: 3px 0;"></div>
<div style="text-align: center; font-size: 32px; "><span th:text="${registeType}"></span>
</div>
<div style="font-size: 24px;text-align: center; margin-top: -15px;">
-----------------------------------------------------------------------------
</div>
<div style="border-bottom: 1px dashed #000;margin: 3px 0;"></div>
</div>
<div style=" font-size: 29px;">
<div style="margin-left: 35px;">&emsp;&emsp;名:<span th:text="${name}"></span></div>
<div style="margin-left: 35px;">&emsp;&emsp;别:<span th:text="${gender}"></span></div>
<div style="margin-left: 35px;">&emsp;&emsp;龄:<span th:text="${age}"></span></div>
<div style="margin-left: 35px;">出生日期:<span th:text="${birthDate}"></span></div>
<div style="margin-left: 35px;">&ensp;&ensp;号:<span th:text="${outpatientNumber}"></span></div>
<div style="margin-left: 35px;">入院科室:<span th:text="${department}"></span></div>
<div style="margin-left: 35px;">&ensp;&ensp;用:<span th:text="${totalFee}"></span>&thinsp;</div>
<div style="margin-left: 35px;">支付方式:<span th:text="${paymentMethod}"></span></div>
<div style="margin-left: 35px;">交易流水:<span style="font-size: 27px;" th:text="${transactionNumber}"></span>
<div style="margin-left: 20px;">&emsp;&emsp;名:<span th:text="${name}"></span></div>
<div style="margin-left: 20px;">&emsp;&emsp;别:<span th:text="${gender}"></span></div>
<div style="margin-left: 20px;">&emsp;&emsp;龄:<span th:text="${age}"></span></div>
<div style="margin-left: 20px;">出生日期:<span th:text="${birthDate}"></span></div>
<div style="margin-left: 20px;">&ensp;&ensp;号:<span th:text="${outpatientNumber}"></span></div>
<div style="margin-left: 20px;">入院科室:<span th:text="${department}"></span></div>
<div style="margin-left: 20px;">&ensp;&ensp;用:<span th:text="${totalFee}"></span>&thinsp;</div>
<div style="margin-left: 20px;">支付方式:<span th:text="${paymentMethod}"></span></div>
<div style="margin-left: 20px;">交易流水:<span style="font-size: 27px;" th:text="${transactionNumber}"></span>
</div>
</div>
<div style=" margin-top: 8px;">
<div style="font-size: 28px;margin-left: 35px; ">备注:缴费凭证,请妥善保管!</div>
<div style="font-size: 28px;margin-left: 35px;">&ensp;&ensp;号:<span th:text="${terminalNumber}"></span>
</div>
<div style="font-size: 28px;margin-left: 35px;">打印时间:<span th:text="${printTime}"></span></div>
<div style="border-bottom: 1px dashed #000;margin: 3px 0;"></div>
<div style="margin-left: 20px;font-size: 29px;">终端编号:<span th:text="${terminalNumber}"></span></div>
<div style="margin-left: 20px;font-size: 29px;">打印时间:<span th:text="${printTime}"></span></div>
<div style="margin-left: 20px; margin-top: 10px;font-size: 27px;">
<span>温馨提示:请取走缴费凭证,并妥善保管,如果对缴费存在疑问,请到人工窗口咨询!</span>
</div>
</div>
</body>

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>门诊缴费</title>
<title>门诊缴费凭证T1</title>
</head>
<body>
<div style="font-size: 24px;font-weight: bold;">
@ -11,30 +11,26 @@
<div style="text-align: center; font-size: 20px;">
****<span th:text="${registeTerminalName}"></span>****
</div>
<div style="font-size: 24px;text-align: center; margin-bottom: -10px; margin-top: -10px;">
-----------------------------------------------------------------------------
</div>
<div style="border-bottom: 1px dashed #000;margin: 3px 0;"></div>
<div style="text-align: center; font-size: 32px; "><span th:text="${registeType}"></span>
</div>
<div style="font-size: 24px;text-align: center; margin-top: -15px;">
-----------------------------------------------------------------------------
<div style="border-bottom: 1px dashed #000;margin: 3px 0;"></div>
</div>
<div style="font-size: 29px;">
<div style="margin-left: 20px;">&emsp;&emsp;名:<span th:text="${name}"></span></div>
<div style="margin-left: 20px;">&emsp;&emsp;别:<span th:text="${gender}"></span></div>
<div style="margin-left: 20px;">&emsp;&emsp;龄:<span th:text="${age}"></span></div>
<div style="margin-left: 20px;">出生日期:<span th:text="${birthDate}"></span></div>
<div style="margin-left: 20px;">&ensp;&ensp;用:<span th:text="${totalFee}"></span>&thinsp;</div>
<div style="margin-left: 20px;">支付方式:<span th:text="${paymentMethod}"></span></div>
<div style="margin-left: 20px;">交易流水:<span style="font-size: 27px;" th:text="${transactionNumber}"></span>
</div>
</div>
<div style=" font-size: 29px;">
<div style="margin-left: 35px;">&emsp;&emsp;名:<span th:text="${name}"></span></div>
<div style="margin-left: 35px;">&emsp;&emsp;别:<span th:text="${gender}"></span></div>
<div style="margin-left: 35px;">&emsp;&emsp;龄:<span th:text="${age}"></span></div>
<div style="margin-left: 35px;">出生日期:<span th:text="${birthDate}"></span></div>
<div style="margin-left: 35px;">&ensp;&ensp;用:<span th:text="${totalFee}"></span>&thinsp;</div>
<div style="margin-left: 35px;">支付方式:<span th:text="${paymentMethod}"></span></div>
<div style="margin-left: 35px;">交易流水:<span style="font-size: 27px;" th:text="${transactionNumber}"></span>
</div>
</div>
<div style=" margin-top: 8px;">
<div style="font-size: 28px;margin-left: 35px; ">备注:缴费凭证,请妥善保管!</div>
<div style="font-size: 28px;margin-left: 35px;">&ensp;&ensp;号:<span th:text="${terminalNumber}"></span>
</div>
<div style="font-size: 28px;margin-left: 35px;">打印时间:<span th:text="${printTime}"></span></div>
<div style="border-bottom: 1px dashed #000;margin: 3px 0;"></div>
<div style="margin-left: 20px;font-size: 29px;">终端编号:<span th:text="${terminalNumber}"></span></div>
<div style="margin-left: 20px;font-size: 29px;">打印时间:<span th:text="${printTime}"></span></div>
<div style="margin-left: 20px; margin-top: 10px;font-size: 27px;">
<span>温馨提示:请取走缴费凭证,并妥善保管,如果对缴费存在疑问,请到人工窗口咨询!</span>
</div>
</div>
</body>

View File

@ -11,44 +11,36 @@
<div style="text-align: center;font-size: 20px;">
<span>****<span th:text="${registeTerminalName}"></span>****</span>
</div>
<div style="font-size: 24px;text-align: center;margin-bottom: -10px; margin-top: -10px;">
-----------------------------------------------------------------------------
</div>
<div style="border-bottom: 1px dashed #000;margin: 3px 0;"></div>
<div style="text-align: center; font-size: 32px;"><span th:text="${registeType}"></span>
</div>
<div style="font-size: 24px;text-align: center; margin-top: -15px;">
-----------------------------------------------------------------------------
</div>
<div style="border-bottom: 1px dashed #000;margin: 3px 0;"></div>
</div>
<div style="font-size: 29px;">
<div style="margin-left: 35px;">&emsp;&emsp;名:<span th:text="${name}"></span></div>
<div style="margin-left: 35px;">&emsp;&emsp;别:<span th:text="${gender}"></span></div>
<div style="margin-left: 35px;">&emsp;&emsp;龄:<span th:text="${age}"></span></div>
<div style="margin-left: 35px;">出生日期:<span th:text="${birthDate}"></span></div>
<div style="margin-left: 35px;">&ensp;&ensp;号:<span th:text="${outpatientNumber}"></span></div>
<div style="margin-left: 35px;">就诊科室:<span th:text="${department}"></span></div>
<div style="margin-left: 35px;">出诊级别:<span th:text="${visitLevel}"></span></div>
<div style="margin-left: 35px;">就诊医生:<span th:text="${doctor}"></span></div>
<div style="margin-left: 35px;">&emsp;&emsp;序:<span th:text="${sequence}"></span></div>
<div style="margin-left: 35px;">挂号日期:<span th:text="${registerDate}"></span></div>
<div style="margin-left: 35px;">&ensp;&ensp;用:<span th:text="${totalFee}"></span>&thinsp;</div>
<div style="margin-left: 35px;">支付方式:<span th:text="${paymentMethod}"></span></div>
<div style="margin-left: 35px;">交易流水:<span style="font-size: 26px;"
<div style="margin-left: 20px;">&emsp;&emsp;名:<span th:text="${name}"></span></div>
<div style="margin-left: 20px;">&emsp;&emsp;别:<span th:text="${gender}"></span></div>
<div style="margin-left: 20px;">&emsp;&emsp;龄:<span th:text="${age}"></span></div>
<div style="margin-left: 20px;">出生日期:<span th:text="${birthDate}"></span></div>
<div style="margin-left: 20px;">&ensp;&ensp;号:<span th:text="${outpatientNumber}"></span></div>
<div style="margin-left: 20px;">就诊科室:<span th:text="${department}"></span></div>
<div style="margin-left: 20px;">出诊级别:<span th:text="${visitLevel}"></span></div>
<div style="margin-left: 20px;">就诊医生:<span th:text="${doctor}"></span></div>
<div style="margin-left: 20px;">&emsp;&emsp;序:<span th:text="${sequence}"></span></div>
<div style="margin-left: 20px;">挂号日期:<span th:text="${registerDate}"></span></div>
<div style="margin-left: 20px;">&ensp;&ensp;用:<span th:text="${totalFee}"></span>&thinsp;</div>
<div style="margin-left: 20px;">支付方式:<span th:text="${paymentMethod}"></span></div>
<div style="margin-left: 20px;">交易流水:<span style="font-size: 26px;"
th:text="${transactionNumber}"></span></div>
<div style="width: 100%; text-align: center; margin-bottom: -20px;">
<img style="display: inline-block; "
th:src="${qrCodeBase64}"
alt="QR Code"/>
<div style="width:100%;text-align: center;">
<img th:src="${qrCodeBase64}"
alt="#"/>
</div>
</div>
<div style="margin-top: 3px;">
<div style="font-size: 24px;margin-top: -40px; text-align: center; ">
-----------------------------------------------------------------------------
</div>
<div style="font-size: 28px;margin-left: 35px; ">备注:凭此条退费,请妥善保管!</div>
<div style="font-size: 28px;margin-left: 35px;">&ensp;&ensp;号:<span th:text="${terminalNumber}"></span>
</div>
<div style="font-size: 28px;margin-left: 35px;">打印时间:<span th:text="${printTime}"></span></div>
<div style="border-bottom: 1px dashed #000;margin-bottom: 3px;"></div>
<div style="margin-left: 20px;font-size: 29px;">终端编号:<span th:text="${terminalNumber}"></span></div>
<div style="margin-left: 20px;font-size: 29px;">打印时间:<span th:text="${printTime}"></span></div>
<div style="margin-left: 20px; margin-top: 10px;font-size: 27px;">
<span>温馨提示:请取走挂号凭证,并妥善保管,如果对缴费存在疑问,请到人工窗口咨询!</span>
</div>
</div>
</body>