java 使用documents4j将word转pdf

发布时间 2023-11-14 20:15:46作者: 快乐小洋人

documents4j 简介

document4j是一个用来进行文档格式转换的Java工具库,它通过借助本机中支持指定文件格式到目标文件格式转换的应用,来实现整个转换的过程。
document4j 实现了Microsoft Word、Excel的适配功能,可以将docx文件转换为pdf文件,并且在这个过程中不会出现非微软文件转换出现的扭曲情况。

  • documents4j 两个转换策略

本地策略

在本地版的实现策略中,document4j将指定文件的转换任务委派给本机中相应的应用程序。因此,为了保证正常运行,这台机器需要在后台预装好支持转换的软件,诸如MicrosoftWord / Excel 。
document4j提供了一套简单易用的机制允许用户注册自定义的转换器,同时将具体的实现细节和Microsoft Word / Excel 进行对接结合。

远程策略

在远程版的实现策略中,document4j将文档的转换工作通过REST-API的方式提供到远程的服务端进行转换。在这个过程中,请求方将文件与相关的格式转换信息发送到服务器端,转换完毕后,转换完成的文件将通过response的方式传输回来。
对于document4j的用户来说,这些实现方式都是完全透明的。因此,用户完全可以在本地开发与测试的时候,采用本地版的实现策略,而在生产环境中,完全透明地切换成远程实现模式。所以,后台转换功能也更容易进行模拟操作。

documents4j 使用

documents4j 是一个跨平台的文档转换库,并且可以在 Linux 上进行 Word 转 PDF 的操作。
它利用 Microsft Office 的 APIs 来进行文档转换,因此需要在Linux上安装 OpenOffice/LibreOffice 编辑器。

  1. 安装 OpenOffice/LibreOffice 编辑器

Ubuntu: sudo apt-get install libreoffice

CentOS:sudo yum install libreoffice

  1. 导包
<!--documents4j-->
<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-local</artifactId>
    <version>1.0.3</version>
</dependency>
<!-- documents4j-->
<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-transformer-msoffice-word</artifactId>
    <version>1.0.3</version>
</dependency>
  1. 引入word转pdf工具类

使用模板引擎将word生成,我这里使用的是 Poi-tl ,它在原有的 Apache POI 基础上做了一些扩展

package com.zl.utils;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DocxToPdfUtil {
	// windows 生成目录
    private static String winDirUrl = "C:\\Users\\zl\\Documents\\";

    /**
     * 通过documents4j 实现word转pdf
     *
     * @param sourcePath 源文件地址 如 /root/example.doc
     */
    public static File documents4jWordToPdf(String sourcePath) {
        return documents4jWordToPdf(new File(sourcePath));
    }
	
    /**
     * 通过documents4j 实现word转pdf
     *
     * @param file 源文件
     */
    public static File documents4jWordToPdf(File file) {
        String os = System.getProperty("os.name").toLowerCase();

        log.info("当前系统:{}", os);
        if (os.contains("win")) {
            // Windows操作系统
            return winDocuments4jWordToPdf(file);
        } else if (os.contains("nix") || os.contains("nux") || os.contains("mac")) {
            // Unix/Linux/Mac操作系统
            return linuxDocuments4jWordToPdf(file);
        } else {
            // 未知操作系统
            throw new RuntimeException("不支持当前操作系统转换文档");
        }
    }

    /**
     * 通过documents4j 实现word转pdf -- Windows 环境 需要有 Microsoft Office 服务
     *
     * @param file 源文件
     */
    public static File winDocuments4jWordToPdf(File file) {
        File outputFile = new File(winDirUrl + file.getName().replaceAll("\\.(docx?|\\w+)$", "") + ".pdf");
        try {
            // 这种方式在Linux服务器不可用,所以除非你是window服务器
            InputStream docxInputStream = new FileInputStream(file);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream)
                .as(DocumentType.DOCX)
                .to(outputStream)
                .as(DocumentType.PDF).execute();
            docxInputStream.close();
            outputStream.close();
            return outputFile;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过documents4j 实现word转pdf -- linux 环境 需要有 libreoffice 服务
     *
     * @param file 源文件
     */
    public static File linuxDocuments4jWordToPdf(File file) {
        // 获取文件的绝对路径和目录路径
        String absolutePath = file.getAbsolutePath();
        String parentPath = file.getParent();

        // 构建LibreOffice的命令行工具命令
        String commands = "libreoffice --convert-to pdf "
            + absolutePath + " --outdir " + parentPath;
        // 执行转换命令
        try {
            boolean result = executeLinuxCmd(commands);
            if (result) {
                // 转换成功,返回转换后的PDF文件
                String pdfFilePath = parentPath + File.separator + file.getName().replaceAll("\\.(docx?|\\w+)$", "") + ".pdf";
                log.info(pdfFilePath);
                log.info(pdfFilePath);
                return new File(pdfFilePath);
            } else {
                return null;
            }

        } catch (Exception e) {
            // 转换失败
            log.error("Word文档转换为PDF失败,原因:执行命令时出现异常。", e);
            return null;
        }
    }

    /**
     * 执行命令行
     *
     * @param cmd 命令行
     * @return
     * @throws IOException
     */
    private static boolean executeLinuxCmd(String cmd) throws IOException {
        // 执行命令行工具命令
        Process process = Runtime.getRuntime().exec(cmd);
        try {
            process.waitFor();
        } catch (InterruptedException e) {
            log.error("执行 Linux 命令异常:", e);
            return false;
        }
        return true;
    }
}