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

}
相关推荐
It's now2 分钟前
Spring AI 基础开发流程
java·人工智能·后端·spring
cxh_陈2 分钟前
线程的状态,以及和锁有什么关系
java·线程·线程的状态·线程和锁
计算机毕设VX:Fegn08955 分钟前
计算机毕业设计|基于springboot + vue图书商城系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
R.lin17 分钟前
Java 8日期时间API完全指南
java·开发语言·python
毕设源码-赖学姐23 分钟前
【开题答辩全过程】以 高校教学质量监控平台为例,包含答辩的问题和答案
java·eclipse
高山上有一只小老虎30 分钟前
翻之矩阵中的行
java·算法
火钳游侠44 分钟前
java单行注释,多行注释,文档注释
java·开发语言
code bean1 小时前
【CMake】为什么需要清理 CMake 缓存文件?深入理解 CMake 生成器切换机制
java·spring·缓存
selt7911 小时前
Redisson之RedissonLock源码完全解析
android·java·javascript
RestCloud1 小时前
智能制造的底层基建:iPaaS 如何统一 ERP、MES 与 WMS 的数据流
java·wms·erp·数据传输·ipaas·mes·集成平台