前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java工具集-Html2PDF

Java工具集-Html2PDF

作者头像
cwl_java
发布2020-06-10 15:50:43
1.5K2
发布2020-06-10 15:50:43
举报
文章被收录于专栏:cwl_Java
代码示例
代码语言:javascript
复制
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 "";
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/06/09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 代码示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档