Java操作word文档将docx转换为pdf格式

Java操作word文档将docx转换为pdf格式

一.整体说明

     在上传 Office 课件时,格式有:doc,docx,xls,xlsx,ppt,pptx,程序需要将其 转换成 pdf 格式,
才能实现在线查看和预览,此时需要安装课件转换插件,如课 件只使用视频或者 pdf,可无需安装.

二.插件说明

课件转换组件支持两种,分别为 LibreOffice 和 OpenOffice 两种,两个安装一 个即可,本文推荐使用 LibreOffice

三.插件下载

进入 LibreOffice 官网:点击下载

四.组件安装

4.1 上传文件
将下载好的 LibreOffice_6.4.7_Linux_x86-64_rpm.tar.gz 上传到服务器的任意 位置,本位将组件放置位置为: /home/LibreOffice_6.4.7_Linux_x86-64_rp m.tar.gz

4.2解压文件

cd /home
tar -xvf LibreOffice_6.4.7_Linux_x86-64_rpm.tar.gz

4.3安装组件

cd /home/LibreOffice_6.4.7.2_Linux_x86-64_rpm/RPMS/ 
yum localinstall *.rpm -y

等待组件安装完毕即可
4.4 获取配置

进入系统/opt 目录,可以看到 libreoffice6.4 文件夹,此文件夹的路径则是程序 要的 OfficeHome,完整路径为:/opt/libreoffice6.4 (注意,不同版本和不同 系统,此文件夹的位置都不一样)
Linux 系统一般为:/opt/libreofficex.x
MacOs 系统一般为:/Applications/LibreOffice.app/Contents
Windows 系统一般为:C:/Program Files/LibreOffice

4.5配置程序(此处可忽略,在第五步,Java代码编写中进行了配置)
打开程序配置文件,如:application-*.yml 或外置配置文件 application-local.yml,修改 office-home 配置,如:

# 文档转换
jodconverter:
  local:
    enabled: true
    office-home: C:/Program Files/LibreOffice
    max-tasks-per-process: 10
    port-numbers: 8100

五.Java代码编写

5.1 先进行依赖导入

        <!-- 文件转换,通过OpenOfficeLibreOffice来转换 -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-core</artifactId>
            <version>4.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-spring-boot-starter</artifactId>
            <version>4.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-local</artifactId>
            <version>4.4.2</version>
        </dependency>

5.2 yml中数据配置

# 文档转换
jodconverter:
  local:
    enabled: true
    office-home: C:/Program Files/LibreOffice
    max-tasks-per-process: 10
    port-numbers: 8100

此处我使用的开发环境是windows,如果上线到linux服务器,需要替换office-home目录(在上述4.4中已经进行了说明)
port-numbers 端口为文档转换的端口,可随意替换,但是保证不被占用
5.3

    @Lazy
    @Autowired
    private DocumentConverter converter;
/**
     * 传入完整的URL路径
     * @param input  word文件所在位置
     * @param dist   word转pdf后的文件位置
     * @throws OfficeException
     */
    @Override
    public String convert(String input, String dist) throws ServiceException {
        log.info("++++++++++传入:"+input);
        //源文件读取不到
        File srcFile = new File(input);
        if(!srcFile.exists()){
            throw new ServiceException("输入文件不存在!");
        }
        log.info("++++++++++目标文件:"+dist);
        //生成时要增加后.pdf后缀,否则无法转换
        File outputFile = new File(dist);
        //必须包含后缀才能转换
        try {
            converter.convert(srcFile)
                    .as(DefaultDocumentFormatRegistry.PDF)
                    .to(outputFile)
                    .execute();
            return dist;
        }catch (OfficeException e){
            throw new ServiceException("文件转换过程发生错误:"+e.getMessage());
        }
    }
	

注意:DocumentConverter 属于导入的依赖的中的接口文件
5.4 配置pdf 的访问路径

/**
     * 转换文档类型数据
     * @param provide
     * @param entity
     * @throws ClientException
     */
    private void convertDocx(CertGrant certGrant,String url) throws ClientException {
        // 本地上传
        LocalConfig conf = localUpService.getConfig();
        // 相对文件路径
        // 变成物理路径
        String path = this.getRealPath(conf, url);
        // 目标地址
        String dist = path+".pdf";
        // 返回物理路径(pdf的物理路径 此处没用)
        String rest = officeService.convert(path, dist);
        // 再变回访问路径
        String result = url + ".pdf";
        //TODO 转换Doc
        certGrant.setCertFilePdf(result);
//        this.saveOrUpdate(certGrant);
        return;
    }

注意:officeService.convert()方法返回的是原word文档的网络地址, 需要在后缀添加 “.pdf”,即可在网络中访问次pdf文件
如图所示
在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闪耀太阳a

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值