java实现word转换pdf,word文件转换pdf文件,java如何将word转换pdf

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方法
```java
 public String getReportFileStream(String id) {
        TestReport report = this.getById(id);
        //word转换pdf
        String pdfUrl = WordToPdfUtils.wordToPdf(report.getReportUrl());
        report.setReportPdfUrl(pdfUrl);
        this.updateById(report);
        return pdfUrl;
    }

3.工具类

java 复制代码
import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil;
import com.aspose.cells.License;
import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @description:

 * @create: 2022-02-28 10:22
 **/
@Slf4j
public class WordToPdfUtils {

    /**
     * aspose授权
     *
     * @return
     */
    public static boolean getLicense() {
        boolean result = false;
        try {
            // 凭证
            String licenseStr = "";
            InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));
            License asposeLic = new License();
            asposeLic.setLicense(license);
            result = true;
        } catch (Exception e) {
            log.error("error:", e);
        }
        return result;
    }


    /**
     * word转pdf
     *
     * @param docFilePath
     */
    public static String wordToPdf(String docFilePath) {
        FileOutputStream fileOS = null;
        // 验证License
        if (!getLicense()) {
            log.error("验证License失败!");
            return null;
        }
        //文件名不带后缀
        Path docPath = Paths.get(docFilePath);
        String fileNameWithoutSuffix = docPath.toString().replaceFirst("[.][^.]+$", "");
        Path filePth = Paths.get(fileNameWithoutSuffix + ".pdf");
        String filePathStr = filePth.toString();
        try {
            //此处处理乱码和小方块
            //如果在本地运行,此处报错,请注释这个这是字体,主要是为了解决linux环境下面运行jar时找不到中文字体的问题 FontSettings.
            //在linux中运行请放开注释!!否则中文乱码!
            OsInfo osInfo = SystemUtil.getOsInfo();
            if (osInfo.isLinux()) {
                FontSettings.setFontsFolders(new String[]{"/usr/share/fonts", "/usr/share/fonts/chinese"}, true);
            }

            Document doc = new Document(docFilePath);
            fileOS = new FileOutputStream(new File(filePathStr));
            // 保存转换的pdf文件
            doc.save(fileOS, SaveFormat.PDF);
        } catch (Exception e) {
            log.error("error:", e);
        } finally {
            try {
                if (fileOS != null) {
                    fileOS.close();
                }
            } catch (IOException e) {
                log.error("error:", e);
            }
        }
        return filePathStr;
    }

}
相关推荐
高兴达1 分钟前
Spring boot入门工程
java·spring boot·后端
萧曵 丶2 分钟前
Spring @TransactionalEventListener
java·数据库·spring·事务·transactional·异步
笑衬人心。3 分钟前
HTTPS详解:原理 + 加解密过程 + 面试问答
java·网络协议·http·面试·https
蓝澈11215 分钟前
弗洛伊德(Floyd)算法-各个顶点之间的最短路径问题
java·数据结构·动态规划
再见晴天*_*14 分钟前
logback 日志不打印
java·服务器·logback
幽络源小助理22 分钟前
SpringBoot基于JavaWeb的城乡居民基本医疗信息管理系统
java·spring boot·学习
欧阳有财25 分钟前
[java八股文][Mysql面试篇]日志
java·mysql·面试
TDengine (老段)34 分钟前
使用 StatsD 向 TDengine 写入
java·大数据·数据库·时序数据库·iot·tdengine·涛思数据
真实的菜36 分钟前
JVM类加载系统详解:深入理解Java类的生命周期
java·开发语言·jvm
N_NAN_N1 小时前
类图+案例+代码详解:软件设计模式----原型模式
java·设计模式·原型模式