vue打开临时aspose生成的html,vue 实现 word、excel文件在线查看(利用aspose转换为pdf文件)...

前言:项目是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文件(通过后缀判断文件格式)

将下载的jar包导入项目(这里是用maven管理的),先在项目模块下新建lib文件夹,将jar包放进去后配置pom.xml文件使jar包可使用

jar包

com.aspose

aspose-slides

15.8.0

system

${basedir}/lib/aspose.slides-15.8.0.jar

com.aspose

aspose-cells

8.5.2

system

${basedir}/lib/aspose-cells-8.5.2.jar

com.aspose

aspose-words

16.8.0

system

${basedir}/lib/aspose-words-16.8.0.jar

引入license.xml授权配置文件至静态文件(resources)目录

Aspose.Total for Java

Aspose.Words for Java

Enterprise

20991231

20991231

8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7

sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=

将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文件所找到的方法,步骤确实稍为复杂,后面几步也没怎么贴代码,写该文章只是为了记录自己解决这个问题时熬的两天的过程。由于当时网上找的解决方法太多了,这里也没有贴出原网址,如文章写得有不合适地方的或者大佬们有更好的处理方法,欢迎赐教!

你可能感兴趣的:(vue打开临时aspose生成的html,vue 实现 word、excel文件在线查看(利用aspose转换为pdf文件)...)