Selaa lähdekoodia

feat(contract): 增加租赁合同日期格式化与PDF字体支持- 在合同信息中添加了对入驻日期(rzrq)的解析和格式化功能
- 更新租赁合同模板字段数量以适应新增数据点
- 扩展RentalTempBo对象以包含新字段d13用于存储格式化后的日期
- 修改Word转PDF工具类,引入字体处理机制确保中文字体正确嵌入
- 配置PDF保存选项以嵌入完整字体,提高跨平台显示一致性- 调整测试用例中的参数值以匹配新的日期输入要求- 将应用配置从开发环境切换到生产环境

nahida 8 kuukautta sitten
vanhempi
commit
7efe5a737a

+ 1 - 0
src/main/java/com/zksy/property/domain/bo/RentalTempBo.java

@@ -116,6 +116,7 @@ public class RentalTempBo {
     private String d10;
     private String d11;
     private String d12;
+    private String d13;
 
     private String e1;
     private String e2;

+ 4 - 0
src/main/java/com/zksy/property/service/impl/AContractInfoServiceImpl.java

@@ -121,6 +121,10 @@ public class AContractInfoServiceImpl extends ServiceImpl<AContractInfoMapper, A
         bo.setD10(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy年M月d日")));
         bo.setD11(dto.getOperator());
         bo.setD12(receiptNumber);
+        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy年M月d日");
+        LocalDate rzrqDate = LocalDate.parse(dto.getRzrq(), inputFormatter);
+        bo.setD13(rzrqDate.format(outputFormatter));
 
         String attachmentUrl = aRentalContractService.generatorRental("4", bo, true);
         AReceiptInfo receiptInfo = new AReceiptInfo();

+ 1 - 1
src/main/java/com/zksy/property/service/impl/ARentalContractServiceImpl.java

@@ -70,7 +70,7 @@ public class ARentalContractServiceImpl extends ServiceImpl<ARentalContractMappe
         markToFieldCount.put("1", 9);
         markToFieldCount.put("2", 56);
         markToFieldCount.put("3", 27);
-        markToFieldCount.put("4", 12);
+        markToFieldCount.put("4", 13);
         markToFieldCount.put("5", 8);
 
         // 验证标记类型

+ 39 - 4
src/main/java/com/zksy/utils/WordUtils.java

@@ -4,6 +4,7 @@ import cn.hutool.core.util.ArrayUtil;
 import cn.hutool.core.util.StrUtil;
 import com.aspose.words.Document;
 import com.aspose.words.FontSettings;
+import com.aspose.words.PdfSaveOptions;
 import com.aspose.words.SaveFormat;
 import com.deepoove.poi.XWPFTemplate;
 import com.itextpdf.text.BaseColor;
@@ -11,6 +12,7 @@ import com.itextpdf.text.DocumentException;
 import com.itextpdf.text.pdf.*;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
 import org.apache.poi.xwpf.usermodel.XWPFParagraph;
+import org.springframework.core.io.ClassPathResource;
 
 import java.io.*;
 import java.nio.file.Files;
@@ -130,6 +132,35 @@ public class WordUtils {
         }
     }
 
+    // 初始化字体目录(只执行一次)
+    private static final String FONT_DIR;
+
+    static {
+        try {
+            // 创建临时字体目录
+            File tempFontDir = Files.createTempDirectory("fonts").toFile();
+            FONT_DIR = tempFontDir.getAbsolutePath();
+
+            // 把 resources/fonts 下的字体文件复制到临时目录
+            String[] fontFiles = { "simsun.ttc", "SimSun.ttf", "msyh.ttc" };
+            for (String fontFile : fontFiles) {
+                ClassPathResource resource = new ClassPathResource("fonts/" + fontFile);
+                if (resource.exists()) {
+                    File targetFile = new File(tempFontDir, fontFile);
+                    try (InputStream in = resource.getInputStream()) {
+                        Files.copy(in, targetFile.toPath());
+                    }
+                }
+            }
+
+            // 设置 Aspose 全局字体目录
+            FontSettings fontSettings = FontSettings.getDefaultInstance();
+            fontSettings.setFontsFolders(new String[]{FONT_DIR}, true);
+
+        } catch (IOException e) {
+            throw new RuntimeException("初始化字体目录失败", e);
+        }
+    }
     /*
      * @Description: word文件转pdf文件(生成临时文件)
      * @Param:
@@ -140,14 +171,18 @@ public class WordUtils {
         FileOutputStream fileOS = null;
         try {
             Document doc = new Document(wordFile.getAbsolutePath());
-            // 创建临时PDF文件
+
+            // PDF 保存选项,嵌入字体,保证跨平台显示一致
+            PdfSaveOptions saveOptions = new PdfSaveOptions();
+            saveOptions.setEmbedFullFonts(true);
+
+            // 创建临时 PDF 文件
             File pdfFile = File.createTempFile("converted_", ".pdf");
             fileOS = new FileOutputStream(pdfFile);
-            // 保存转换的pdf文件
-            doc.save(fileOS, SaveFormat.PDF);
+            doc.save(fileOS, saveOptions);
             return pdfFile;
         } catch (Exception e) {
-            throw new RuntimeException("转换Word文档为PDF失败: " + e.getMessage());
+            throw new RuntimeException("转换Word文档为PDF失败: " + e.getMessage(), e);
         } finally {
             try {
                 if (fileOS != null) {

+ 1 - 1
src/main/resources/application.yml

@@ -20,7 +20,7 @@ spring:
     # 国际化资源文件路径
     basename: i18n/messages
   profiles:
-    active: dev
+    active: prod
   # 文件上传
   servlet:
     multipart:

BIN
src/main/resources/fonts/SimSun.ttf


BIN
src/main/resources/fonts/msyh.ttc


BIN
src/main/resources/fonts/simsun.ttc


BIN
src/main/resources/templates/4.docx


+ 2 - 1
src/test/java/com/zksy/property/service/impl/AContractInfoServiceImplTest.java

@@ -141,7 +141,8 @@ class AContractInfoServiceImplTest {
                 new BigDecimal(1),
                 new BigDecimal(1),
                 "1",
-                "1"
+                "2025-07-29",
+                "2025-07-29"
         );
         String receipt = aContractInfoService.getReceipt("31c7f3fdfc236328bce3307645d566dd",dto);
         System.out.println(receipt);