diff --git a/yudao-module-system/yudao-module-system-biz/pom.xml b/yudao-module-system/yudao-module-system-biz/pom.xml index 096a3c03c..a9da986ed 100644 --- a/yudao-module-system/yudao-module-system-biz/pom.xml +++ b/yudao-module-system/yudao-module-system-biz/pom.xml @@ -128,6 +128,18 @@ 4.4.0 + + + com.itextpdf + itextpdf + 5.5.13.4 + + + com.itextpdf + itext-asian + 5.2.0 + + diff --git a/yudao-module-tblist/yudao-module-tblist-biz/src/main/java/cn/iocoder/yudao/module/tblist/controller/admin/patientexamlist/PatientexamlistController.java b/yudao-module-tblist/yudao-module-tblist-biz/src/main/java/cn/iocoder/yudao/module/tblist/controller/admin/patientexamlist/PatientexamlistController.java index 4ab9f8534..63d7944ce 100644 --- a/yudao-module-tblist/yudao-module-tblist-biz/src/main/java/cn/iocoder/yudao/module/tblist/controller/admin/patientexamlist/PatientexamlistController.java +++ b/yudao-module-tblist/yudao-module-tblist-biz/src/main/java/cn/iocoder/yudao/module/tblist/controller/admin/patientexamlist/PatientexamlistController.java @@ -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> generatePdf_ct(@RequestBody JSONObject params) { + Map out = patientexamlistService.generatePdf_ct(params); + return success(out); + } + + @PostMapping("/generatePdf_us") + @Operation(summary = "生成pdf进行上传(us)") + public CommonResult> generatePdf_us(@RequestBody JSONObject params) { + Map out = patientexamlistService.generatePdf_us(params); + return success(out); + } + @PostMapping("/ftppdf") @Operation(summary = "获取pdf上传ftp") public void FtpPdf(@RequestBody inspdfscreenshotVO insimagescreenshotVO) { diff --git a/yudao-module-tblist/yudao-module-tblist-biz/src/main/java/cn/iocoder/yudao/module/tblist/service/patientexamlist/PatientexamlistService.java b/yudao-module-tblist/yudao-module-tblist-biz/src/main/java/cn/iocoder/yudao/module/tblist/service/patientexamlist/PatientexamlistService.java index cc32c042a..fa9042cd0 100644 --- a/yudao-module-tblist/yudao-module-tblist-biz/src/main/java/cn/iocoder/yudao/module/tblist/service/patientexamlist/PatientexamlistService.java +++ b/yudao-module-tblist/yudao-module-tblist-biz/src/main/java/cn/iocoder/yudao/module/tblist/service/patientexamlist/PatientexamlistService.java @@ -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 { Map getCheckRecord(String regId, String orgId); List selectMp(Map columnMap); + + /** + * 生成pdf进行上传(ct报告) + */ + Map generatePdf_ct(JSONObject params); + + /** + * 生成pdf进行上传(us报告) + */ + Map generatePdf_us(JSONObject params); } \ No newline at end of file diff --git a/yudao-module-tblist/yudao-module-tblist-biz/src/main/java/cn/iocoder/yudao/module/tblist/service/patientexamlist/PatientexamlistServiceImpl.java b/yudao-module-tblist/yudao-module-tblist-biz/src/main/java/cn/iocoder/yudao/module/tblist/service/patientexamlist/PatientexamlistServiceImpl.java index 0b4c2875e..d6978e15f 100644 --- a/yudao-module-tblist/yudao-module-tblist-biz/src/main/java/cn/iocoder/yudao/module/tblist/service/patientexamlist/PatientexamlistServiceImpl.java +++ b/yudao-module-tblist/yudao-module-tblist-biz/src/main/java/cn/iocoder/yudao/module/tblist/service/patientexamlist/PatientexamlistServiceImpl.java @@ -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 implements PatientexamlistService { + @Resource + private ConfigService configService; + @Resource private PatientexamlistMapper patientexamlistMapper; @@ -397,4 +415,243 @@ public class PatientexamlistServiceImpl extends ServiceImpl generatePdf_ct(JSONObject params) { + Map 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 generatePdf_us(JSONObject params) { + Map 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; + } + } } \ No newline at end of file