当前位置:   article > 正文

spring boot快速集成Apache OpenOffice

openoffice

工作需求:提供一个接口,页面上的文档不仅支持下载,还可以在浏览器上可以预览的功能。我们知道pdf是支持浏览器在线预览的功能的,所以只要把其他格式的文档转换为pdf是不是就间接的展示了呢?

一、openoffice简介:

OpenOffice.org 是一套跨平台的办公室软件套件,能在Windows、Linux、MacOS X (X11)和 Solaris 等操作系统上执行。它与各个主要的办公室软件套件兼容。OpenOffice.org 是自由软件,任何人都可以免费下载、使用及推广它。

看看官网对它的介绍
在这里插入图片描述

二、基本环境

下载windows版本的Apache_OpenOffice_4.1.10_Win_x86_install_zh-CN.exe为例,这个是32位的

1. 下载与安装Oppenoffice SDK

官方下载地址:https://www.openoffice.org/download/

在这里插入图片描述
下载完成,双击进行安装,安装路径自己指定,我的安装到了c盘,执行文件安装了两个文件夹,我们主要用的是sdk,安装后如下图:
在这里插入图片描述

  • OpenOffice 4.1.10 目录
    在这里插入图片描述
  • OpenOffice 4 目录
    在这里插入图片描述
2. Office运行使用java运行时环境 :版本jre1.8.0_201

可以是open jre 、oracle jre

3. 启动openoffice服务

window命令

  1. 进入安装目录program目录下
  2. 启动服务
cd C:\Program Files (x86)\OpenOffice 4\program  
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard 
  • 1
  • 2

不报错,为启动成功。

三、 集成代码

1.maven核心jar包
<!--jodconverter 核心包 -->
		<!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-core -->
		<dependency>
			<groupId>org.jodconverter</groupId>
			<artifactId>jodconverter-core</artifactId>
			<version>4.2.2</version>
		</dependency>

		<!--springboot支持包,里面包括了自动配置类 -->
		<!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-spring-boot-starter -->
		<dependency>
			<groupId>org.jodconverter</groupId>
			<artifactId>jodconverter-spring-boot-starter</artifactId>
			<version>4.2.2</version>
		</dependency>

		<!--jodconverter 本地支持包 -->
		<!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-local -->
		<dependency>
			<groupId>org.jodconverter</groupId>
			<artifactId>jodconverter-local</artifactId>
			<version>4.2.2</version>
		</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
2. yml配置文件
jodconverter:
  local:
    enabled: true
    office-home: C:\Program Files (x86)\OpenOffice 4 #安装路径
    max-tasks-per-process: 10
    port-numbers: 8100 #端口号
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
3.controller接口
	
    @Autowired
    private DocumentConverter converter;


    @GetMapping("testPreview")
    public void toPdfFile(HttpServletResponse response) {
        /*需要转换的文件*/
        // word文件
        File file = new File("f:\\test.doc");
        // 图片
//        File file = new File("f:\\铁山靠.png");
        // excel文件
//        File file = new File("f:\\test.xlsx");

        try {
            // 转换之后文件生成的地址
            File newFile = new File("D:/testMyDoc");
            if (!newFile.exists()) {
                newFile.mkdirs();
            }
            // pdf文件生成保存的路径
            String savePath="D:/testMyDoc/";
            String fileName="JCccc"+ UUID.randomUUID().toString().replaceAll("-","").substring(0,6);
            // pdf文件后缀
            String fileType=".pdf";
            // 将这三个拼接起来,就是我们最后生成文件保存的完整访问路径了
            String newFileMix=savePath+fileName+fileType;

            // 文件转化
            converter.convert(file).to(new File(newFileMix)).execute();
            // 使用response,将pdf文件以流的方式发送的前端浏览器上
            ServletOutputStream outputStream = response.getOutputStream();
            // 读取文件
            InputStream in = new FileInputStream(new File(newFileMix));
            // copy流数据,i为字节数
            int i = IOUtils.copy(in, outputStream);
            in.close();
            outputStream.close();
            System.out.println("流已关闭,可预览,该文件字节大小:"+i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • PDF在线预览则不需要进行转换,如下
@GetMapping("testPreview")
    public void toPdfFile(HttpServletResponse response) {
        /*需要转换的文件*/
        // pdf
		String pdfpath ="F:\\test.pdf";

        try {
            
            // 使用response,将pdf文件以流的方式发送的前端浏览器上
            ServletOutputStream outputStream = response.getOutputStream();
            // 读取文件
            InputStream in = new FileInputStream(new File(pdfpath ));
            // copy流数据,i为字节数
            int i = IOUtils.copy(in, outputStream);
            in.close();
            outputStream.close();
            System.out.println("流已关闭,可预览,该文件字节大小:"+i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 效果图如下:
    带图片的word转pdf在这里插入图片描述
    图片转pdf
    在这里插入图片描述
    excel转pdf(这个把表格吃了居然)
    在这里插入图片描述
    pdf直接转成流预览:
    在这里插入图片描述

四、总结

集成比较快速,但不知道是否满足我们的需求,上面的是个比较简单的demo吧。官网上把它说的这么好, 如果你从事IT行业,OpenOffice 对你来说是一个很好的业务。其灵活的文本编辑器、强大的电子表格、绘图、数据库访问等等可以满足办公软件的所有需求。 对于我们更好的发掘它提供了动力。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/428604
推荐阅读
相关标签
  

闽ICP备14008679号