1.```java依赖
<dependency><groupId>com.aspose.cells</groupId><artifactId>aspose-cells</artifactId><version>8.5.2</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>${hutool.version}</version></dependency><dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>15.8.0</version></dependency>
2.service方法
```javapublic String getReportFileStream(String id) {TestReport report = this.getById(id);//word转换pdfString pdfUrl = WordToPdfUtils.wordToPdf(report.getReportUrl());report.setReportPdfUrl(pdfUrl);this.updateById(report);return pdfUrl;}
3.工具类
java">word">import cn.hutool.system.OsInfo;
word">import cn.hutool.system.SystemUtil;
word">import com.aspose.cells.License;
word">import com.aspose.words.Document;
word">import com.aspose.words.FontSettings;
word">import com.aspose.words.SaveFormat;
word">import lombok.extern.slf4j.Slf4j;word">import java.io.*;
word">import java.nio.file.Path;
word">import java.nio.file.Paths;/*** @description:* @create: 2022-02-28 10:22**/
@Slf4j
word">public word">class WordToPdfUtils {/*** aspose授权** @return*/word">public word">static word">boolean getLicense() {word">boolean result = false;word">try {// 凭证String licenseStr = "";InputStream license = word">new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));License asposeLic = word">new License();asposeLic.setLicense(license);result = true;} word">catch (Exception e) {log.error("error:", e);}word">return result;}/*** word转pdf** @param docFilePath*/word">public word">static String wordToPdf(String docFilePath) {FileOutputStream fileOS = word">null;// 验证Licenseword">if (!getLicense()) {log.error("验证License失败!");word">return word">null;}//文件名不带后缀Path docPath = Paths.get(docFilePath);String fileNameWithoutSuffix = docPath.toString().replaceFirst("[.][^.]+$", "");Path filePth = Paths.get(fileNameWithoutSuffix + ".pdf");String filePathStr = filePth.toString();word">try {//此处处理乱码和小方块//如果在本地运行,此处报错,请注释这个这是字体,主要是为了解决linux环境下面运行jar时找不到中文字体的问题 FontSettings.//在linux中运行请放开注释!!否则中文乱码!OsInfo osInfo = SystemUtil.getOsInfo();word">if (osInfo.isLinux()) {FontSettings.setFontsFolders(word">new String[]{"/usr/share/fonts", "/usr/share/fonts/chinese"}, true);}Document doc = word">new Document(docFilePath);fileOS = word">new FileOutputStream(word">new File(filePathStr));// 保存转换的pdf文件doc.save(fileOS, SaveFormat.PDF);} word">catch (Exception e) {log.error("error:", e);} word">finally {word">try {word">if (fileOS != word">null) {fileOS.close();}} word">catch (IOException e) {log.error("error:", e);}}word">return filePathStr;}}