diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/pdf/service/ChangePdfService.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/pdf/service/ChangePdfService.java index 7f3a7399..55fad6ba 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/pdf/service/ChangePdfService.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/pdf/service/ChangePdfService.java @@ -12,9 +12,10 @@ import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayOutputStream; -import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.util.Map; + @Service public class ChangePdfService { // 定义最大和最小字体大小,用于动态调整 @@ -41,8 +42,11 @@ public class ChangePdfService { try { // 加载模板PDF ClassLoader classLoader = getClass().getClassLoader(); - File templateFile = new File(classLoader.getResource("static/file/pdf/smrydlgwspb.pdf").getFile()); - document = PDDocument.load(templateFile); + InputStream templateStream = classLoader.getResourceAsStream("static/file/pdf/smrydlgwspb.pdf"); + if (templateStream == null) { + throw new IOException("无法找到PDF模板文件"); + } + document = PDDocument.load(templateStream); PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); if (acroForm == null) { @@ -50,8 +54,11 @@ public class ChangePdfService { } // 加载中文字体 - PDType0Font font = PDType0Font.load(document, - new File(classLoader.getResource("static/file/pdf/STSONG.TTF").getFile())); + InputStream fontStream = classLoader.getResourceAsStream("static/file/pdf/STSONG.TTF"); + if (fontStream == null) { + throw new IOException("无法找到字体文件"); + } + PDType0Font font = PDType0Font.load(document, fontStream); // 设置需要更新表单外观 acroForm.setNeedAppearances(true); @@ -196,4 +203,4 @@ public class ChangePdfService { return fontSize; } -} +} \ No newline at end of file diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/pdf/service/LeavePdfService.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/pdf/service/LeavePdfService.java index 4eff5b6d..40a22557 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/pdf/service/LeavePdfService.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/pdf/service/LeavePdfService.java @@ -12,8 +12,8 @@ import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayOutputStream; -import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.util.Map; @Service @@ -26,7 +26,7 @@ public class LeavePdfService { private static final float ESTIMATED_FIELD_WIDTH = 100f; // 需要特殊处理的字段列表 private static final String[] AUTO_WRAP_FIELDS = { - "涉密岗位","职务职级","姓名","政治面貌" + "涉密岗位","职务职级","姓名","政治面貌" }; public ByteArrayOutputStream fillForm(Map formData, @@ -43,8 +43,11 @@ public class LeavePdfService { try { // 加载模板PDF ClassLoader classLoader = getClass().getClassLoader(); - File templateFile = new File(classLoader.getResource("static/file/pdf/smrylzlgspb.pdf").getFile()); - document = PDDocument.load(templateFile); + InputStream templateStream = classLoader.getResourceAsStream("static/file/pdf/smrylzlgspb.pdf"); + if (templateStream == null) { + throw new IOException("无法找到PDF模板文件"); + } + document = PDDocument.load(templateStream); PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); if (acroForm == null) { @@ -52,8 +55,11 @@ public class LeavePdfService { } // 加载中文字体 - PDType0Font font = PDType0Font.load(document, - new File(classLoader.getResource("static/file/pdf/STSONG.TTF").getFile())); + InputStream fontStream = classLoader.getResourceAsStream("static/file/pdf/STSONG.TTF"); + if (fontStream == null) { + throw new IOException("无法找到字体文件"); + } + PDType0Font font = PDType0Font.load(document, fontStream); // 设置需要更新表单外观 acroForm.setNeedAppearances(true);