javaweb项目实现文档在线预览详细步骤
实现文档在线预览不可下载,右键无法下载和打印。
所以需要把源文档转换成pdf,再pdf转swf,最后展示swf实现该目的。页面传的是一个源文件的id(系统不能通过这个id来下载源文件,所以下载源文件的时候需要对文件ID和时间有效期做加密签名认证),系统转换成swf后给出swf的id的加密签名认证后的id,并现在该id的使用次数或者有效期,已实现该id用过或者过期后就时效。具体的逻辑这里不做叙述,后面有机会在我博客其他文章会有详细加密代码。
资料准备
-
OpenOffice
上官网下载,不好下载,下载了很多次都下载不下来,中间老是断掉。
后面忘记在哪里下载了一个目前最新的英文版
Apache_OpenOffice_4.1.7_Linux_x86-64_install-rpm_en-US.tar.gz
下载地址
https://download.csdn.net/download/qq_40726812/11933167 -
swftools
这个很好下载直接上官网下载最新linux版本swftools-0.9.2.tar.gz
http://www.swftools.org/download.html -
flexpaper
也不好下载,我这里是拷贝其他朋友的包,没看出是什么版本的,不过可以使用。下载地址如下
https://download.csdn.net/download/qq_40726812/11933331
软件安装
代码实现是比较简单,主要的精力是花在Linux的环境搭建上,就是openoffice和swftools的按照,我们项目测试环境给的帐号没有sudo权限,还需要申请使用sudo权限。安装步骤可以搜索网上其他博客,有很多,这里就不做引用。完成安装后做的验证正常情况下是可以的,需要注意以下几点
- openoffice或者swftools操作转pdf或者pdf转swf时,报无法操作文件,应该是权限问题,需要授权,暴力的方式就是chmod -R 777.
- 转pdf或者转swf时字体乱码,应该是字体没安装的问题,搜索如何安装linux字体后解决。
java代码实现
openoffice实现文档转PDF
- 导入依赖包
使用jodconverter-local包可以实现本地openoffice服务的调用,如果使用远程主机的openoffice则需要导入jodconverter-online包,另外4.1.0版本是用jdk1.7来编译的(由于本项目用jdk1.7,兼容不了高版本),如果你的项目是用1.8的jdk,可以下载最新版本的jodconverter-local。
<!-- 文档转PDF依赖 -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.1.0</version>
</dependency>
- 编写转换代码
非常简单,几行代码就搞定了
public class FileConvertUtils {
/**
*
* @param sourceFilePath office文件路径
* @param targetFilePath pdf文件路径
* @throws OfficeException
*/
public static void fileConvertPdf(String sourceFilePath, String targetFilePath) throws Exception {
// 从配置表中获取
CodeOptionsServiceImpl codeOptionsServiceImpl = PubUtils.getBeanByName("codeOptionsServiceImpl", CodeOptionsServiceImpl.class) ;
String open_office_home = codeOptionsServiceImpl.getSystemConfigByCode("OPEN_OFFICE_HOME") ;
String open_office_server_port = codeOptionsServiceImpl.getSystemConfigByCode("OPEN_OFFICE_SERVER_PORT") ;
if(ValidateUtils.isNullStr(open_office_server_port)) {
throw new Exception("服务器未配置转pdf服务端口") ;
}
fileConvertPdf(sourceFilePath, targetFilePath, open_office_home,Integer.parseInt(open_office_server_port));
}
/**
*
* @param sourceFilePath office文件路径
* @param targetFilePath pdf文件路径
* @param officeHome openoffice转pdf服务的路径 /opt/openoffice4
* @param portNumber openoffice转pdf服务的端口 8100
* @throws OfficeException
*/
public static void fileConvertPdf(String sourceFilePath, String targetFilePath,String officeHome,int portNumber) throws OfficeException {
OfficeManager officeManager = LocalOfficeManager.builder().officeHome(officeHome).portNumbers(portNumber)
.maxTasksPerProcess(10).build();
officeManager.start();
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.convert(new File(sourceFilePath), new File(targetFilePath));
officeManager.stop();
}
}
swftools实现pdf转swf
/**
* - 文件转换工具类
*
* @author chenk
*
*/
public class FileConvertUtils {
/**
* - 将pdf文件转换为swf文件
* @param sourceFilePath
* @param targetFilePath
* @throws Exception
*/
public static void pdfConvertSwf(String sourceFilePath, String targetFilePath) throws Exception {
// 从配置表中获取
CodeOptionsServiceImpl codeOptionsServiceImpl = PubUtils.getBeanByName("codeOptionsServiceImpl", CodeOptionsServiceImpl.class) ;
String swfToolsPath = codeOptionsServiceImpl.getSystemConfigByCode("SWF_TOOLS_PATH") ;
if(ValidateUtils.isNullStr(swfToolsPath)) {
throw new Exception("服务器未配置pdf转swf工具路径") ;
}
pdfConvertSwf(sourceFilePath, targetFilePath, swfToolsPath);
}
/**
* - 将pdf文件转换为swf文件
* @param sourceFilePath
* @param targetFilePath
* @param swfToolsPath pdf转swf工具命令路径 D:/App/SWFTools/pdf2swf.exe
* @throws Exception
*/
public static void pdfConvertSwf(String sourceFilePath, String targetFilePath,String swfToolsPath) throws Exception {
// 构建命令
List<String> command = new ArrayList<String>();
command.add(swfToolsPath);
command.add("-i");
// 需要使用文件的绝对路径
command.add(new File(sourceFilePath).getAbsolutePath());
command.add("-s");
command.add("flashversion=9");
command.add("-o");
command.add(new File(targetFilePath).getAbsolutePath());
command.add("-f");
// 调用操作系统安装的swf工具
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(command);
Process process = processBuilder.start();
SwfToolInputStreamWathThread swfToolInputStreamWathThread = new SwfToolInputStreamWathThread(process.getInputStream());
swfToolInputStreamWathThread.start();
SwfToolInputStreamWathThread swfToolErrorStreamWathThread = new SwfToolInputStreamWathThread(process.getErrorStream());
swfToolErrorStreamWathThread.start();
try {
process.waitFor();
swfToolInputStreamWathThread.setOver(true);
swfToolErrorStreamWathThread.setOver(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
前端使用flexpaper展示swf文件
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>${swfFile.fileName}</title>
<%@include file="/WEB-INF/jsp/pub/includes/meta.jsp" %>
<script type="text/javascript" src="${contextPath}/static/jsframe/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="${contextPath}/static/jsframe/flexpaper/flexpaper.js"></script>
<script type="text/javascript" src="${contextPath}/static/jsframe/flexpaper/flexpaper_handlers.js"></script>
<style type="text/css" media="screen">
html, body {
height: 100%;
}
body {
margin: 0;
padding: 0;
overflow: auto;
}
#flashContent {
display: none;
}
</style>
</head>
<body>
<div style="margin: 5px;">
<a id="viewerPlaceHolder" style="width: 100%; height: 100%; display: block"></a>
</div>
</body>
<script type="text/javascript">
$(function(){
var height = $(window).height();
$("#viewerPlaceHolder").height((height-10)+"px");
})
// 参数
var args = {
config:{
jsDirectory:'${contextPath}/static/jsframe/flexpaper/',
SwfFile:'${contextPath}/file/downLoadFile.action?fileIds=${swfFile.fileId}',
Scale : 0.6,
// 变焦过渡
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
// 缩放滑块-移动的缩放基础[工具栏]
ZoomInterval : 0.2,
// 自适应页面
FitPageOnLoad : true,
// 自适应宽度
FitWidthOnLoad : true,
// 全屏按钮-新页面全屏[工具栏]
FullScreenAsMaxWindow : false,
// 分割加载
ProgressiveLoading : false,
// 最小缩放
MinZoomSize : 0.2,
// 最大缩放
MaxZoomSize : 3,
// SearchMatchAll : true,
// 初始显示模式(SinglePage,TwoPage,Portrait)
InitViewMode : 'Portrait',
// 显示模式工具栏是否显示
ViewModeToolsVisible : true,
// 缩放工具栏是否显示
ZoomToolsVisible : true,
// 跳页工具栏
NavToolsVisible : true,
CursorToolsVisible : false,
SearchToolsVisible : true,
PrintPaperAsBitmap:false,
localeChain: 'zh_CN',
PrintEnabled:false,
PrintToolsVisible:false
}
}
$("#viewerPlaceHolder").FlexPaperViewer(args) ;
</script>
</html>
- SwfFile参数指定的是一个文件的下载地址
- 文件的上传和下载需要另外代码实现,这里不做描述
- 文件需要先转成pdf,再有pdf转成swf,都是在操作系统的文件系统中进行的