欢迎来提更好的意见
在线预览word文档
昨天下午组长告诉我,你去研究一下在线预览word文档,带着一脸懵去上网查资料,csdn,开源,知乎等。最常见的四种文件预览方法:
- kkfileview 文件在线预览 ,是在spring boot上搭建的,因为我项目没用到微服务所以放弃了解;
- officetohtml纯前端的方式,我不是专业的前端,虽然工作中我也是一个人负责前后端(前端:layui框架+js)等页面展示+后端,但是我尽量能后端编写就不会把值复杂化传给前端。所以我放弃了解 ;
- JODConverter ,这个里面提到一个可以用openoffice,实现原理就是:通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件流;这样就可以在浏览器上实现预览了。我也去了解并下载软件,但是下载实在太忙,我又放弃了,想有没有更简单的方法:“不下载软件,不去部署环境,最多就是加个包”,所以又放弃了解;
- Aspose ,这个忘记为啥没再去了解了哈哈;
- 最后在各种java群里问,有没有更简单的方式,结果的确有个超级简单的方法,Java 使用wps将word文件转换pdf文件 ;
Java 使用wps将word文件转换pdf文件
贴上代码:
/***
* 判断需要转化文件的类型(Excel、Word、ppt)
*
* @param inputFile
*/
public String convertToPDF(String inputFile) {
//判断原文件是否存在
File file = new File(inputFile);
if (file.exists()) {
String kind = getFileSufix(inputFile);
if (kind.equals("pdf")) {
return inputFile;//原文件就是PDF文件
}
String pdfFile = inputFile.substring(0, inputFile.lastIndexOf(".")) + ".pdf";
if (kind.equals("doc")||kind.equals("docx")||kind.equals("txt")) {
wordToPDF(inputFile,pdfFile);
}else if (kind.equals("ppt")||kind.equals("pptx")||kind.equals("pptm")||kind.equals("ppsx")) {
pptToPDF(inputFile,pdfFile);
}else if(kind.equals("xls")||kind.equals("xlsx")){
ExToPDF(inputFile,pdfFile);
}else{
return inputFile;//原文件是其它格式文件
}
//返回创建的pdf
return pdfFile;
} else {
//System.out.println("原文件不存在!");
return inputFile;
}
}
依赖包:
<dependency>
<groupId>net.sf.jacob-project</groupId>
<artifactId>jacob</artifactId>
<version>1.14.3</version>
</dependency>
<dependency>
<groupId>com.jacob</groupId>
<artifactId>1.0.0</artifactId>
<scope>system</scope>
<systemPath>K:\jacob-1.20\jacob.jar</systemPath>
</dependency>
记得还有下载:jacob-1.20.zip 解压以后,会看到五个文件,但是都不用管它,只需要里面的jar,我是window的所以用…x64.dll。分别放到1: --dll文件 ,放到自己的tomacat/bin下面 2:–dll文件 放到C:/windows/system32下面 3 --dll ,jar 这个我因为我项目下面没有WEB-INF/lib,所以我并没有添加。
启动项目,报错了,,,,,,,,
java.lang.NoClassDefFoundError: Could not initialize class com.jacob.com.ComThread
然后看到一篇博主的就改了一下就好了,目前还没更新到线上,本地测试是可以的。
转成PDF
1、贴代码:
//设置响应内容类型为PDF类型
response.setContentType("application/pdf;charset=UTF-8");
ServletOutputStream sos = response.getOutputStream();
//不在网页中打开,而是直接下载该文件,下载后的文件名为“Example.pdf”
File pdf = null;
FileInputStream fis = null;
byte[] buffer = new byte[1024*1024];
if(filePath!=null){ //pdf文件路径
pdf = new File(filePath);
response.setContentLength((int) pdf.length());
fis = new FileInputStream(pdf);
int readBytes = -1;
while((readBytes = fis.read(buffer, 0, 1024*1024)) != -1){
sos.write(buffer, 0, 1024*1024);
}
sos.close();
fis.close();
}
在线预览word(转成pdf)前端展示
前端js请求并把id带到后台,用
String id = (String) request.getSession().getAttribute("页面名称xxxxx");
页面用iframe打开
<iframe id="test1" style="width: 100%;height: 80vh;"></iframe>
js请求:
//pdf预览
$(".withExport ").on("click",function(){
var fileId= $(this).attr("fileId");
$.get( contextPath + 'xxx/pdfs?'+'id='+fileId, {}, function(str){
var addIndex = layer.open({
type: 1,
title:'pdf',
content: str,
area: ["70%","750px"]
});
$.get( contextPath + '/selectPDFs?'+ 'id='+fileId, {}, function(str){
$("#test1").attr(
"src",contextPath + "/pdf/web/viewer.html?file=/ams/displayPDFs?"
);
});
});
});
以上就是在线预览word和pdf的思路,如果需要代码,可以留言,我整理文档发给你。溜啦溜啦,还要部署到线上测试。
本文含有隐藏内容,请 开通VIP 后查看