openOffice+jodConverter+jquery.media.js在线预览office、pdf、txt等文档
来源:互联网 发布:森林资源二类调查数据 编辑:程序博客网 时间:2024/06/05 11:58
一 、确定实现需要用到的技术(openOffice+jodConverter+jquery.media.js):
用jodConverter内部提供的方法调用openOffice的接口将office文档及txt类型的文件转成pdf格式的文件,然后利用jquery.media.js将pdf文件显示到页面。
二、资料准备:
1、下载openOffice安装包,下载地址:http://www.openoffice.org/download/index.html,并安装。
2、下载jodConverter,将解压后文件夹内部的lib文件夹下的jar导入项目中,笔者这里用的是jodConverter-2.2.2版本,lib文件如图:
3、下载jquery.media.js,下载地址:http://plugins.jquery.com/media/.
二、实现方法:
1、安装openOffice软件它可以将office文档转成pdf文件。转换需要启动openOffice server。可以在命令行下进入openOffice的安装目录下的program文件内输入以下命令启动openOffice服务:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;"。执行以上命令之后可进入windows任务管理器查看有个进程soffice.bin,说明openoffice启动成功。
也可在java代码中通过代码启动服务。该文档就是以java代码的方式启动openOffice服务的。具体请我往下看。
2、导入之前下载的jar包到项目下。
3、 以上office转pdf的环境即一配置好了下面请看java代码:
public class Office2Pdf {// OpenOffice的安装目录,默认会安装到c盘下private static String OpenOffice_HOME = "C:/Program Files (x86)/OpenOffice 4/program/";// 启动服务的命令private static String command = "soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";/** * 核心转pdf方法 * * @param sourcefile 被转文件 * @param targetfile 目标文件即pdf文件 * @return */ public static void convertTo(File sourcefile, File targetfile) { //目标文件 Process process = null; try { // 启动方法 process = startOpenOffice(); //连接openoffice的端口必须和启动服务的端口一致, OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); //转换,传入源文件和目标文件; converter.convert(sourcefile, targetfile); connection.disconnect(); } catch (ConnectException e) { e.printStackTrace(); }finally{ //强制终止进程 process.destroy(); } } /** * 启动openOffice服务 */ public static Process startOpenOffice(){ // 启动OpenOffice的服务的完整命令 String fullCommand = OpenOffice_HOME + command; try { return Runtime.getRuntime().exec(fullCommand);} catch (IOException e) {e.printStackTrace();} return null; }}
Office2Pdf类中有两个静态的方法和两个静态变量:第一个方法用于转换,第二个用于在程序内部通过代码启动openOffice服务,两个变量分别是openOffice的安装目录和启动命令。第一个方法需要传入两个参数:sourcefile是一个File类型的需要转成pdf格式的源文件,targetfile是转成pdf格式后的文件,转换之前必须先启动openOffice server。此时笔者将启动方法封装进了startOpenOffice()也即第二个静态方法,这样targetfile就是被转换后的pdf文件了,下面将targetPdf文件写到页面就ok啦上代码:
public static void fileToPdf(File sourcefile,String fileName,HttpServletResponse response){// 调用Office2Pdf的静态方法将传入的文件转成pdf类型的文件File pdffile = new File(fileName+".pdf");Office2Pdf.convertTo(sourcefile, pdffile);try {ServletOutputStream outputStream = response.getOutputStream();InputStream inputStream = new FileInputStream(pdffile);byte[] b = new byte[2048];int len;while((len=inputStream.read(b))!=-1){outputStream.write(b, 0, len);}} catch (IOException e) {e.printStackTrace();}}
html代码:
<a id="ddd" class="media" href="#" hidden="true" target="_blank">ddd</a>js代码: function showPdf(){<pre name="code" class="html"> var url='${pageContext.request.contextPath}/你的action!getPreview.action';$("#ddd").attr('href',url);$('#admin_jsxm_lookfj').media({width:400,height:600,});document.getElementById("admin_jsxm_lookfj").click();//这一句就是不用再去点击超链接了,直接触发showPdf方法就可在页面显示了}
效果如下:
- openOffice+jodConverter+jquery.media.js在线预览office、pdf、txt等文档
- java实现在线预览doc,txt,ppt,xls等文档,用到了libreOffice,openOffice,SWFTools,JODConverter,flexPaper
- Android实现在线预览office文档(Word,Pdf,excel,PPT.txt等格式)
- openoffice4.1.3+jquery.media.js 实现txt,doc,excel,ppt,pdf在线预览
- jquery.media.js 插件实现在线预览PDF文件
- Jquery.media.js实现网页在线预览pdf文件
- 在线预览Office(openoffice )
- openoffice+pdf2swf实现在线转换office文档,实现在线预览
- centos6.5下安装openoffice+jodconverter+swftool+flexpaper工具实现在线预览文档功能
- JQuery在线生成word、pdf、excel、txt等文档
- web使用openoffice实现在线预览office文档
- web使用openoffice实现在线预览office文档
- office(doc,xls,txt,pdf,ppt)文档在线预览及转换(office2pdf) - PHP版
- apache poi操作office文档----java在线预览txt、word、ppt、execel,pdf代码
- 利用 jodconverter 和openoffice 结合 把doc、ppt 等一系列文档转换成pdf
- (三)java实现文档的在线浏览-使用JODConverter将office文档转换为pdf
- 文档在线预览(二)-使用JODConverter转换文档为PDF
- OpenOffice+JodConverter实现Office文件到PDF的转换
- Open Credit System
- “FLASH”惹得祸之‘部件“Flash32-11-7-700-202.ocx”或其附件之一不能正确注册:一个文件丢失或无效’
- Hbase原理、基本概念、基本架构
- CRtmpServer转推流到Nginx Rtmp及SRS(SimpleRtmpServer)的经历
- C语言静态函数
- openOffice+jodConverter+jquery.media.js在线预览office、pdf、txt等文档
- 好记性不如烂笔头44-javaWeb中用Session统计在线人数(8)
- 写在2015的开始
- crtmpserver流媒体服务器的介绍与搭建
- Java 注解 Annotation
- connection-error-in-apache-pig
- stm32启动文件中heap与stack的区别
- i2c 开发笔记
- ios中的coredata的使用