pdf2htmlEX实现pdf转html

安装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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值