第一次些博客啊。看了很多网上文件在线预览的的文章,感觉太过繁杂。最常用的是(openoffice+swftools+flexpaper),需要可以尝试:
http://blog.csdn.net/z69183787/article/details/17468039
自己试了好多次,找了很多demo,表示已经对flexpaper绝望了,flexpaper需要flash,flash又可能会被拦截。搞了很多次.swf文件死活加载不出来。从jetty服务器换到tomcat,中间突然可以,然后又不行了。留下一脸懵逼的我。
后来想了想PDF都是可以在浏览器打开的啊,应该可以有js实现pdf在线预览的,后来找到了:
http://www.open-open.com/news/view/1fc3e18/
其实还是挺多的,我就拿了个兼容性比较好的jquery.media.js。于是就是 openoffice4.1.3 + jquery.media.js
1、开发前准备:安装openoffice
打开dos窗口,进入openoffice安装盘符,输入以下代码来启动服务:
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
这句命令是在host中启动openoffice服务,端口是8100.这句话是可以写在程序里的,不用在cmd写。2、jar包:
比较不常见的那几个jar在安装目录找找看,比如我的安装目录C:\Program Files (x86)\OpenOffice 4\program\classes
其他是ssm、ssh、jfinal框架也好,只要能实现servlet基本就可以了。
3、java工具类,office-->pdf
package com.xh.utils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.sun.star.io.ConnectException;
public class Office2PDF {
public static int office2PDF(String sourceFile, String destFile) {
Process pro = null;
OpenOfficeConnection connection = null;
try {
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
return -1;//文件不存在
}
//文件夹不存在创建目录
File outputFile = new File(destFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
String OpenOffice_HOME = "C:\\Program Files (x86)\\OpenOffice 4";
if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {
OpenOffice_HOME += "\\";
}
// 启动OpenOffice的服务
String command = OpenOffice_HOME
+ "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";
pro = Runtime.getRuntime().exec(command);
// connect to an OpenOffice.org instance running on port 8100
connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
connection.connect();
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);
converter.convert(inputFile, outputFile);
return 0;
} catch (Exception e) {
e.printStackTrace();
}finally{
if(connection!=null){
// close the connection
connection.disconnect();
}
// 关闭OpenOffice服务的进程
if(pro!=null){
pro.destroy();
}
}
return 1;
}
}
4、controller层:
public void index(){
String sourceFile=getRequest().getServletContext().getRealPath("/")+"/upload/面试题分析.docx";
String ss="cache/面试题分析.pdf";
String destFile=getRequest().getServletContext().getRealPath("/")+"/"+ss;
System.out.println(Office2PDF.office2PDF(sourceFile, destFile));
setAttr("ss",ss);
render("/Test/1.jsp");
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Online View PDF</title>
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/jquery.media.js"></script>
<script type="text/javascript">
$(function() {
$('iframe.media').media({width:800, height:600});
});
</script>
</head>
<body>
<iframe class="media" src="${ss}" height="600" width="800"></iframe>
</body>
</html>
s