在线预览pdf/swf
openoffice 将上传文件转为.pdf------------->pdf2swf软件将pdf转为.swf
转pdf
Java Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// 启动OpenOffice的服务 String command = this.openOffice; command += "soffice.exe -headless -accept=\"socket,host= 127. 0. 0. 1,port= 8100;urp;\ " -nofirststartwizard"; Runtime.getRuntime().exec(command); try { OpenOfficeConnection connection = new SocketOpenOfficeConnection( "127.0.0.1", 8100); connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docFile, pdfFile); connection.disconnect(); System.out.println( "****pdf转换成功,PDF输出:" + pdfFile.getPath() + "****"); } catch (java.net.ConnectException e) { e.printStackTrace(); throw e; } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) { e.printStackTrace(); throw e; } catch (Exception e) { e.printStackTrace(); throw e; } |
转swf
Java Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
try
{ Process p = r.exec( this.swf + " " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9"); System.err.println( "****swf转换成功,文件输出:" + swfFile.getPath() + "****"); try { p.waitFor(); } catch (Exception e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); throw e; } |
前段页面FlexPaperViewer展示:
<script type="text/javascript" src="#{contextPath}/js/flexpaper_flash.js?version=#{springProperty.version}"></script>
HTML Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<div
style=
"position:fixed;left:1px;top:1px;overflow: hidden;width:100%;height:100%"
>
<a id= "viewerPlaceHolder" name= "viewerPlaceHolder" ></a> <script type= "text/javascript" > //<![CDATA[ var fp = new FlexPaperViewer( 'FlexPaperViewer', 'viewerPlaceHolder', { config : { SwfFile : escape( '#{oneReportManagerAction.showLocation}'), Scale : 0. 6, ZoomTransition : 'easeOut', ZoomTime : 0. 5, ZoomInterval : 0. 2, FitPageOnLoad : true, FitWidthOnLoad : false, FullScreenAsMaxWindow : false, ProgressiveLoading : false, MinZoomSize : 0. 2, MaxZoomSize : 5, SearchMatchAll : false, InitViewMode : 'Portrait', PrintPaperAsBitmap : false, ViewModeToolsVisible : true, ZoomToolsVisible : true, NavToolsVisible : true, CursorToolsVisible : true, SearchToolsVisible : true, localeChain: 'zh_CN' }}); //]] > </script> </div> |
采用pdfObject 显示PDF
Java Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
/** * @param sourceFile * @param destFile * @return * * 1:最终 * 0:转换成功 * -1:文件不存在 * -2:文件不能生成pdf,提供预览 * -3:文件路径错误 */ public String convertPDF( String sourceFile, String destFile) { //check file if(StringUtils.isNotBlank(sourceFile) && StringUtils.isNotBlank(destFile)) { try { String sr_suffix = sourceFile.substring(sourceFile.lastIndexOf( ".") + 1); String de_suffix = destFile.substring(destFile.lastIndexOf( ".") + 1); if(!de_suffix.equals(PDF_POSTFIX)) { return "目的文件路径不对"; } if(!Arrays.asList(OFFICE_POSTFIXS).contains(sr_suffix)) { return "源文件路径不对,不是合法文件格式 (doc, docx, xls, xlsx, ppt, pptx) 请尝试下载阅览"; } } catch (Exception e) { return "error"; } } Process pro = null; OpenOfficeConnection connection = null; File inputFile = null; File outputFile = null; try { inputFile = new File(sourceFile); if (!inputFile.exists()) { return "没找到文件"; // 文件不存在 } // 文件夹不存在创建目录 outputFile = new File(destFile); if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } // 启动OpenOffice的服务 String command = fa_InvestListService.getOpenOfficPath(); if( ! new File(command).exists()) { return "没有安装OpenOffice software"; } command += "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); try { connection.connect(); // convert DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(inputFile, outputFile); return "success"; } catch (Exception e) { return "链接OpenOffix 服务失败;" + e.getMessage(); } } catch (FileNotFoundException e) { return "没找到文件"; } catch(Exception e) { return "转换pdf错误"; } finally { if (connection != null) { // close the connection try { connection.disconnect(); } catch (Exception e) { // if start windows service failed at first time , this code will try to start agin String command = fa_InvestListService.getOpenOfficPath(); if( ! new File(command).exists()) { return "没有安装OpenOffice software"; } command += "soffice.exe -headless -accept=\"socket,host= 127. 0. 0. 1,port= 8100;urp;\ " -nofirststartwizard"; try { pro = Runtime.getRuntime().exec(command); connection = new SocketOpenOfficeConnection( "127.0.0.1", 8100); connection.connect(); } catch (Exception e1) { } DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(inputFile, outputFile); connection.disconnect(); if (pro != null) { pro.destroy(); log.info( "{exception---pro.destry}"); } return "success"; } } // 关闭OpenOffice服务的进程 if (pro != null) { pro.destroy(); log.info( "{normal---pro.destry}"); } } } @RequestMapping( "/viewUploadFile") public void viewUploadFile(@RequestParam(value = "modelId", required = true) Long modelId, HttpServletRequest request, HttpServletResponse response) throws IOException { String result = null; boolean isWindows = false; String os = System.getProperty( "os.name"); if(StringUtils.isNotBlank(os) && os.toLowerCase().contains( "windows")) { isWindows = true; } else { isWindows = false; result = "不是windows系统,无法提供OpenOffice 转换功能"; } if (modelId != null && isWindows) { FA_InvestList fl = fa_InvestListService.findById(modelId); String filePath = fl.getFilePath(); log.info(filePath + "文件正在预览"); String pdf = request.getSession().getServletContext().getRealPath( "/pdf"); String opdf = pdf + "\\temp.pdf"; result = convertPDF(filePath, opdf); if(result.equals( "success")) { try { response.setContentType( "text/html; charset=utf-8"); response.setCharacterEncoding( "utf-8"); String projectName = request.getSession().getServletContext().getContextPath(); String url = request.getRequestURL().toString(); url = url.substring( 0, url.lastIndexOf( "/")); StringBuffer html = new StringBuffer(); html.append( "<html>"); html.append( "<head>"); html.append( "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>"); html.append( "<title>在线预览</title> "); html.append( " <script type='text/javascript' src='" + projectName + "/js/pdfobject.js'></script>"); html.append( "<script type='text/javascript'>"); html.append( "window.onload = function (){"); html.append( " var success = new PDFObject({ url: '" + url + "/pdf/temp.pdf'}).embed();"); html.append( " };"); html.append( "</script>"); html.append( "</head>"); html.append( "<body>"); html.append( "</body>"); html.append( "</html>"); response.getWriter().write(html.toString()); } catch (Exception e) { response.setContentType( "text/html; charset=utf-8"); response.setCharacterEncoding( "utf-8"); String content = new String( "<html><span style='font-style:25px;font-weight:bold;'>so sorry! Document Don't find(文件没有找到...)</span></html>"); response.getWriter().write(content); } } else { response.setContentType( "text/html; charset=utf-8"); response.setCharacterEncoding( "utf-8"); String content = new String( "<html><span style='font-style:25px;font-weight:bold;'>" + result + "</span></html>"); response.getWriter().write(content); } } else { response.setContentType( "text/html; charset=utf-8"); response.setCharacterEncoding( "utf-8"); String content = new String( "<html><span style='font-style:25px;font-weight:bold;'>" + result + "</span></html>"); response.getWriter().write(content); } } |