diff --git a/pom.xml b/pom.xml index cc1ed732..d2282a69 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,6 @@ 2.3.0 2.6 1.2.2 - 2.5 1.10 1.10 1.4.0 @@ -133,6 +132,40 @@ ${project.basedir}/jar/aspose-words-16.8.0.jar + + + org.apache.poi + poi-ooxml + 5.2.3 + + + org.apache.poi + poi + 5.2.3 + + + org.apache.logging.log4j + log4j-api + 2.24.2 + + + + org.apache.logging.log4j + log4j-core + 2.24.2 + + + + org.apache.xmlbeans + xmlbeans + 5.2.2 + + + com.luhuiguo + aspose-cells + 23.1 + + org.assertj assertj-core @@ -243,7 +276,7 @@ commons-io commons-io - ${commons.io.version} + 2.18.0 commons-codec diff --git a/src/main/java/com/xujie/sys/modules/auth/service/impl/AuthHistServiceImpl.java b/src/main/java/com/xujie/sys/modules/auth/service/impl/AuthHistServiceImpl.java index c159feba..dd45033d 100644 --- a/src/main/java/com/xujie/sys/modules/auth/service/impl/AuthHistServiceImpl.java +++ b/src/main/java/com/xujie/sys/modules/auth/service/impl/AuthHistServiceImpl.java @@ -101,13 +101,13 @@ public class AuthHistServiceImpl extends ServiceImpl"); if (emailAddress.get(0).getRecordTypeDb().equals("1")){ // 使用 标签创建可点击链接,链接地址和文本均自定义 - Link = "" + + Link = "" + emailAddress.get(0).getOrderRef1() + ""; fromName = "报价单"; s.append("
  • 有新的报价单待审批,请及时处理!报价单号:").append(Link).append("
  • "); } else if (emailAddress.get(0).getRecordTypeDb().equals("2")) { - Link = "" + + Link = "" + emailAddress.get(0).getOrderRef1() + ""; fromName = "询价申请单"; s.append("
  • 有新的询价申请单待审批,请及时处理!询价申请单号:").append(Link).append("
  • "); diff --git a/src/main/java/com/xujie/sys/modules/oss/controller/OssController.java b/src/main/java/com/xujie/sys/modules/oss/controller/OssController.java index a85da21a..def0b05e 100644 --- a/src/main/java/com/xujie/sys/modules/oss/controller/OssController.java +++ b/src/main/java/com/xujie/sys/modules/oss/controller/OssController.java @@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; +import java.io.*; import java.util.List; @RestController @@ -37,7 +38,7 @@ public class OssController { } @PostMapping("/{id}") - public void previewOssFileById(@PathVariable("id") Integer id, HttpServletResponse response){ + public void previewOssFileById(@PathVariable("id") Integer id, HttpServletResponse response) throws Exception { sysOssService.previewOssFileById(id,response); } } diff --git a/src/main/java/com/xujie/sys/modules/oss/service/SysOssService.java b/src/main/java/com/xujie/sys/modules/oss/service/SysOssService.java index 22930fa8..4868f06b 100644 --- a/src/main/java/com/xujie/sys/modules/oss/service/SysOssService.java +++ b/src/main/java/com/xujie/sys/modules/oss/service/SysOssService.java @@ -9,6 +9,8 @@ import com.xujie.sys.modules.oss.vo.SysOssEntityData; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.IOException; import java.util.List; import java.util.Map; @@ -50,5 +52,5 @@ public interface SysOssService extends IService { List queryOssFile(SysOssEntity oss); - void previewOssFileById(Integer id, HttpServletResponse response); + void previewOssFileById(Integer id, HttpServletResponse response) throws Exception; } diff --git a/src/main/java/com/xujie/sys/modules/oss/service/impl/SysOssServiceImpl.java b/src/main/java/com/xujie/sys/modules/oss/service/impl/SysOssServiceImpl.java index 36b95d94..abc7f8a7 100644 --- a/src/main/java/com/xujie/sys/modules/oss/service/impl/SysOssServiceImpl.java +++ b/src/main/java/com/xujie/sys/modules/oss/service/impl/SysOssServiceImpl.java @@ -1,5 +1,10 @@ package com.xujie.sys.modules.oss.service.impl; +import com.aspose.cells.PageOrientationType; +import com.aspose.cells.PageSetup; +import com.aspose.cells.Worksheet; +import com.aspose.cells.Workbook; +import com.aspose.cells.PaperSizeType; import com.xujie.sys.datasource.annotation.DataSource; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -27,6 +32,13 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import org.apache.poi.ss.usermodel.*; +import com.aspose.words.Document; + +import com.aspose.cells.Workbook; + +import java.io.File; + @Service("sysOssService") @@ -141,7 +153,7 @@ public class SysOssServiceImpl extends ServiceImpl impl } @Override - public void previewOssFileById(Integer id, HttpServletResponse response) { + public void previewOssFileById(Integer id, HttpServletResponse response) throws Exception { SysOssEntity oss = this.getById(id); if (Objects.isNull(oss)) { throw new IllegalArgumentException("文件不存在"); @@ -152,10 +164,27 @@ public class SysOssServiceImpl extends ServiceImpl impl if (!file.exists()) { throw new IllegalArgumentException("文件不存在"); } + if (!new File(oss.getUrl().split("\\.")[0] + ".pdf").exists()) { + // 获取文件扩展名 + String fileExtension = getFileExtension(file); + + if ("xlsx".equalsIgnoreCase(fileExtension) || "xls".equalsIgnoreCase(fileExtension)) { + // 如果是 Excel 文件,将其转换为 PDF + convertExcelToPdf(file); + } else if ("docx".equalsIgnoreCase(fileExtension)) { + // 如果是 Word 文件,将其转换为 PDF + convertWordToPdf(file); + } + } String encodedFileName; + String fileName = oss.getFileName(); try { - encodedFileName = URLEncoder.encode(oss.getFileName(), "UTF-8"); + // 如果oss.getFileName()的扩展名为xlsx或xls或docx或doc,则将文件名后缀改为pdf + if (fileName.endsWith(".xlsx") || fileName.endsWith(".xls") || fileName.endsWith(".docx") || fileName.endsWith(".doc")) { + fileName = fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf"; + } + encodedFileName = URLEncoder.encode(fileName, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException("文件名编码失败", e); } @@ -168,6 +197,13 @@ public class SysOssServiceImpl extends ServiceImpl impl } response.setHeader("Content-Disposition", "attachment; filename=" + encodedFileName); + + if (oss.getFileType().equals("xlsx") || oss.getFileType().equals("xls") || oss.getFileType().equals("docx") || oss.getFileType().equals("doc")) { + file = new File(oss.getUrl().split("\\.")[0] + ".pdf"); + } else { + file = new File(oss.getUrl()); + } + try (FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); ServletOutputStream os = response.getOutputStream()) { @@ -183,6 +219,60 @@ public class SysOssServiceImpl extends ServiceImpl impl } catch (IOException e) { throw new RuntimeException("文件下载失败", e); } + } + + // 获取文件扩展名的方法 + public static String getFileExtension(File file) { + String fileName = file.getName(); + int lastIndexOfDot = fileName.lastIndexOf('.'); + if (lastIndexOfDot == -1) { + return ""; // 如果没有扩展名,返回空字符串 + } + return fileName.substring(lastIndexOfDot + 1); + } + + // 将 Excel 文件转换为 PDF + public static void convertExcelToPdf(File file) throws Exception { + Workbook workbook; + String filePath = file.getAbsolutePath(); + + if (filePath.endsWith(".xlsx")) { + workbook = new Workbook(file.getAbsolutePath()); + } else { + workbook = new Workbook(filePath); // 使用 Aspose.Cells 读取 XLS 文件 + } + + Worksheet worksheet = workbook.getWorksheets().get(0); + PageSetup pageSetup = worksheet.getPageSetup(); + + // 设置页面方向(横向或纵向) + pageSetup.setOrientation(PageOrientationType.PORTRAIT); // 纵向 + // pageSetup.setOrientation(PageOrientationType.LANDSCAPE); // 横向 + + // 设置纸张大小(例如 A4) + pageSetup.setPaperSize(PaperSizeType.PAPER_A_4); + // 设置缩放比例(例如调整为 80% 的原始大小) + pageSetup.setZoom(60); + + // 其他设置,比如边距 + pageSetup.setLeftMargin(0); // 左边距 + pageSetup.setRightMargin(0); // 右边距 + pageSetup.setTopMargin(0.5); // 上边距 + pageSetup.setBottomMargin(0.5); // 下边距 + + // 转换为 PDF + String pdfPath = filePath.replaceAll("\\.xlsx|\\.xls", ".pdf"); + workbook.save(pdfPath, com.aspose.cells.SaveFormat.PDF); + System.out.println("Excel 文件转换为 PDF 成功,保存路径:" + pdfPath); + } + + // 将 Word 文件转换为 PDF + public static void convertWordToPdf(File file) throws Exception { + Document document = new Document(file.getAbsolutePath()); + // 转换为 PDF + String pdfPath = file.getAbsolutePath().replace(".docx", ".pdf"); + document.save(pdfPath); + System.out.println("Word 文件转换为 PDF 成功,保存路径:" + pdfPath); } }