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 小时前
Java-71 深入浅出 RPC Dubbo 上手 父工程配置编写 附详细POM与代码
java·分布式·程序人生·spring·微服务·rpc·dubbo
武子康3 小时前
Java-72 深入浅出 RPC Dubbo 上手 生产者模块详解
java·spring boot·分布式·后端·rpc·dubbo·nio
_殊途3 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
椰椰椰耶5 小时前
【Spring】拦截器详解
java·后端·spring
没有bug.的程序员5 小时前
JAVA面试宝典 - 《MyBatis 进阶:插件开发与二级缓存》
java·面试·mybatis
没有羊的王K7 小时前
SSM框架学习——day1
java·学习
又菜又爱coding7 小时前
安装Keycloak并启动服务(macOS)
java·keycloak
不知道叫什么呀7 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc
wan_da_ren8 小时前
JVM监控及诊断工具-GUI篇
java·开发语言·jvm·后端
cui_hao_nan8 小时前
JAVA并发——什么是Java的原子性、可见性和有序性
java·开发语言