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;
    }

}
相关推荐
寻星探路2 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
曹牧4 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
爬山算法5 小时前
Hibernate(90)如何在故障注入测试中使用Hibernate?
java·后端·hibernate
kfyty7255 小时前
集成 spring-ai 2.x 实践中遇到的一些问题及解决方案
java·人工智能·spring-ai
猫头虎5 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
李少兄5 小时前
在 IntelliJ IDEA 中修改 Git 远程仓库地址
java·git·intellij-idea
忆~遂愿5 小时前
ops-cv 算子库深度解析:面向视觉任务的硬件优化与数据布局(NCHW/NHWC)策略
java·大数据·linux·人工智能
小韩学长yyds6 小时前
Java序列化避坑指南:明确这4种场景,再也不盲目实现Serializable
java·序列化
仟濹6 小时前
【Java基础】多态 | 打卡day2
java·开发语言
Re.不晚6 小时前
JAVA进阶之路——无奖问答挑战2
java·开发语言