• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Libreoffice整合SpringBoot只需这一篇

武飞扬头像
社会搬运工
帮助1

CentOs安装Libreoffice

1、去官网下载Libreoffice,选择rpm安装版本的,上传到服务器’

注意:linux rpm版本
学新通

tar -xvf LibreOffice_7.5.1_Linux_x86-64_rpm.tar.gz
1. cd LibreOffice_7.5.1_Linux_x86-64_rpm/RPMS 
2. yum localinstall *.rpm

2、安装成功后会在 /opt/ 下生产一个 libreof

fice7.5 的文件夹,我们需要的启动命令是:/opt/libreoffice7.5/program/soffice
注意:访问端口,自行修改,如需远程使用,请打开防火墙

nohup /opt/libreoffice7.5/program/soffice -headless -accept="socket,host=0.0.0.0,port=8100;urp;" -nofirststartwizard &
1.通过 ps -ef |grep soffice 获取pid
2.通过kill -9 pid 关闭服务

3、 Linux本地测试文档转换命令

自行修改转换格式,注意文件路径和转换后路径

 /opt/libreoffice7.5/program/soffice --headless --invisible --convert-to pdf 文件所在路径 --outdir 转换后路径

整合springboot

1、导入maven

jodconverter2.2.1:不支持docx等文件的转换
jodconverter2.2.2: Maven中央仓库只有到2.2.1的版本
jodconverter2.2.2jar包,提取码:2x79 ,请自行导入私有库。

<dependency>
  <groupId>com.artofsolving</groupId>
  <artifactId>jodconverter</artifactId>
  <version>2.2.2</version>
  <scope>system</scope> 
  <systemPath>${basedir}/src/main/resources/lib/jodconverter-2.2.2.jar</systemPath> <!--项目根目录下的lib文件夹下-->
</dependency>
<dependency>
  <groupId>org.openoffice</groupId>
  <artifactId>juh</artifactId>
  <version>4.1.2</version>
</dependency>
<dependency>
  <groupId>org.openoffice</groupId>
  <artifactId>ridl</artifactId>
  <version>4.1.2</version>
</dependency>
<dependency>
  <groupId>org.openoffice</groupId>
  <artifactId>unoil</artifactId>
  <version>4.1.2</version>
</dependency>
<dependency>
  <groupId>org.jodconverter</groupId>
  <artifactId>jodconverter-spring-boot-starter</artifactId>
  <version>4.2.0</version>
</dependency>
学新通

2、使用

配置安装服务器地址,以及启用到端口

    @Value("${lesoft.libreoffce.location:127.0.0.1}")
    private String libreoffceLocation;
    @Value("${lesoft.libreoffce.port:8100}")
    private Integer libreoffceProt;
HttpServletResponse response = SpringContext.currentResponse();、
// 设置头信息
String fileName = UUID.randomUUID().toString()   ".pdf";
response.setHeader("Content-Disposition", "attachment; filename=\""   fileName   "\"");
response.setContentType("application/pdf");
OutputStream outputStream = response.getOutputStream();
// 获取文档输入流,根据业务自行修改
ByteArrayInputStream inputStream = getNiceXWPFDocByInputStream(doc);
doDocumentConvert(inputStream,outputStream, "docx","pdf");


/**
* 文档转换为输入流
* @param doc
* @return
* @throws IOException
*/
private  ByteArrayInputStream getNiceXWPFDocByInputStream(NiceXWPFDocument doc) throws IOException {
    // 获取文档对象
    XWPFDocument document = doc.getXWPFDocument();
    //二进制OutputStream
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    document.write(baos);//文档写入流

    //OutputStream写入InputStream二进制流
    ByteArrayInputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
    return inputStream;
}

public void doDocumentConvert(InputStream inputStream, OutputStream outputStream, String form, String to) {
    // 建立连接,根据配置文件获取
    SocketOpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
    try {
        connection.connect();
        System.out.println("获取连接成功!");
    } catch (ConnectException e) {
        System.out.println("获取连接失败!");
        e.printStackTrace();
    }
	// 转换
    StreamOpenOfficeDocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
    // 转换格式
    DocumentFormat docDocumentFormat = (new DefaultDocumentFormatRegistry()).getFormatByFileExtension(form);
    DocumentFormat pdfDocumentFormat = (new DefaultDocumentFormatRegistry()).getFormatByFileExtension(to);

    // 多种转换方式,文件方式,流方式
    converter.convert(inputStream,docDocumentFormat, outputStream,pdfDocumentFormat);
    // 关闭连接
    connection.disconnect();
}
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhifkfgc
系列文章
更多 icon
同类精品
更多 icon
继续加载