2 Commits 42a8cdbcf7 ... 0f7acef882

Author SHA1 Message Date
  Crazy 0f7acef882 Merge branch 'master' of http://gogs.gisvg.com/YDM/graduate 2 days ago
  Crazy 889b0e6740 1.7 2 days ago

+ 6 - 0
yudao-module-system/yudao-module-system-biz/pom.xml

@@ -154,6 +154,12 @@
         </dependency>
 
 
+        <dependency>
+            <groupId>com.itextpdf</groupId>
+            <artifactId>itextpdf</artifactId>
+            <version>5.5.13.2</version>
+        </dependency>
+
     </dependencies>
 
 </project>

+ 55 - 13
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/mail/MailTemplateController.java

@@ -18,13 +18,17 @@ import cn.iocoder.yudao.module.system.service.user.AdminUserService;
 import com.alibaba.excel.EasyExcel;
 import com.alibaba.excel.ExcelWriter;
 import com.alibaba.excel.write.metadata.WriteSheet;
+import com.itextpdf.text.DocumentException;
+import com.itextpdf.text.Paragraph;
+import com.itextpdf.text.pdf.PdfDocument;
+import com.itextpdf.text.pdf.PdfWriter;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.Parameter;
 import io.swagger.v3.oas.annotations.tags.Tag;
-import org.apache.poi.ss.usermodel.Sheet;
+
+import jdk.javadoc.internal.doclets.formats.html.Table;
 import org.apache.poi.xwpf.usermodel.*;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
-import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
+
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
@@ -32,15 +36,17 @@ import org.springframework.web.bind.annotation.*;
 import javax.annotation.Resource;
 import javax.validation.Valid;
 import java.io.*;
-import java.math.BigInteger;
-import java.time.DayOfWeek;
+
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
 import java.time.LocalDate;
 import java.util.*;
-import java.util.stream.Collectors;
+
 
 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
 import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
-import static org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc.CENTER;
+
 
 @Tag(name = "管理后台 - 邮件模版")
 @RestController
@@ -60,8 +66,6 @@ public class MailTemplateController {
     @Resource
     private PermissionService permissionService;
 
-    private static final short[] COLUMN_WIDTHS = {20, 20, 20, 20}; // 根据需要设置列宽
-
     @Scheduled(cron = "0 30 8 * * ?") // 每天早上 8:30发送前一天的
     @Operation(summary = "定时发送邮件给导师")
     public void sendMailToTeacherScheduled() {
@@ -239,12 +243,19 @@ public class MailTemplateController {
 
                 document.write(byteArrayOutputStream);
 
+                String outputPath ="output.pdf";
+                // 将 Word 文档转换为 PDF
+                convert(byteArrayOutputStream.toByteArray(), outputPath);
+
+                FileInputStream pdfInputStream =new FileInputStream(outputPath);
+
+
                 Map<String, InputStream> attachments = new HashMap<>();
-                String fileName = String.format("%s考勤信息.docx", (teacher.getDeptId() == null || teacher.getDeptId() == 0 ? "" : teacher.getDeptName()) + yesterday);
-                attachments.put(fileName, new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
+                String fileName = String.format("%s考勤信息.pdf", (teacher.getDeptId() == null || teacher.getDeptId() == 0 ? "" : teacher.getDeptName()) + yesterday);
+                attachments.put(fileName, pdfInputStream);
                 // 发送邮件,包含附件
                 if (teacher.getEmail() != null) {
-                    mailSendService.sendSingleMailToMemberWithAttachments(teacher.getEmail(), null, "attendance-list-word", templateParams, attachments);
+                    mailSendService.sendSingleMailToMemberWithAttachments("1157853982@qq.com", null, "attendance-list-word", templateParams, attachments);
                 }
                 // 每次推送后等待0.1s
                 try {
@@ -566,11 +577,17 @@ public class MailTemplateController {
 
                 document.write(byteArrayOutputStream);
 
+                String outputPath ="output.pdf";
+                // 将 Word 文档转换为 PDF
+                convert(byteArrayOutputStream.toByteArray(), outputPath);
+
+                FileInputStream pdfInputStream =new FileInputStream(outputPath);
+
                 // 创建附件并将文件保存
                 Map<String, InputStream> attachments = new HashMap<>();
 //            String fileName = String.format("%s考勤信息.docx", (teacher.getDeptId()==null||teacher.getDeptId()==0?"":teacher.getDeptName())+yesterday);
                 String fileName = String.format("%s考勤信息.docx", yesterday);
-                attachments.put(fileName, new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
+                attachments.put(fileName, pdfInputStream);
                 // 发送邮件
                 if (teacher.getEmail() != null) {
                     mailSendService.sendSingleMailToMemberWithAttachments(email, null, "attendance-list-excel", templateParams, attachments);
@@ -803,4 +820,29 @@ public class MailTemplateController {
 
     }
 
+    public static void convert(byte[] wordBytes, String outputFilePath) {
+        try {
+            // 使用 ByteArrayInputStream 读取 Word 文档的字节数据
+            ByteArrayInputStream bis = new ByteArrayInputStream(wordBytes);
+            XWPFDocument document = new XWPFDocument(bis);
+
+            // 创建 PDF 文档
+            com.itextpdf.text.Document pdfDocument = (com.itextpdf.text.Document) new com.itextpdf.text.Document();
+            PdfWriter.getInstance((com.itextpdf.text.Document) pdfDocument, Files.newOutputStream(Paths.get(outputFilePath)));
+            pdfDocument.open();
+
+            // 将 Word 文档内容写入 PDF 文档
+            List<XWPFParagraph> paragraphs = document.getParagraphs();
+            for (XWPFParagraph paragraph : paragraphs) {
+                pdfDocument.add(new Paragraph(paragraph.getText()));
+            }
+            // 关闭文档
+            pdfDocument.close();
+            bis.close();
+            System.out.println("Word转PDF成功!");
+        } catch (IOException | DocumentException e) {
+            System.out.println("Word转PDF失败:" + e.getMessage());
+        }
+    }
+
 }