fix:修改模板渲染

This commit is contained in:
石崇礼 2025-03-19 12:33:35 +08:00
parent da5c10b632
commit df17c286fe
4 changed files with 49 additions and 47 deletions

View File

@ -145,8 +145,8 @@ public class TemplateUtils {
String[] split = key.split("_"); String[] split = key.split("_");
if (split.length > 1 && split[1].equals("2base64Type")) { if (split.length > 1 && split[1].equals("2base64Type")) {
int type = split.length > 2 ? Integer.parseInt(split[2]) : 1; int type = split.length > 2 ? Integer.parseInt(split[2]) : 1;
int width = split.length > 3 ? Integer.parseInt(split[3]) : 100; int width = split.length > 2 ? Integer.parseInt(split[3]) : 100;
int height = split.length > 4 ? Integer.parseInt(split[4]) : 100; int height = split.length > 3 ? Integer.parseInt(split[4]) : 100;
// 如果是图片类型,需要进行base64转换 // 如果是图片类型,需要进行base64转换
String base64 = this.generateQRCode(type, String.valueOf(data.get(key)), width, height); String base64 = this.generateQRCode(type, String.valueOf(data.get(key)), width, height);
context.setVariable(split[0], "data:image/jpeg;base64," + base64); context.setVariable(split[0], "data:image/jpeg;base64," + base64);
@ -161,33 +161,30 @@ public class TemplateUtils {
} }
/**
* 根据内容生成二维码
* @param content 转换内容
*/
private String generateQRCode(int type, String content, int width, int height) { private String generateQRCode(int type, String content, int width, int height) {
try { try {
BufferedImage qrImage = null; BufferedImage qrImage = null;
BitMatrix bitMatrix = null; BitMatrix bitMatrix = null;
if (type == 1) { Map<EncodeHintType, Object> hints = new HashMap<>();
Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符编码为 UTF-8
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符编码为 UTF-8 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 设置纠错级别
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 设置纠错级别 // 设置外边距为 0 以去除白边
hints.put(EncodeHintType.MARGIN, 0);
if (type == 1) {
QRCodeWriter qrCodeWriter = new QRCodeWriter(); QRCodeWriter qrCodeWriter = new QRCodeWriter();
bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints); bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
qrImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 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);
Code128Writer barcodeWriter = new Code128Writer(); Code128Writer barcodeWriter = new Code128Writer();
bitMatrix = barcodeWriter.encode(content, BarcodeFormat.CODE_128, width, height, hints); bitMatrix = barcodeWriter.encode(content, BarcodeFormat.CODE_128, width, height, hints);
qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix); qrImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
} }
if ( qrImage == null ){
throw new RRException("图片渲染失败");
}
qrImage.createGraphics(); qrImage.createGraphics();
Graphics2D graphics = (Graphics2D) qrImage.getGraphics(); Graphics2D graphics = (Graphics2D) qrImage.getGraphics();
@ -206,7 +203,7 @@ public class TemplateUtils {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(qrImage, "png", baos); ImageIO.write(qrImage, "png", baos);
return Base64.getEncoder().encodeToString(baos.toByteArray()); return Base64.getEncoder().encodeToString(baos.toByteArray());
}catch (Exception e){ } catch (Exception e) {
log.error("二维码/条形码生成失败", e); log.error("二维码/条形码生成失败", e);
throw new RRException("二维码/条形码生成失败"); throw new RRException("二维码/条形码生成失败");
} }

View File

@ -10,6 +10,8 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
/** /**
* 挂号controller * 挂号controller
@ -39,10 +41,15 @@ public class RegisterController {
* 挂号,通过文件路径 * 挂号,通过文件路径
*/ */
@PostMapping("/print") @PostMapping("/print")
public Result<LexMarkResultDTO> registerByFilePath(@RequestBody @Validated ReceiptPrintRequest request){ public Result<LexMarkResultDTO> registerByFilePath(@RequestBody @Validated ReceiptPrintRequest request) throws UnsupportedEncodingException {
String testData = "{\"hospitalName\":\"澜沧县中医医院\",\"registeTerminalName\":\"中国农业银行自助终端\",\"registeType\":\"自助挂号\",\"name\":\"刘博雅\",\"gender\":\"\",\"age\":28,\"birthDate\":\"1996-06-31\",\"cardNumber\":\"6221**********0731\",\"outpatientNumber\":\"2501150038\",\"department\":\"普外科门诊\",\"visitLevel\":\"普通号\",\"doctor\":\"普通门诊\",\"sequence\":\"1\",\"registerDate\":\"2025-01-15\",\"totalFee\":4.00,\"paymentMethod\":\"微信扫码支付\",\"orderNumber\":\"\",\"transactionNumber\":\"2025011513090412092794szztzzj\",\"qrCodeBase64_2base64Type_1_250_250\":\"maby this is a Base64 code data if has\",\"terminalNumber\":\"12092794\",\"printTime\":\"2025-01-15 13:10:08\"}"; // String testData = "{\"hospitalName\":\"澜沧县中医医院\",\"registeTerminalName\":\"中国农业银行自助终端\",\"registeType\":\"自助挂号\",\"name\":\"刘博雅\",\"gender\":\"\",\"age\":28,\"birthDate\":\"1996-06-31\",\"cardNumber\":\"6221**********0731\",\"outpatientNumber\":\"2501150038\",\"department\":\"普外科门诊\",\"visitLevel\":\"普通号\",\"doctor\":\"普通门诊\",\"sequence\":\"1\",\"registerDate\":\"2025-01-15\",\"totalFee\":4.00,\"paymentMethod\":\"微信扫码支付\",\"orderNumber\":\"\",\"transactionNumber\":\"2025011513090412092794szztzzj\",\"qrCodeBase64_2base64Type_1_250_250\":\"maby this is a Base64 code data if has\",\"terminalNumber\":\"12092794\",\"printTime\":\"2025-01-15 13:10:08\"}";
// String testData = "{\"hospitalName\":\"澜沧县中医医院\",\"registeTerminalName\":\"中国农业银行自助终端\",\"registeType\":\"自助挂号\",\"name\":\"刘博雅\",\"gender\":\"\",\"age\":28,\"birthDate\":\"1996-06-31\",\"cardNumber\":\"6221**********0731\",\"outpatientNumber\":\"2501150038\",\"department\":\"普外科门诊\",\"visitLevel\":\"普通号\",\"doctor\":\"普通门诊\",\"sequence\":\"1\",\"registerDate\":\"2025-01-15\",\"totalFee\":4.00,\"paymentMethod\":\"微信扫码支付\",\"orderNumber\":\"\",\"transactionNumber\":\"2025011513090412092794szztzzj\",\"qrCodeBase64_2base64Type_2_500_160\":\"maby this is a Base64 code data if has\",\"terminalNumber\":\"12092794\",\"printTime\":\"2025-01-15 13:10:08\"}"; // String testData = "{\"hospitalName\":\"澜沧县中医医院\",\"registeTerminalName\":\"中国农业银行自助终端\",\"registeType\":\"自助挂号\",\"name\":\"刘博雅\",\"gender\":\"\",\"age\":28,\"birthDate\":\"1996-06-31\",\"cardNumber\":\"6221**********0731\",\"outpatientNumber\":\"2501150038\",\"department\":\"普外科门诊\",\"visitLevel\":\"普通号\",\"doctor\":\"普通门诊\",\"sequence\":\"1\",\"registerDate\":\"2025-01-15\",\"totalFee\":4.00,\"paymentMethod\":\"微信扫码支付\",\"orderNumber\":\"\",\"transactionNumber\":\"2025011513090412092794szztzzj\",\"qrCodeBase64_2base64Type_2_500_160\":\"maby this is a Base64 code data if has\",\"terminalNumber\":\"12092794\",\"printTime\":\"2025-01-15 13:10:08\"}";
return Result.ok((LexMarkResultDTO)printService.printImage(JSONObject.parseObject(testData), request.getTemplateName(), request.getWidth(), request.getHeight(), request.getFileDir())); return Result.ok((LexMarkResultDTO)printService.printImage(
JSONObject.parseObject(URLDecoder.decode(request.getTemplateFillData(), "UTF-8")),
request.getTemplateName(),
request.getWidth(),
request.getHeight(),
request.getFileDir()));
} }
/** /**

View File

@ -53,17 +53,15 @@ public class RegisterServiceImpl implements PrintService {
param.put("alignment", 0); // 对齐方式 param.put("alignment", 0); // 对齐方式
param.put("offsetX", 0); // x param.put("offsetX", 0); // x
param.put("offsetY", 0); // y param.put("offsetY", 0); // y
param.put("resolution", 1); // param.put("resolution", 8); //
param.put("mediaCtrl", 1); param.put("mediaCtrl", 1);
param.put("fields", "LOGO=" + filePath); param.put("fields", "LOGO=" + filePath);
lexMarkDTO.setParam(param.toJSONString()); lexMarkDTO.setParam(param.toJSONString());
LexMarkResultDTO<LexMarkResultDTO.Param> paramLexMarkResultDTO = thirdService.callDevice(lexMarkDTO, LexMarkResultDTO.Param.class); LexMarkResultDTO<LexMarkResultDTO.Param> paramLexMarkResultDTO = thirdService.callDevice(lexMarkDTO, LexMarkResultDTO.Param.class);
// 切纸
//this.thirdService.cutPaper("ReceiptPrinter", "ControlMedia", 4);
File file = new File(filePath.toString()); File file = new File(filePath.toString());
// 检查文件是否存在 // 检查文件是否存在
if (file.exists()) { if (file.exists() ) {
// 尝试删除文件 // 尝试删除文件
if (file.delete()) { if (file.delete()) {
log.info("文件删除成功: " + filePath); log.info("文件删除成功: " + filePath);

View File

@ -4,39 +4,39 @@
<title>挂号单</title> <title>挂号单</title>
</head> </head>
<body> <body>
<div style="font-size: 32px; margin-top: 5px;"> <div style="font-size: 32px; margin-top: 20px;">
<div style=""> <div style="">
<div style="text-align: center; font-size: 40px; font-weight: 700;"><span th:text="${hospitalName}"></span></div> <div style="text-align: center; font-size: 40px; font-weight: 700;"><span th:text="${hospitalName}"></span></div>
<div style="text-align: center; font-weight: 700; font-size: 28px;">****<span th:text="${registeTerminalName}"></span></span>****</div> <div style="text-align: center; font-weight: 700; font-size: 28px;">****<span th:text="${registeTerminalName}"></span></span>****</div>
<div style="text-align: center; font-weight: 700; margin-bottom: -5px;">-----------------------------------------------------------------------------</div> <div style="text-align: center; font-weight: 700; margin-bottom: -10px; margin-top: -10px;">-----------------------------------------------------------------------------</div>
<div style="text-align: center; font-size: 40px; font-weight: 700;"><span th:text="${registeType}"></span></div> <div style="text-align: center; font-size: 40px; font-weight: 700;"><span th:text="${registeType}"></span></div>
<div style="text-align: center; font-weight: 700; margin-top: -7px;">-----------------------------------------------------------------------------</div> <div style="text-align: center; font-weight: 700; margin-top: -15px;">-----------------------------------------------------------------------------</div>
</div> </div>
<div style="font-weight: 700; font-size: 32px;"> <div style="font-weight: 700; font-size: 38px;">
<div style="margin-left: 59px;">&emsp;&emsp;名:<span th:text="${name}"></span></div> <div style="margin-left: 35px;">&emsp;&emsp;名:<span th:text="${name}"></span></div>
<div style="margin-left: 59px;">&emsp;&emsp;别:<span th:text="${gender}"></span></div> <div style="margin-left: 35px;">&emsp;&emsp;别:<span th:text="${gender}"></span></div>
<div style="margin-left: 59px;">&emsp;&emsp;龄:<span th:text="${age}"></span></div> <div style="margin-left: 35px;">&emsp;&emsp;龄:<span th:text="${age}"></span></div>
<div style="margin-left: 59px;">出生日期:<span th:text="${birthDate}"></span></div> <div style="margin-left: 35px;">出生日期:<span th:text="${birthDate}"></span></div>
<!-- <div style="margin-left: 59px;">卡&emsp;&emsp;号:<span th:text="${cardNumber}"></span></div>--> <!-- <div style="margin-left: 35px;">卡&emsp;&emsp;号:<span th:text="${cardNumber}"></span></div>-->
<div style="margin-left: 59px;">&ensp;&ensp;号:<span th:text="${outpatientNumber}"></span></div> <div style="margin-left: 35px;">&ensp;&ensp;号:<span th:text="${outpatientNumber}"></span></div>
<div style="margin-left: 59px;">就诊科室:<span th:text="${department}"></span></div> <div style="margin-left: 35px;">就诊科室:<span th:text="${department}"></span></div>
<div style="margin-left: 59px;">出诊级别:<span th:text="${visitLevel}"></span></div> <div style="margin-left: 35px;">出诊级别:<span th:text="${visitLevel}"></span></div>
<div style="margin-left: 59px;">就诊医生:<span th:text="${doctor}"></span></div> <div style="margin-left: 35px;">就诊医生:<span th:text="${doctor}"></span></div>
<div style="margin-left: 59px;">&emsp;&emsp;序:<span th:text="${sequence}"></span></div> <div style="margin-left: 35px;">&emsp;&emsp;序:<span th:text="${sequence}"></span></div>
<div style="margin-left: 59px;">挂号日期:<span th:text="${registerDate}"></span></div> <div style="margin-left: 35px;">挂号日期:<span th:text="${registerDate}"></span></div>
<div style="margin-left: 59px; font-size: 46px; ">&ensp;&ensp;用:<span th:text="${totalFee}"></span>&thinsp;</div> <div style="margin-left: 35px;">&ensp;&ensp;用:<span th:text="${totalFee}"></span>&thinsp;</div>
<div style="margin-left: 59px;">支付方式:<span th:text="${paymentMethod}"></span></div> <div style="margin-left: 35px;">支付方式:<span th:text="${paymentMethod}"></span></div>
<div style="margin-left: 59px;">&ensp;&ensp;号:<span th:text="${orderNumber}"></span></div> <div style="margin-left: 35px;">&ensp;&ensp;号:<span style="font-size: 32px;" th:text="${orderNumber}"></span></div>
<div style="margin-left: 59px; ">交易流水:<span th:text="${transactionNumber}"></span></div> <div style="margin-left: 35px;">交易流水:<span style="font-size: 32px;" th:text="${transactionNumber}"></span></div>
<div style="width: 100%; text-align: center; margin-bottom: -20px;"> <div style="width: 100%; text-align: center; margin-bottom: -20px;">
<img style="display: inline-block; " th:src="${qrCodeBase64}" alt="QR Code"/> <img style="display: inline-block; " th:src="${qrCodeBase64}" alt="QR Code"/>
</div> </div>
</div> </div>
<div style="font-weight: 700; margin-top: -20px;"> <div style="font-weight: 700; margin-top: 8px;">
<div style="margin-top: -40px; text-align: center; ">-----------------------------------------------------------------------------</div> <div style="margin-top: -40px; text-align: center; ">-----------------------------------------------------------------------------</div>
<div style="font-size: 32px;margin-left: 59px; ">备注:凭此条退费,请妥善保管!</div> <div style="font-size: 32px;margin-left: 35px; ">备注:凭此条退费,请妥善保管!</div>
<div style="font-size: 32px;margin-left: 59px;">&ensp;&ensp;号:<span th:text="${terminalNumber}"></span></div> <div style="font-size: 32px;margin-left: 35px;">&ensp;&ensp;号:<span th:text="${terminalNumber}"></span></div>
<div style="font-size: 32px;margin-left: 59px;">打印时间:<span th:text="${printTime}"></span></div> <div style="font-size: 32px;margin-left: 35px;">打印时间:<span th:text="${printTime}"></span></div>
</div> </div>
</div> </div>
</body> </body>