前言
PDF、TXT 隻要資源可通路,根本就不需要進行任何處理,直接通路檢視就完事了。
也是因為這個PDF可以直接檢視(現在浏覽器基本支援了),那麼我們實作Word文檔線上預覽,其實也是 把WORD文檔 複制一份生成一份供預覽的 PDF檔案而已。
先看看效果:
正文
這篇執行個體,實作線上預覽WORD文檔,分兩步:
一. 安裝OpenOffice
二.寫點小代碼
一. 安裝OpenOffice
不要看到安裝東西就覺得麻煩,因為這個安裝不需要做任何配置,你隻需要下載下傳,選擇安裝位址,一直下一步 即可。
那麼下載下傳位址,我也給你了(Windows版, 線上linux伺服器安裝的話随便搜一下 Linux 安裝 openoffice 就可以):
百度網盤連結:
百度網盤 請輸入提取碼
提取碼:
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檔案,然後拼接成資源路徑,直接通路即可。
如: