Springboot 使用 OpenOffice 實現附件在線預覽功能

OpenOffice 是 Apache 開源的一個辦公組件,可以直接到官網下載使用。適用windows、linux、mac等各大平臺,當然對我們程序員來說,肯定不會下載下來用用就完了。我們要在代碼中使用她,實現一些 web 項目中的附件預覽功能。

一、OpenOffice安裝

安裝的話就不細說了,直接到官網下載,一路next點下去就行,沒啥難度。

二、新建boot項目

這裏簡單建一個springboot的項目,引入這幾個轉換包,

        <!-- jodconverter -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-core</artifactId>
            <version>4.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-local</artifactId>
            <version>4.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-spring-boot-starter</artifactId>
            <version>4.3.0</version>
        </dependency>

隨後,配置文件 application-dev.yml,

server:
  port: 8085

#jodconverter
jodconverter:
  local:
    enabled: true
    office-home: D:/dev/tools/OpenOffice 4
    max-tasks-per-process: 10
    port-numbers: 8100

三、Controller 調用

package com.my.cloud.previewservice.controller;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.jodconverter.core.DocumentConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @Author: 技術大咖秀
 * @Date: 2020/3/27 11:13
 * motto: Saying and doing are two different things.
 */
@Controller
public class PreviewController {

    @Autowired
    private DocumentConverter converter; // 轉換器
    @Autowired
    private HttpServletResponse response;

    @RequestMapping("/home")
    public String homePage(){
        return "index";
    }

    @RequestMapping("/test")
    public String test(){
        return "test";
    }

    @RequestMapping("/toPdfFile")
    public String toPdfFile(){
        File file = new File("D:/test/excel.xlsx");//需要轉換的文件
        try {
            File newFile = new File("D:/obj-pdf");//轉換之後文件生成的地址
            if (!newFile.exists()) {
                newFile.mkdirs();
            }
            //文件轉化
            converter.convert(file).to(new File("D:/obj-pdf/hello.pdf")).execute();
            //使用response,將pdf文件以流的方式發送的前段
            ServletOutputStream outputStream = response.getOutputStream();
            InputStream in = new FileInputStream(new File("D:/obj-pdf/hello.pdf"));// 讀取文件
            // copy文件
            int i = IOUtils.copy(in, outputStream);
            System.out.println(i);
            in.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "This is to pdf";
    }

}

四、測試

新建測試文件,docx、xlsx、pptx 都試一下。

啓動項目訪問地址: http://localhost:8085/toPdfFile

測試成功。

PS。IE瀏覽器的話需要pdf.js(可百度下載)。

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章