第一步 导入 OpenOffice.org3.3需要的 pom.xml
com.artofsolving jodconverter 2.2.1
第二步 :文件转换pdf工具类
package com.yeptc.util; import java.io.File; 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; public class DocConverter { private static final int environment = 1;// 环境1:windows,2:linux(涉及pdf2swf路径问题) private String fileString; private String outputPath = "";// 输入路径,如果不设置就输出在默认位置 private String fileName; private File pdfFile; private File swfFile; private File docFile; public DocConverter(String fileString) { ini(fileString); } /* * 重新设置 file @param fileString */ public void setFile(String fileString) { ini(fileString); } /* * 初始化 @param fileString */ private void ini(String fileString) { this.fileString = fileString; fileName = fileString.substring(0, fileString.lastIndexOf(".")); docFile = new File(fileString); pdfFile = new File(fileName + ".pdf"); } /* * 转为PDF @param file */ private void doc2pdf() throws Exception { if (docFile.exists()) { if (!pdfFile.exists()) { String OpenOffice_HOME = "C:\\Program Files (x86)\\OpenOffice.org 3";//这里是OpenOffice的安装目录, 在我的项目中,为了便于拓展接口,没有直接写成这个样子,但是这样是绝对没问题的 // 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\' if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') { OpenOffice_HOME += "\\"; } // 启动OpenOffice的服务 String command = OpenOffice_HOME + "program\\soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard"; Process pro = null; try { // 启动OpenOffice的服务 q pro = Runtime.getRuntime().exec(command); OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1",8100); connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docFile, pdfFile); // close the connection connection.disconnect(); // 封闭OpenOffice服务的进程 pro.destroy(); System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath() + "****"); } catch (java.net.ConnectException e) { // ToDo Auto-generated catch block e.printStackTrace(); System.out.println("****swf转换异常,openoffice服务未启动!****"); throw e; } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) { e.printStackTrace(); System.out.println("****swf转换器异常,读取转换文件失败****"); throw e; } catch (Exception e) { e.printStackTrace(); throw e; } } else { System.out.println("****已经转换为pdf,不需要再进行转化****"); } } else { System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****"); } } /* * 转换主方法 */ public boolean conver() { if (swfFile.exists()) { System.out.println("****swf转换器开始工作,该文件已经转换为swf****"); return true; } if (environment == 1) { System.out.println("****swf转换器开始工作,当前设置运行环境windows****"); } else { System.out.println("****swf转换器开始工作,当前设置运行环境linux****"); } try { doc2pdf(); } catch (Exception e) { // TODO: Auto-generated catch block e.printStackTrace(); return false; } if (swfFile.exists()) { return true; } else { return false; } } }
String OpenOffice_HOME = "C:\\Program Files (x86)\\OpenOffice.org 3"; //这里是OpenOffice的安装目录
第三步:调用 DocConverter
/** * pdf */ @RequestMapping(value = "pdf/",method = RequestMethod.POST) @ResponseBody public int pdf(@RequestBody String jsonStr){ //利用java中URLDecoder对json进行解码 try { jsonStr=java.net.URLDecoder.decode(jsonStr,"UTF-8"); String sArray=jsonStr.replaceAll("&", ","); String[] str=sArray.split(","); for(int i = 0 ; ilength;i++) { DocConverter d = new DocConverter(str[i].split("=")[1]); d.conver(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return 0; }
注意:
DocConverter d = new DocConverter(str[i].split("=")[1]); str[i].split("=")[1]等于//传入需要转换pdf 的路径
d.conver();
第四步:页面 hmtl
01.设置模态框
class="modal fade" id="myModalPreview" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
class="modal-dialog" role="document" style="width: 1090px;">
class="modal-content">
class="modal-header">
class="modal-title" id="myModalLabel">文件预览
class="" style="height:580px;overflow:auto; ">
class="row">
class="embed-responsive embed-responsive-4by3 col-md-9">
class="col-xs-3 col-md-3" id="filePreview">
02. 预览按钮
03.点击预览 执行js
//查询文件预览 function filePreview(){ $('#filePreview').find("div").remove() if (fileNamePath.length>0){ var html =''
fileNamePath$. each(fileNamePath , function(key ,value) { html+= '
fileNamePath 文件地址 , 是页面加载储存的 全局变量
点击文件 实现预览
//预览文件 function filePreviewName (obj) { $('.filePreviewName').removeClass('active') $(obj).addClass('btn btn-default filePreviewName active') $('.embed-responsive-item').attr("src",decodeURI($(obj).attr('href'))) }