前言:項(xiàng)目是vue+spring Boot項(xiàng)目帕涌,原本有直接查看PDF文件的功能(利用vue-pdf組件實(shí)現(xiàn))噩茄,現(xiàn)在需要增加上傳word下面、excel文件以及之后的查看、打印等一系列功能绩聘,此時vue-pdf組件也無法直接查看word/excel文件沥割,于是在網(wǎng)上也查詢了許多直接查看word/excel文件的方法,但是都有各種局限性凿菩,綜合考慮之后有了如下思路
思路:
- 上傳word/excel文件至文件系統(tǒng)(使用的ftp服務(wù)器)時机杜,同時保存一份在本地,將本地的文件轉(zhuǎn)換為同名的pdf文件衅谷,此時生成的pdf文件也在本地椒拗;
- 緊接著上傳該本地pdf文件至文件系統(tǒng),與原word/excel文件同一文件夾;
- 刪除保存在本地的文件及生成的pdf文件蚀苛;
- 在線查看文件時進(jìn)行判斷在验,如為pdf文件直接查看,如為word枉阵、excel(后綴為doc/docx/xls/xlsx)文件译红,則將文件名后綴替換為 .pdf 后再進(jìn)行查看。
該思路步驟確實(shí)比較繁瑣兴溜,但是結(jié)合項(xiàng)目實(shí)際以及網(wǎng)上找到的解決方案,處理起來比較合適耻陕,以下是解決的詳細(xì)步驟:
1. 利用aspose將上傳的word/excel文件轉(zhuǎn)換為pdf文件(通過后綴判斷文件格式)
- 下載aspose相關(guān)jar包:鏈接:https://pan.baidu.com/s/1EcWi5TU_Lqwr_EKTooi-cA
提取碼:xcnv -
將下載的jar包導(dǎo)入項(xiàng)目(這里是用maven管理的)拙徽,先在項(xiàng)目模塊下新建lib文件夾,將jar包放進(jìn)去后配置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授權(quán)配置文件至靜態(tài)文件(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文件轉(zhuǎn)為pdf的方法
@Slf4j
public class WordToPDF {
//實(shí)現(xiàn)匹配文件授權(quán)
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授權(quán)文件失敗");
e.printStackTrace();
}
return result;
}
public static void convert2PDF(File sourceFile, File targetFile) {
if (!matchLicense()){
throw new RuntimeException("匹配文件授權(quán)失斒膘怕!");
}
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("轉(zhuǎn)換word出錯:"+e);
}finally {
if(os!=null) {
try {
os.close();
} catch (IOException e) {
log.error("關(guān)閉輸出流出錯:"+e);
}
}
}
}
}
- 將excel文件轉(zhuǎn)為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授權(quán)文件失敗");
e.printStackTrace();
}
return result;
}
public static void convert2PDF(File sourceFile, File targetFile) {
if (!matchLicense()){
throw new RuntimeException("匹配文件授權(quán)失敗召庞!");
}
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("轉(zhuǎn)換word出錯:"+e);
}finally {
if(fileOs!=null) {
try {
fileOs.close();
} catch (IOException e) {
log.error("關(guān)閉輸出流出錯:"+e);
}
}
}
}
}
- 此時在項(xiàng)目文件夾就會生成轉(zhuǎn)換后的pdf文件岛心,我們這里使用到的是ftp服務(wù)器,需要利用ftp的方法將該文件上轉(zhuǎn)至文件系統(tǒng)篮灼;
- 刪除本地的文件們忘古;
- 此時用vue-pdf組件直接查看上傳的pdf文件即可達(dá)到目的;
- 如需刪除原上傳文件诅诱,同時也應(yīng)該刪除轉(zhuǎn)化后的pdf文件髓堪。
目前發(fā)現(xiàn)的缺點(diǎn):
- 在Linux系統(tǒng)下word轉(zhuǎn)換的pdf文件會亂碼,但網(wǎng)上也有很多解決方案
- excel轉(zhuǎn)pdf時娘荡,如原excel文件沒有繪制單元格邊框干旁,則預(yù)覽及之后打印的文件無單元格格子
以上便是我在實(shí)現(xiàn)vue在線查看word、excel文件所找到的方法炮沐,步驟確實(shí)稍為復(fù)雜争群,后面幾步也沒怎么貼代碼,寫該文章只是為了記錄自己解決這個問題時熬的兩天的過程大年。由于當(dāng)時網(wǎng)上找的解決方法太多了换薄,這里也沒有貼出原網(wǎng)址,如文章寫得有不合適地方的或者大佬們有更好的處理方法鲜戒,歡迎賜教专控!