- 下载Pdf2htmlEX包
Pdf2htmlEXUtil
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 | package application; import application.StreamGobbler; /** * @author liuzhengyong * @version 1.0 时间:2013-12-30 下午2:24:10 pdf文件转html工具类 */ public class Pdf2htmlEXUtil { /** * 调用pdf2htmlEX将pdf文件转换为html文件 * * @param exeFilePath * pdf2htmlEX.exe文件路径 * @param pdfFile * pdf文件绝对路径 * @param [destDir] 生成的html文件存放路径 * @param htmlName * 生成的html文件名称 * @return */ public static boolean pdf2html(String exeFilePath, String pdfFile, String destDir, String htmlFileName) { if (!(exeFilePath != null && !"".equals(exeFilePath) && pdfFile != null && !"".equals(pdfFile) && htmlFileName != null && !"" .equals(htmlFileName))) { System.out.println("传递的参数有误!"); return false; } Runtime rt = Runtime.getRuntime(); StringBuilder command = new StringBuilder(); command.append(exeFilePath).append(" "); if (destDir != null && !"".equals(destDir.trim()))// 生成文件存放位置,需要替换文件路径中的空格 command.append("--dest-dir ").append(destDir.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(pdfFile.replace(" ", "" "")).append(" ");// 需要替换文件路径中的空格 if (htmlFileName != null && !"".equals(htmlFileName.trim())) { command.append(htmlFileName); if (htmlFileName.indexOf(".html") == -1) command.append(".html"); } try { System.out.println("Command:" + command.toString()); Process p = rt.exec(command.toString()); StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR"); // 开启屏幕标准错误流 errorGobbler.start(); StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT"); // 开启屏幕标准输出流 outGobbler.start(); int w = p.waitFor(); int v = p.exitValue(); if (w == 0 && v == 0) { return true; } } catch (Exception e) { e.printStackTrace(); } return false; } public static boolean pdf2html_linux(String pdfFile, String destDir, String htmlFileName) { if (!(pdfFile != null && !"".equals(pdfFile) && htmlFileName != null && !"" .equals(htmlFileName))) { System.out.println("传递的参数有误!"); return false; } Runtime rt = Runtime.getRuntime(); StringBuilder command = new StringBuilder(); command.append("pdf2htmlEX").append(" "); if (destDir != null && !"".equals(destDir.trim()))// 生成文件存放位置,需要替换文件路径中的空格 command.append("--dest-dir ").append(destDir.replace(" ", "" "")) .append(" "); command.append("--optimize-text 1 ");// 尽量减少用于文本的HTML元素的数目 (default: 0) command.append("--process-outline 0 ");// html中显示链接:0——false,1——true command.append("--font-format woff ");// 嵌入html中的字体后缀(default ttf) // ttf,otf,woff,svg command.append(pdfFile.replace(" ", "" "")).append(" ");// 需要替换文件路径中的空格 if (htmlFileName != null && !"".equals(htmlFileName.trim())) { command.append(htmlFileName); if (htmlFileName.indexOf(".html") == -1) command.append(".html"); } try { System.out.println("Command:" + command.toString()); Process p = rt.exec(command.toString()); StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR"); // 开启屏幕标准错误流 errorGobbler.start(); StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT"); // 开启屏幕标准输出流 outGobbler.start(); int w = p.waitFor(); int v = p.exitValue(); if (w == 0 && v == 0) { return true; } } catch (Exception e) { e.printStackTrace(); } return false; } // public static void main(String[] args) { // pdf2html("D:\\毕业设计\\pdfBox\\pdf2htmlEX-win32-0.13.6\\pdf2htmlEX.exe","C:\\Users\\钟倩文\\Desktop\\pdf测试文件\\1.pdf","C:\\Users\\钟倩文\\Desktop\\pdf测试文件","my.html"); // } } |
StreamGobbler
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 | package application; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; /** * 用于处理Runtime.getRuntime().exec产生的错误流及输出流 * * @author shaojing * */ public class StreamGobbler extends Thread { InputStream is; String type; OutputStream os; public StreamGobbler(InputStream is, String type) { this(is, type, null); } StreamGobbler(InputStream is, String type, OutputStream redirect) { this.is = is; this.type = type; this.os = redirect; } public void run() { InputStreamReader isr = null; BufferedReader br = null; PrintWriter pw = null; try { if (os != null) pw = new PrintWriter(os); isr = new InputStreamReader(is); br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { if (pw != null) pw.println(line); System.out.println(type + ">" + line); } if (pw != null) pw.flush(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if (pw != null) pw.close(); if (br != null) br.close(); if (isr != null) isr.close(); } catch (IOException e) { e.printStackTrace(); } } } } |
- 调用方法
1 2 | Pdf2htmlEXUtil pdf2htmlEXUtil = new Pdf2htmlEXUtil(); pdf2htmlEXUtil.pdf2html("D:\\毕业设计\\pdfBox\\pdf2htmlEX-win32-0.13.6\\pdf2htmlEX.exe","C:\\Users\\钟倩文\\Desktop\\pdf测试文件\\1.pdf","C:\\Users\\钟倩文\\Desktop\\pdf测试文件","my.html"); |