当前位置: 代码网 > it编程>编程语言>Java > Springboot如何集成jodconverter做文档转换

Springboot如何集成jodconverter做文档转换

2024年08月15日 Java 我要评论
公司项目开发中,早期使用docx4j进行word转pdf,出现了很多格式紊乱、空格缩进、字体间距变大等问题。虽然针对空格缩进等处理,采取全角模式,进行了改善。但依旧还是会有很多解决不了的格式。一直在找

公司项目开发中,早期使用docx4j进行word转pdf,出现了很多格式紊乱、空格缩进、字体间距变大等问题。

虽然针对空格缩进等处理,采取全角模式,进行了改善。但依旧还是会有很多解决不了的格式。

一直在找一种新的方式进行替代,主要是:保证显示格式。

jodconverter 简介

这是一款利用操作系统中的office库,实现文档类型转换的工具。目前支持很多格式间的互相转换。

  • 这里不做太多的阐述,度娘、论坛等都有很多博客的说明。
  • 本次只是为了基本的测试与使用。

下载安装 libreoffice

libreoffice 下载地址

根据电脑对应的系统,选择指定系统版本的进行安装即可。

本次以windows进行演示,后期会增加linux的安装脚本。

代码演示

1、创建springboot项目工程并引入依赖

本次测试代码,结合docx模板数据填入的思想,进行doc文件内容填充,并将doc文件转换pdf处理。

往期回顾:

所以需要导入以下依赖:

<!-- docx 模板填入与导出doc -->
<dependency>
    <groupid>com.deepoove</groupid>
    <artifactid>poi-tl</artifactid>
    <version>1.5.0</version>
</dependency>

<!-- libreoffice 进行文件转换 -->
<dependency>
    <groupid>org.jodconverter</groupid>
    <artifactid>jodconverter-spring-boot-starter</artifactid>
    <version>4.4.4</version>
</dependency>
<dependency>
    <groupid>org.jodconverter</groupid>
    <artifactid>jodconverter-local</artifactid>
    <version>4.4.4</version>
</dependency>

2、配置

除了增加对应依赖文件之外,还需要增加application.properties文件的配置。

如下所示:

server.port=80

jodconverter.local.enabled=true
# libreoffice根目录
jodconverter.local.office-home=c:/program files/libreoffice
# 任务执行的超时时间
jodconverter.local.task-execution-timeout=86400000
# 任务队列的超时时间
jodconverter.local.task-queue-timeout=86400000
# 端口(线程)
jodconverter.local.port-numbers=2001,2002,2003
# 一个进程的超时时间
jodconverter.local.process-timeout=86400000

3、准备一个docx模板

并放置于resources/templates_report下,如下所示:

4、编写测试代码

如下所示:

package cn.xj.controller;

import com.deepoove.poi.xwpftemplate;
import lombok.extern.slf4j.slf4j;
import org.jodconverter.core.documentconverter;
import org.jodconverter.core.document.defaultdocumentformatregistry;
import org.jodconverter.core.office.officeexception;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.core.io.classpathresource;
import org.springframework.core.io.resource;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
import java.io.file;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.outputstream;
import java.util.hashmap;
import java.util.map;

@slf4j
@restcontroller
@requestmapping("/pdf")
public class testcontroller {

    @autowired
    private documentconverter documentconverter;

    @requestmapping("/test")
    public void test() throws ioexception {
        map<string, object> params = new hashmap<>();
        params.put("username","xiangjiao1");
        params.put("password","******");
        params.put("age",22);
        params.put("email","专注写bug测试中文");
        resource resource = new classpathresource("templates_report/001.docx");
        file file = resource.getfile();

        // 数据填充
        xwpftemplate template = xwpftemplate.compile(file).render(params);

        string docoutpath = system.getproperty("user.dir")+file.separator+"springboot-poi"+file.separator+"pdf"+file.separator+system.currenttimemillis()+ ".doc";
        outputstream outputstream = new fileoutputstream(docoutpath);
        template.write(outputstream);

        try {
            string pdfoutpath = system.getproperty("user.dir")+file.separator+"springboot-poi"+file.separator+"pdf"+file.separator+system.currenttimemillis()+".pdf";
            documentconverter.convert(new file(docoutpath)).to(new file(pdfoutpath)).as(defaultdocumentformatregistry.pdf).execute();
        } catch (officeexception e) {
            log.error("文档转换异常:{}", e.getmessage());
        }
    }
}

运行后的样式

linux 环境下安装 libreoffice

脚本如下所示:

#!/bin/bash
cd /tmp

install_redhat() {
   wget https://kkfileview.keking.cn/libreoffice_7.3.7_linux_x86-64_rpm.tar.gz -co libreoffice_7_rpm.tar.gz && tar -zxf /tmp/libreoffice_7_rpm.tar.gz && cd /tmp/libreoffice_7.3.7.2_linux_x86-64_rpm/rpms
   echo $?
   if [ $? -eq 0 ];then
     yum install -y libsm.x86_64 libxrender.x86_64  libxext.x86_64
     yum groupinstall -y  "x window system"
     yum localinstall -y *.rpm
     echo 'install finshed...'
   else
     echo 'download package error...'
   fi
}

install_ubuntu() {
   wget  https://kkfileview.keking.cn/libreoffice_7.3.7_linux_x86-64_deb.tar.gz  -co libreoffice_7_deb.tar.gz && tar -zxf /tmp/libreoffice_7_deb.tar.gz && cd /tmp/libreoffice_7.3.7.2_linux_x86-64_deb/debs
   echo $?
 if [ $? -eq 0 ];then
     apt-get install -y libxinerama1 libcairo2 libcups2 libx11-xcb1
     dpkg -i *.deb
     echo 'install finshed...'
  else
    echo 'download package error...'
 fi
}


if [ -f "/etc/redhat-release" ]; then
  yum install -y wget
  install_redhat
else
  apt-get install -y wget
  install_ubuntu
fi

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com