打印模板
Some checks are pending
Java CI with Maven / build (11) (push) Waiting to run
Java CI with Maven / build (17) (push) Waiting to run
Java CI with Maven / build (8) (push) Waiting to run
yudao-ui-admin CI / build (14.x) (push) Waiting to run
yudao-ui-admin CI / build (16.x) (push) Waiting to run

This commit is contained in:
旺仔 2025-01-16 09:20:41 +08:00
parent aa8b2d7139
commit a9504ffc1f
4 changed files with 295 additions and 0 deletions

View File

@ -128,6 +128,18 @@
<version>4.4.0</version>
</dependency>
<!--itextpdf-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
</project>

View File

@ -11,6 +11,7 @@ import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import cn.iocoder.yudao.module.tblist.dal.mysql.patientexamlist.PatientexamlistMapper;
import cn.iocoder.yudao.module.tblist.service.patientexamlist.org.OrgService;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mzt.logapi.starter.annotation.LogRecord;
@ -486,6 +487,20 @@ public class PatientexamlistController {
return success(msg);
}
@PostMapping("/generatePdf_ct")
@Operation(summary = "生成pdf进行上传(ct)")
public CommonResult<Map<String, Object>> generatePdf_ct(@RequestBody JSONObject params) {
Map<String, Object> out = patientexamlistService.generatePdf_ct(params);
return success(out);
}
@PostMapping("/generatePdf_us")
@Operation(summary = "生成pdf进行上传(us)")
public CommonResult<Map<String, Object>> generatePdf_us(@RequestBody JSONObject params) {
Map<String, Object> out = patientexamlistService.generatePdf_us(params);
return success(out);
}
@PostMapping("/ftppdf")
@Operation(summary = "获取pdf上传ftp")
public void FtpPdf(@RequestBody inspdfscreenshotVO insimagescreenshotVO) {

View File

@ -5,6 +5,7 @@ import java.util.*;
import cn.iocoder.yudao.module.tblist.controller.admin.patientexamlist.vo.*;
import cn.iocoder.yudao.module.tblist.dal.dataobject.patientexamlist.PatientexamlistDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import javax.validation.Valid;
@ -147,4 +148,14 @@ public interface PatientexamlistService extends IService<PatientexamlistDO> {
Map<String, Object> getCheckRecord(String regId, String orgId);
List<PatientexamlistDO> selectMp(Map<String, Object> columnMap);
/**
* 生成pdf进行上传(ct报告)
*/
Map<String, Object> generatePdf_ct(JSONObject params);
/**
* 生成pdf进行上传(us报告)
*/
Map<String, Object> generatePdf_us(JSONObject params);
}

View File

@ -1,17 +1,32 @@
package cn.iocoder.yudao.module.tblist.service.patientexamlist;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.util.io.FileUtils;
import cn.iocoder.yudao.module.infra.dal.dataobject.config.ConfigDO;
import cn.iocoder.yudao.module.infra.service.config.ConfigService;
import cn.iocoder.yudao.module.system.dal.dataobject.dicomworklist.*;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.dicomworklist.DicomworklistMapper;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import cn.iocoder.yudao.module.tblist.dal.mysql.ecganalysisparas.EcganalysisparasMapper;
import cn.iocoder.yudao.module.tblist.dal.mysql.positivestatistics.PositivestatisticsMapper;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
@ -39,6 +54,9 @@ import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUti
@Validated
public class PatientexamlistServiceImpl extends ServiceImpl<PatientexamlistMapper, PatientexamlistDO> implements PatientexamlistService {
@Resource
private ConfigService configService;
@Resource
private PatientexamlistMapper patientexamlistMapper;
@ -397,4 +415,243 @@ public class PatientexamlistServiceImpl extends ServiceImpl<PatientexamlistMappe
{
return patientexamlistMapper.selectByMap(columnMap);
}
@Override
public Map<String, Object> generatePdf_ct(JSONObject params) {
Map<String, Object> out = new HashMap<>();
if (params != null && !params.isEmpty()) {
if (safeString(params.get("model")).equals("0")) {
//普通文件模式
ConfigDO configInfo_basePath = configService.getConfigByKey("printTemplate.basePath");
ConfigDO configInfo_CTTemplate = configService.getConfigByKey("printTemplate.CTTemplate");
String templateFile = configInfo_basePath.getValue() + "\\" + configInfo_CTTemplate.getValue();
PdfReader reader = null;
PdfStamper stamper = null;
ByteArrayOutputStream bos = null;
try {
reader = new PdfReader(templateFile);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
//BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//form.addSubstitutionFont(font);
form.setField("regid",safeString(params.get("regid")));
form.setField("examid",safeString(params.get("examid")));
form.setField("name",safeString(params.get("name")));
form.setField("gender",safeString(params.get("gender")));
form.setField("age",safeString(params.get("age")));
form.setField("deviceName",safeString(params.get("deviceName")));
form.setField("Department",safeString(params.get("Department")));
form.setField("examItemName",safeString(params.get("examItemName")));
form.setField("examDescription",safeString(params.get("examDescription")));
form.setField("diagResults",safeString(params.get("diagResults")));
form.setField("hsname",safeString(params.get("hsname")));
form.setField("diagDoctor",safeString(params.get("diagDoctor")));
form.setField("diagDate",safeString(params.get("diagDate")));
form.setField("reviewDoctor",safeString(params.get("reviewDoctor")));
form.setField("reviewDate", safeString(params.get("reviewDate")));
stamper.setFormFlattening(true);
stamper.close();
String fileName = System.currentTimeMillis() + ".pdf";
writeAttachment(bos.toByteArray(), safeString(params.get("folderPath")), fileName.split("\\.")[0], "pdf");
out.put("code", 200);
out.put("msg", "文件上传成功");
if (!safeString(params.get("id")).equals("")) {
ConfigDO config = configService.getConfigByKey("url.ftpurl");
PatientexamlistSaveReqVO updateReqVO = new PatientexamlistSaveReqVO();
updateReqVO.setId(safeString(params.get("id")));
updateReqVO.setPdfurl(config.getValue() + "/" + safeString(params.get("id")) + "/" + fileName);
updatePatientexamlist(updateReqVO);
}
} catch (Exception ex) {
System.out.println("文件上传失败" + ex.getMessage());
out.put("code", -200);
out.put("msg", "文件上传失败");
} finally {
try {
if (bos != null) {
bos.close();
}
if (reader != null) {
reader.close();
}
} catch (Exception ex) {
}
}
} else {
//ftp模式
out.put("code", -200);
out.put("msg", "ftp模式禁用");
}
} else {
out.put("code", -200);
out.put("msg", "参数不能为空");
}
return out;
}
@Override
public Map<String, Object> generatePdf_us(JSONObject params) {
Map<String, Object> out = new HashMap<>();
if (params != null && !params.isEmpty()) {
if (safeString(params.get("model")).equals("0")) {
//普通文件模式
ConfigDO configInfo_basePath = configService.getConfigByKey("printTemplate.basePath");
ConfigDO configInfo_USTemplate = configService.getConfigByKey("printTemplate.USTemplate");
String templateFile = configInfo_basePath.getValue() + "\\" + configInfo_USTemplate.getValue();
PdfReader reader = null;
PdfStamper stamper = null;
ByteArrayOutputStream bos = null;
try {
reader = new PdfReader(templateFile);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
//BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//form.addSubstitutionFont(font);
form.setField("regid",safeString(params.get("regid")));
form.setField("examid",safeString(params.get("examid")));
form.setField("name",safeString(params.get("name")));
form.setField("gender",safeString(params.get("gender")));
form.setField("age",safeString(params.get("age")));
form.setField("deviceName",safeString(params.get("deviceName")));
form.setField("Department",safeString(params.get("Department")));
form.setField("examItemName",safeString(params.get("examItemName")));
form.setField("examDescription",safeString(params.get("examDescription")));
form.setField("diagResults",safeString(params.get("diagResults")));
form.setField("hsname",safeString(params.get("hsname")));
form.setField("diagDoctor",safeString(params.get("diagDoctor")));
form.setField("diagDate",safeString(params.get("diagDate")));
form.setField("reviewDoctor",safeString(params.get("reviewDoctor")));
form.setField("reviewDate", safeString(params.get("reviewDate")));
Image image = null;
int pageNo = 0;
PdfContentByte under = null;
Rectangle signRect = null;
try {
//image1_af_image
image = getAcroField_Image(safeString(params.get("image1")));
pageNo = form.getFieldPositions("image1_af_image").get(0).page;
if (image != null && pageNo >= 1) {
under = stamper.getOverContent(pageNo);
signRect = form.getFieldPositions("image1_af_image").get(0).position;
if (signRect != null) {
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
image.setAbsolutePosition(signRect.getLeft(), signRect.getBottom() +
((signRect.getHeight() - image.getScaledHeight() - 4 > 0) ? signRect.getHeight() - image.getScaledHeight() - 4 : 0));
under.addImage(image);
}
}
//image2_af_image
image = getAcroField_Image(safeString(params.get("image2")));
pageNo = form.getFieldPositions("image2_af_image").get(0).page;
if (image != null && pageNo >= 1) {
under = stamper.getOverContent(pageNo);
signRect = form.getFieldPositions("image2_af_image").get(0).position;
if (signRect != null) {
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
image.setAbsolutePosition(signRect.getLeft(), signRect.getBottom() +
((signRect.getHeight() - image.getScaledHeight() - 4 > 0) ? signRect.getHeight() - image.getScaledHeight() - 4 : 0));
under.addImage(image);
}
}
//image3_af_image
image = getAcroField_Image(safeString(params.get("image3")));
pageNo = form.getFieldPositions("image3_af_image").get(0).page;
if (image != null && pageNo >= 1) {
under = stamper.getOverContent(pageNo);
signRect = form.getFieldPositions("image3_af_image").get(0).position;
if (signRect != null) {
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
image.setAbsolutePosition(signRect.getLeft(), signRect.getBottom() +
((signRect.getHeight() - image.getScaledHeight() - 4 > 0) ? signRect.getHeight() - image.getScaledHeight() - 4 : 0));
under.addImage(image);
}
}
} catch (Exception ex) {
System.out.println("[image_af_image]生成失败");
}
stamper.setFormFlattening(true);
stamper.close();
String fileName = System.currentTimeMillis() + ".pdf";
writeAttachment(bos.toByteArray(), safeString(params.get("folderPath")), fileName.split("\\.")[0], "pdf");
out.put("code", 200);
out.put("msg", "文件上传成功");
if (!safeString(params.get("id")).equals("")) {
ConfigDO config = configService.getConfigByKey("url.ftpurl");
PatientexamlistSaveReqVO updateReqVO = new PatientexamlistSaveReqVO();
updateReqVO.setId(safeString(params.get("id")));
updateReqVO.setPdfurl(config.getValue() + "/" + safeString(params.get("id")) + "/" + fileName);
updatePatientexamlist(updateReqVO);
}
} catch (Exception ex) {
System.out.println("文件上传失败" + ex.getMessage());
out.put("code", -200);
out.put("msg", "文件上传失败");
} finally {
try {
if (bos != null) {
bos.close();
}
if (reader != null) {
reader.close();
}
} catch (Exception ex) {
}
}
} else {
//ftp模式
out.put("code", -200);
out.put("msg", "ftp模式禁用");
}
} else {
out.put("code", -200);
out.put("msg", "参数不能为空");
}
return out;
}
private Image getAcroField_Image(String url) {
Image out = null;
try {
URL urlObj = new URL(safeString(url));
if (urlObj != null)
out = Image.getInstance(urlObj);
} catch (Exception ex) {
out = null;
}
return out;
}
private String safeString(Object value) {
String out = (value == null) ? "" : value.toString().trim();
return out.trim();
}
private void writeAttachment(byte[] decodedBytes, String folderPath, String fileName, String extension) throws Exception {
// 检查文件名是否有效
if (fileName.contains("..") || fileName.contains("/") || fileName.contains("\\")) {
throw new IllegalArgumentException("Invalid file name.");
}
// 创建文件的路径
Path folder = Paths.get(folderPath);
if (!Files.exists(folder)) {
// 如果文件夹不存在则创建它
Files.createDirectories(folder);
}
// 构建完整的文件名包括扩展名
Path filePath = folder.resolve(fileName + "." + extension);
// 使用Files.newOutputStream创建OutputStream并设置为追加模式
try (java.io.OutputStream outputStream = Files.newOutputStream(filePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
outputStream.write(decodedBytes);
} catch (IOException ex) {
throw ex;
}
}
}