安装pdf2htmlEX
代码:
public static 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文件名)
String 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:/tools/pdf2htmlEX/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;
}
static class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
参考:https://blog.csdn.net/liujuncheng000/article/details/81112701