Java工具集-Html2PDF

代碼示例


import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * @program: simple_tools
 * @description: Html2PDF
 * @author: Mr.chen
 * @create: 2020-06-09 09:39
 **/
public class CustomWKHtmlToPdfUtil {
    /**
     * 把html字節數組轉換成pdf的字節數組,非線性安全
     *
     * @param src
     * @return
     * @throws IOException
     */
    public static byte[] html2Pdf(byte[] src, String wkhtmlToPdfHome) throws IOException {
        File srcFile = new File(getTmpFilePath(".html"));
        FileUtils.writeByteArrayToFile(srcFile, src);

        String targetFilePath = getTmpFilePath(".pdf");
        File descFile = new File(targetFilePath);
        html2Pdf(srcFile.getAbsolutePath(), descFile.getAbsolutePath(), wkhtmlToPdfHome);
        byte[] result = FileUtils.readFileToByteArray(new File(targetFilePath));
        srcFile.delete();
        descFile.delete();

        return result;
    }

    private static String getTmpFilePath(String suffix) {
        String system = System.getProperty("os.name");
        if (system.contains("Windows")) {
            return "./tmp" + System.currentTimeMillis() + suffix;
        } else if (system.contains("Linux")) {
            return "/tmp/tmp" + System.currentTimeMillis() + suffix;
        } else if (system.contains("Mac OS")) {
            return "/tmp/tmp" + System.currentTimeMillis() + suffix;
        }

        return null;
    }

    public static String html2Pdf(String sourceFilePath, String targetFilePath, String wkhtmlToPdfHome) {
        Process process = null;
        String cmd = getCommand(sourceFilePath, targetFilePath, wkhtmlToPdfHome);
        try {
            process = Runtime.getRuntime().exec(cmd);
            // 這個調用比較關鍵,就是等當前命令執行完成後再往下執行
            process.waitFor();
//			File file = new File(sourceFilePath);
//			if (file.exists()) {
//				file.delete();
//			}
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (process != null) {
                process.destroy();
            }
        }

        return targetFilePath;
    }

    public static String getCommand(String sourceFilePath, String targetFilePath, String wkhtmlToPdfHome) {
        String system = System.getProperty("os.name");
        if (system.contains("Windows")) {
            return wkhtmlToPdfHome + "/wkhtmltopdf.exe " + sourceFilePath + " " + targetFilePath;
        } else if (system.contains("Linux")) {
            return wkhtmlToPdfHome + "/wkhtmltopdf " + sourceFilePath + " " + targetFilePath;
        } else if (system.contains("Mac OS")) {
            return wkhtmlToPdfHome + "/wkhtmltopdf " + sourceFilePath + " " + targetFilePath;
        }
        return "";
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章