Docx4j实现MsOffice(word、ppt、Excel)文件转PDF

MsOffice转PDF开源方案大致如下,如有错误请指正补充

  1. docx4j(可远程/本地)
  2. documents4j(本地且支持windows)
  3. jodconverter+libreoffice(windows+linux)
  4. jodconverter+openoffice(windows+linux)
  5. XDocReport(暂未试验)

如需完美转换,最好的方式仍然是使用windows,利用安装的office组件转换

一、docx4j相关

  1. 官方:https://www.docx4java.org/trac/docx4j
  2. github:https://www.docx4java.org/trac/docx4j
  3. 开源版与企业版比较:https://www.plutext.com/index.html?_ga=2.60354084.806085802.1650340826-1443275412.1650340826
  4. docx4j 8.2.3 提供了 3 种不同的方法来将 Microsoft Word docx 文档转换为 PDF。也可以将pptx或xlsx转换为PDF:https://www.docx4java.org/blog/2020/09/office-pptxxlsxdocx-to-pdf-to-in-docx4j-8-2-3/

二、环境说明

  1. java版本:1.8
  2. springboot:2.6.6
  3. 相关依赖:
<dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-JAXB-Internal</artifactId>
            <version>8.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
            <version>8.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-JAXB-MOXy</artifactId>
            <version>8.3.4</version>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.docx4j</groupId>-->
<!--            <artifactId>docx4j-export-fo</artifactId>-->
<!--            <version>8.3.4</version>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-documents4j-local</artifactId>
            <version>8.3.4</version>
        </dependency>
        <dependency>
            <groupId>com.documents4j</groupId>
            <artifactId>documents4j-transformer-msoffice-excel</artifactId>
            <version>1.1.7</version>
        </dependency>   
        

三、代码实现


import com.documents4j.api.DocumentType;
import org.docx4j.documents4j.local.Documents4jLocalServices;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import static com.jc.officefileconvert.util.Itext7PdfUtils.getCtm;
import static com.jc.officefileconvert.util.Itext7PdfUtils.getServerBasePath;
 
public class Docx4jOfficeFileToPDF {

    public static File transferToFile(MultipartFile multipartFile) {
//        选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。
        File file = null;
        try {
            String originalFilename = multipartFile.getOriginalFilename();
            String[] filename = originalFilename.split("\\.");
            file=File.createTempFile(filename[0], filename[1]);
            multipartFile.transferTo(file);
            file.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static String docx2Pdf(MultipartFile file) {
        String relativelyPath = String.format("pdf/%s.pdf", getCtm());
        String path = CommonConfig.getFileSavePath() + relativelyPath;
        File output = new File(path);
        if (!output.exists()){
            output.getParentFile().mkdirs();
        }

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(output);
            Documents4jLocalServices exporter = new Documents4jLocalServices ();
            exporter.export(transferToFile(file), fos, DocumentType.MS_WORD);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

          return getServerBasePath() + relativelyPath;
    }

    public static String pptx2Pdf(MultipartFile file) {
        String relativelyPath = String.format("pdf/%s.pdf", getCtm());
        String path = CommonConfig.getFileSavePath() + relativelyPath;
        File output = new File(path);
        if (!output.exists()){
            output.getParentFile().mkdirs();
        }

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(output);

            Documents4jLocalServices   exporter = new Documents4jLocalServices  ();
            exporter.export(transferToFile(file), fos, DocumentType.MS_POWERPOINT);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
          return getServerBasePath() + relativelyPath;

    }


    public static String xlsx2Pdf(MultipartFile file) {



        String relativelyPath = String.format("pdf/%s.pdf", getCtm());
        String path = CommonConfig.getFileSavePath() + relativelyPath;
        File output = new File(path);
        if (!output.exists()){
            output.getParentFile().mkdirs();
        }

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(output);

            Documents4jLocalServices   exporter = new Documents4jLocalServices  ();
            exporter.export(transferToFile(file), fos, DocumentType.MS_EXCEL);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
          return getServerBasePath() + relativelyPath;

    }

    public static void main(String[] args) {
    }
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值