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

}
相关推荐
阿华的代码王国16 分钟前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
Zyy~16 分钟前
《设计模式》装饰模式
java·设计模式
A尘埃23 分钟前
企业级Java项目和大模型结合场景(智能客服系统:电商、金融、政务、企业)
java·金融·政务·智能客服系统
青云交1 小时前
Java 大视界 -- 基于 Java 的大数据可视化在城市交通拥堵治理与出行效率提升中的应用(398)
java·大数据·flink·大数据可视化·拥堵预测·城市交通治理·实时热力图
CHEN5_021 小时前
【Java基础面试题】Java基础概念
java·开发语言
二十雨辰2 小时前
[TG开发]照片机器人
java·web3
武昌库里写JAVA2 小时前
JAVA面试汇总(四)JVM(一)
java·vue.js·spring boot·sql·学习
落霞的思绪3 小时前
Java设计模式详细解读
java·开发语言·设计模式
Java小白程序员3 小时前
Spring Framework:Java 开发的基石与 Spring 生态的起点
java·数据库·spring
拂晓银砾4 小时前
Java数据结构-栈
java·数据结构