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方法就可在页面显示了
}


效果如下:


0 0