【open office+jodconverter】文件预览

一、环境/工具

  1、Windows10 64

  2、Apache OpenOffice 3.3.0 for Windows( OOo_3.3.0_Win_x86_install-wJRE_zh-CN.exe

  3、jdk8

  4、springboot项目工程

 

二、依赖

复制代码
        <!--jodconverter-core-->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-core</artifactId>
            <version>4.3.0</version>
        </dependency>
        <!--jodconverter-local-->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-local</artifactId>
            <version>4.3.0</version>
        </dependency>
        <!--jodconverter-springboot-->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-spring-boot-starter</artifactId>
            <version>4.3.0</version>
        </dependency>
复制代码

 

三、配置属性文件

# jodconverter
jodconverter.local.enabled=true
# 开启openoffice进程对应的端口
jodconverter.local.port-numbers=8200

 

四、编写测试用接口

复制代码
package com.example.controller;

import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.office.OfficeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * Description:文件转pdf预览
 * Package:com.example.controller
 *
 * @author lightbc
 * @version 1.0
 */
@RestController
@RequestMapping("/preview")
public class PreviewController {
    @Autowired
    private DocumentConverter converter;
    private String filePath="用于转化的文件全路径(例:d:/xxx/1.docx)";
    private String newFilePath="新生成的文件的存放目录(例:d:/temp)";
    private String newFileName="test.pdf";

    @RequestMapping(value = "/convert/file/2/pdf")
    public void convertFileToPDF(HttpServletResponse response){
        File file=new File(filePath);
        InputStream in;
        OutputStream out;
        File tempFile=new File(newFilePath);
        if(!tempFile.exists()){
            tempFile.mkdirs();
        }
        File newFile=new File(tempFile.getAbsolutePath()+File.separator+newFileName);
        // 清除首部空白行
        response.reset();
        try {
            converter.convert(file).to(newFile).execute();
            in=new BufferedInputStream(new FileInputStream(newFile));
            byte[] b=new byte[in.available()];
            in.read(b);
            out=response.getOutputStream();
            out.write(b);
            in.close();
            out.flush();
            out.close();
        } catch (OfficeException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
复制代码

 

五、文件预览效果

 

 

六、总结

  1、Apache OpenOffice 3.3.0以上的Windows版本需要单独下载32位JRE运行环境。

  2、以上是比较简单的文件预览功能,如有其他需求需自行拓展。

posted @   lightbc  阅读(672)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示