openOffice+pdf2htmlEX实现上传文件转pdf转html,目录和文档的预览

           上一篇文章里面使用了openOffice+swfTools+FlexPaper将上传文档转pdf转swf实现文档的预览,因为swf中无法提取上传word文档的目录,这次试用了openOffice+pdf2html的方式,将word转为pdf再转html,从html中来提取目录的信息实现目录和文档的预览。

            jsp中上传文件,采用form表单提交,在web.xml中配置,后台采用servlet来处理请求。在将上传的word文件的路径传递给office2pdf方法后,在该方法中使用openOffice插件将word文件转化为pdf文件。注意:在执行项目前将openOfficce服务启动,启动方法:用cmd命令进入到openOffice安装路径下,执行命令:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard,执行后的效果如图:

在office2pdf方法执行完毕,返回一个生成的pdf的路径,作为pdf转html的入参。具体转化方法参考:

public String   pdf2html(String pdfFilePath) throws InterruptedException{
		if(!pdfFilePath.equals("")||pdfFilePath!=null){
			String  filePath = pdfFilePath.substring(0,pdfFilePath.lastIndexOf("\\"));
		     File htmlFile  = new File(filePath+".html");//html文件路径(包括有html文件名)
		     htmlFilePath = filePath;//html文件路径
		     System.out.println("html文件的路径是 "+htmlFilePath);
		     String pdfName = pdfFilePath.substring(pdfFilePath.lastIndexOf("\\")+1);
		     String htmlFileName = pdfName.substring(0,pdfName.lastIndexOf("."))+".html";//html文件的name
		   //pdf2html的插件的路径
			String pdf2htmlexePath = "D:/JavaAbout/MyEclipse/cipanWorkspace/pdf2htmlEX-0.12-win32-static/pdf2htmlEX.exe";
			if (!(pdf2htmlexePath != null && !"".equals(pdf2htmlexePath) && pdfFilePath != null
	                && !"".equals(pdfFilePath) && htmlFileName != null && !""
	                    .equals(htmlFileName))) {
	            System.out.println("传递的参数有误!");
	            return null;
	        }
			Runtime rt = Runtime.getRuntime();
			StringBuilder command = new StringBuilder();
	        command.append(pdf2htmlexePath).append(" ");
	        if (htmlFilePath != null && !"".equals(htmlFilePath.trim()))// 生成文件存放位置,需要替换文件路径中的空格
	            command.append("--dest-dir ").append(htmlFilePath.replace(" ", "\" \"")).append(" ");
	        	command.append("--optimize-text 1 ");// 尽量减少用于文本的HTML元素的数目 (default: 0)
	        	command.append("--zoom 1.4 ");
	        	command.append("--process-outline 0 ");// html中显示链接:0——false,1——true
	        	command.append("--font-format woff ");// 嵌入html中的字体后缀(default ttf)
	                                                // ttf,otf,woff,svg
	        	command.append(pdfFilePath.replace(" ", "\" \"")).append(" ");// 需要替换文件路径中的空格
	        if (htmlFileName != null && !"".equals(htmlFileName.trim())) {
	                command.append(htmlFileName);
	                if (htmlFileName.indexOf(".html") == -1)
	                    command.append(".html");
	            }
	        try {
	            Process p = rt.exec(command.toString());
	            StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
	            // 开启屏幕标准错误流
	            errorGobbler.start();
	            StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT");
	            // 开启屏幕标准输出流
	            long o1 = System.currentTimeMillis();
	            outGobbler.start();
	            int w = p.waitFor();
	            int v = p.exitValue();
	            if (w == 0 && v == 0) {
	            	System.out.println("转化html文件路径是 "+htmlFilePath);
	                return htmlFilePath+"\\"+htmlFileName;
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	}else{
		System.out.println("要转化的pdf文件不存在");
		}
		return null;
	}

servlet执行完毕后跳转到预览点击的页面。将上传文件的路径和html文件的路径作为参数在跳转中传递:

request.getRequestDispatcher("/ClickYulan.jsp?converfilename="+converfilename+"&htmlFilePath="+htmlFilePath2+"").forward(request, response);

注意:此时的路径作为参数进行传递时,路径必须使用“/”,使用"\"时会将其作为标记对待,传递过去后,路径的"\"就会消失,也可以将路径替换下,"\"替换成“\\”。

       在生成目录时,根据传递的html的路径,针对word版本是07还是03版进行目录的提取,提取后将目录封装到list返回到前台,前台遍历

在文件展示时,将html文件的路径传递,在初始化时加载,我这里在tomcat的server.xml中配置了相对路径,因此采用如下方法

最终就可以实现目录和文档的预览了

猜你喜欢

转载自blog.csdn.net/liujuncheng000/article/details/81112701