java實現PDF轉HTML

問題場景:html

在使用PB嵌入HTML頁面時發現調不起查看PDF的插件java

 

解決方法:linux

將PDF轉換爲HTML來展現app

 

解決步驟:svg

1.下載PDF轉換工具.exe工具

下載地址:http://pan.baidu.com/s/1eSHq3JG字體

 

2.建立工具類ui

package org.common.util.pdftohtml;

import org.common.util.pdftohtml.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:\\pdf2htmlEX-v1.0\\pdf2htmlEX.exe","D:\\pdf2htmlEX-v1.0\\PDF\\my.pdf","D:\\pdf2htmlEX-v1.0\\HTML","my.html");
    }
}
package org.common.util.pdftohtml;

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();
            }
        }
    }
}

 

3.運行工具類中的main方法。this

 

注:運行上述步驟,問題解決,成功生成html文件spa

(注意pdf2htmlEX.exe文件不要單獨copy出來用,須要和pdf2htmlEX-v1.0文件夾裏面的東西放在一塊兒使用,否則會報錯:Error: Cannot open the manifest file)

相關文章
相關標籤/搜索