Springboot 超简单实现在线预览Word文档 doc、xlsx、pdf、txt等

前言

PDF、TXT 只要资源可访问,根本就不需要进行任何处理,直接访问查看就完事了。

也是因为这个PDF可以直接查看(现在浏览器基本支持了),那么我们实现Word文档在线预览,其实也是 把WORD文档 复制一份生成一份供预览的 PDF文件而已。

先看看效果:

正文

这篇实例,实现在线预览WORD文档,分两步:

一. 安装OpenOffice

二.写点小代码

一. 安装OpenOffice

不要看到安装东西就觉得麻烦,因为这个安装不需要做任何配置,你只需要下载,选择安装地址,一直下一步 即可。

那么下载地址,我也给你了(Windows版, 线上linux服务器安装的话随便搜一下 Linux 安装 openoffice 就可以):

百度网盘链接:
https://pan.baidu.com/s/17y5O7IqkB0YxA6tWdjJNvA

提取码:

7u23

安装完之后,你只需要知道你安装在哪里了即可,就像我:

我的安装地址是(因为一会项目代码需要用到):  

C:\Program Files (x86)\OpenOffice 4

二.写点小代码

首先,pom.xml文件加上核心的依赖(我的springboot版本用的 2.1.4.RELEASE):

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--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>

        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

接着是 application.yml文件:

server:
  port: 8089

jodconverter:
  local:
    enabled: true
    office-home: C:\Program Files (x86)\OpenOffice 4
    max-tasks-per-process: 10
    port-numbers: 8100

然后就是实现代码,非常简短,我们直接写在接口里,方便我测试(你们自己可以提出来):

 新建一个 TestController.java:

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.jodconverter.DocumentConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {


    @Autowired
    private DocumentConverter converter;  //用于转换

    @ResponseBody
    @RequestMapping("testPreview")
    public void toPdfFile(HttpServletResponse response) {
        File file = new File("D:\\testMyDoc\\doc\\testJc.docx");//需要转换的文件
        try {
            File newFile = new File("D:/testMyDoc");//转换之后文件生成的地址
            if (!newFile.exists()) {
                newFile.mkdirs();
            }
            String savePath="D:/testMyDoc/"; //pdf文件生成保存的路径
            String fileName="JCccc"+UUID.randomUUID().toString().replaceAll("-","").substring(0,6);
            String fileType=".pdf"; //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));// 读取文件
            int i = IOUtils.copy(in, outputStream);   // copy流数据,i为字节数
            in.close();
            outputStream.close();
            System.out.println("流已关闭,可预览,该文件字节大小:"+i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

代码简单分析:

好了,不多说,到这里已经实现了。

那么我们简单测试一下,运行项目:

这是代码里面写死的需要预览的doc文件:

然后我们在浏览器访问接口  http://localhost:8089/testPreview  ,可以看到预览没问题:

看一眼,我们生成的用于预览pdf文件:

控制台:

同样如果你需要在线预览Excel表的内容,那么也是一样只需要把对应的文件xlsx路径传到接口里面即可,如:

在线预览:

ps:

文件越大,实现预览就越久转换时间也就越久.

那么如果想用户体验更好,怎么办?  

前文提到了pdf是直接可以预览的,我们也是将word转为pdf而已,所以如果在客户上传完成的时候,可以异步执行转换的代码,对这个word文档直接去生成对应的pdf,然后绑定关系存储一下。

当用户想预览时,直接查出来对于的pdf文件,然后拼接成资源路径,直接访问即可。

如:

当然,这只是我随口说的一种方式,一定程度去改善预览的体验。

还有很多方式,只要你明白预览实现的原理,那么方案自己设计就好。

好的,该篇就到这吧。

猜你喜欢

转载自blog.csdn.net/qq_35387940/article/details/108752041
今日推荐