前言:項目是vue+spring Boot項目,原本有直接查看PDF文件的功能(利用vue-pdf組件實現),現在需要增加上傳word、excel文件以及之后的查看、打印等一系列功能,此時vue-pdf組件也無法直接查看word/excel文件,于是在網上也查詢了許多直接查看word/excel文件的方法,但是都有各種局限性,綜合考慮之后有了如下思路
思路:
- 上傳word/excel文件至文件系統(使用的ftp服務器)時,同時保存一份在本地,將本地的文件轉換為同名的pdf文件,此時生成的pdf文件也在本地;
- 緊接著上傳該本地pdf文件至文件系統,與原word/excel文件同一文件夾;
- 刪除保存在本地的文件及生成的pdf文件;
- 在線查看文件時進行判斷,如為pdf文件直接查看,如為word、excel(后綴為doc/docx/xls/xlsx)文件,則將文件名后綴替換為 .pdf 后再進行查看。
該思路步驟確實比較繁瑣,但是結合項目實際以及網上找到的解決方案,處理起來比較合適,以下是解決的詳細步驟:
1. 利用aspose將上傳的word/excel文件轉換為pdf文件(通過后綴判斷文件格式)
- 下載aspose相關jar包:鏈接:https://pan.baidu.com/s/1EcWi5TU_Lqwr_EKTooi-cA
提取碼:xcnv -
將下載的jar包導入項目(這里是用maven管理的),先在項目模塊下新建lib文件夾,將jar包放進去后配置pom.xml文件使jar包可使用
jar包
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>15.8.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/aspose.slides-15.8.0.jar</systemPath>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
<scope>system</scope>
<systemPath>${basedir}/lib/aspose-cells-8.5.2.jar</systemPath>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>16.8.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/aspose-words-16.8.0.jar</systemPath>
</dependency>
- 引入license.xml授權配置文件至靜態文件(resources)目錄
<License>
<Data>
<Products>
<Product>Aspose.Total for Java</Product>
<Product>Aspose.Words for Java</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SubscriptionExpiry>20991231</SubscriptionExpiry>
<LicenseExpiry>20991231</LicenseExpiry>
<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
</Data>
<Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>
- 將word文件轉為pdf的方法
@Slf4j
public class WordToPDF {
//實現匹配文件授權
public static boolean matchLicense() {
boolean result = false;
InputStream is = WordToPDF.class.getClassLoader().getResourceAsStream("license.xml");
License wordLicense = new License();
try {
wordLicense.setLicense(is);
result = true;
} catch (Exception e) {
log.error("載入excel授權文件失敗");
e.printStackTrace();
}
return result;
}
public static void convert2PDF(File sourceFile, File targetFile) {
if (!matchLicense()){
throw new RuntimeException("匹配文件授權失敗!");
}
FileOutputStream os = null;
try {
os = new FileOutputStream(targetFile);
Document doc = new Document(sourceFile.getAbsolutePath());
doc.save(os, SaveFormat.PDF);
} catch (FileNotFoundException e) {
log.error("輸出到"+sourceFile.getAbsolutePath()+"錯誤:"+e);
} catch (Exception e) {
log.error("轉換word出錯:"+e);
}finally {
if(os!=null) {
try {
os.close();
} catch (IOException e) {
log.error("關閉輸出流出錯:"+e);
}
}
}
}
}
- 將excel文件轉為pdf的方法
@Slf4j
public class ExcelToPDF {
protected static boolean matchLicense() {
boolean result = false;
InputStream is = ExcelToPDF.class.getClassLoader().getResourceAsStream("license.xml");
License excelLicense = new License();
try {
excelLicense.setLicense(is);
result = true;
} catch (Exception e) {
System.err.println("載入excel授權文件失敗");
e.printStackTrace();
}
return result;
}
public static void convert2PDF(File sourceFile, File targetFile) {
if (!matchLicense()){
throw new RuntimeException("匹配文件授權失敗!");
}
FileOutputStream fileOs = null;
Workbook wb = null;
try {
fileOs = new FileOutputStream(targetFile);
wb = new Workbook(sourceFile.getAbsolutePath());
wb.save(fileOs, SaveFormat.PDF);
} catch (FileNotFoundException e) {
log.error("輸出到"+sourceFile.getAbsolutePath()+"錯誤:"+e);
e.getStackTrace();
} catch (Exception e) {
log.error("轉換word出錯:"+e);
}finally {
if(fileOs!=null) {
try {
fileOs.close();
} catch (IOException e) {
log.error("關閉輸出流出錯:"+e);
}
}
}
}
}
- 此時在項目文件夾就會生成轉換后的pdf文件,我們這里使用到的是ftp服務器,需要利用ftp的方法將該文件上轉至文件系統;
- 刪除本地的文件們;
- 此時用vue-pdf組件直接查看上傳的pdf文件即可達到目的;
- 如需刪除原上傳文件,同時也應該刪除轉化后的pdf文件。
目前發現的缺點:
- 在Linux系統下word轉換的pdf文件會亂碼,但網上也有很多解決方案
- excel轉pdf時,如原excel文件沒有繪制單元格邊框,則預覽及之后打印的文件無單元格格子
以上便是我在實現vue在線查看word、excel文件所找到的方法,步驟確實稍為復雜,后面幾步也沒怎么貼代碼,寫該文章只是為了記錄自己解決這個問題時熬的兩天的過程。由于當時網上找的解決方法太多了,這里也沒有貼出原網址,如文章寫得有不合適地方的或者大佬們有更好的處理方法,歡迎賜教!