JAVA使用wkhtml 将html转成pdf或Image文件

linux的wkhtml 安装:

linux安装wkhtmltopdf(清晰明了)_sout-lanys的博客-CSDN博客

win的wkhtml安装:

直接下载:wkhtmltopdf

html 必须加UTF-8编码

<head>

<meta charset="utf-8">

</head>

java代码:

复制代码
import lombok.extern.log4j.Log4j2;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * <p>
 *   html转换工具
 * </p>
 *
 * @author Garcia
 * @since 2023-09-12
 */

@Log4j2
public class HtmlUtils {

    private final static String WIN_PDF_TOOL = "D:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";

    private final static String WIN_IMAGE_TOOL = "D:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage.exe";

    private final static String LINUX_PDF_TOOL = "/usr/local/bin/wkhtmltopdf";

    private final static String LINUX_IMAGE_TOOL = "/usr/local/bin/wkhtmltoimage";


    /**
     * html转PDF
     *
     * @param htmlPath html路径
     * @param pdfPath  pdf路径
     */
    public static void htmlToPdf(String htmlPath, String pdfPath) {
        File file = new File(pdfPath);
        File parent = file.getParentFile();
        //如果pdf保存路径不存在,则创建路径
        if (!parent.exists()) {
            parent.mkdirs();
        }
        String tool;
        StringBuilder cmd = new StringBuilder();
        if (System.getProperty("os.name").contains("Windows")) {
            //非windows 系统
            tool = WIN_PDF_TOOL;
        }else {
            tool = LINUX_PDF_TOOL;
        }
        cmd.append(tool);
        cmd.append(" ");
        cmd.append("-B 0 -L 0 -R 0 -T 0 ");
        //开启本地文件访问
        cmd.append("--enable-local-file-access ");
        //cmd.append(" --header-line");//页眉下面的线
        //cmd.append(" --header-center 这里是页眉这里是页眉这里是页眉这里是页眉 ");//页眉中间内容
        //cmd.append(" --margin-top 3cm ");//设置页面上边距 (default 10mm)
        //cmd.append(" --header-html file:///" + WebUtil.getServletContext().getRealPath("") + FileUtil.convertSystemFilePath("\\style\\pdf\\head.html"));// (添加一个HTML页眉,后面是网址)
        //cmd.append(" --header-spacing 5 ");// (设置页眉和内容的距离,默认0)
        //cmd.append(" --footer-center (设置在中心位置的页脚内容)");//设置在中心位置的页脚内容
        //cmd.append(" --footer-html file:///" + WebUtil.getServletContext().getRealPath("") + FileUtil.convertSystemFilePath("\\style\\pdf\\foter.html"));// (添加一个HTML页脚,后面是网址)
        //cmd.append(" --footer-line");//* 显示一条线在页脚内容上)
        //cmd.append(" --footer-spacing 5 ");// (设置页脚和内容的距离)
        cmd.append(htmlPath);
        cmd.append(" ");
        cmd.append(pdfPath);

        try {
            Process proc = Runtime.getRuntime().exec(cmd.toString());
            InputStreamReader isr = new InputStreamReader(proc.getInputStream(), StandardCharsets.UTF_8);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println("html转pdf进度和信息:"+ line);
            }
            proc.waitFor();
        } catch (Exception e) {
            log.error("HTML转PDF失败",e);
        }
    }

    public static void htmlToImg(String srcPath, String destPath) {
        try {
            File file = new File(destPath);
            File parent = file.getParentFile();
            //如果pdf保存路径不存在,则创建路径
            if (!parent.exists()) {
                parent.mkdirs();
            }
            String tool;
            StringBuilder cmd = new StringBuilder();
            if (System.getProperty("os.name").contains("Windows")) {
                //非windows 系统
                tool = WIN_IMAGE_TOOL;
            }else {
                tool = LINUX_IMAGE_TOOL;
            }
            cmd.append(tool);
            cmd.append(" ");
            cmd.append(srcPath);
            cmd.append(" ");
            cmd.append(destPath);

            Process proc = Runtime.getRuntime().exec(cmd.toString());
            InputStreamReader isr = new InputStreamReader(proc.getInputStream(), StandardCharsets.UTF_8);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println("html转jpg进度和信息:"+ line);
            }
            proc.waitFor();
        } catch (Exception e) {
            log.error("HTML转IMAGE失败",e);
        }
    }
}
相关推荐
Kyln.Wu11 小时前
【python实用小脚本-190】Python一键删除PDF任意页:输入页码秒出干净文件——再也不用在线裁剪排队
服务器·python·pdf
阿幸软件杂货间1 天前
免费万能电子书格式转换器!Neat Converter支持 ePub、Azw3、Mobi、Doc、PDF、TXT 文件的相互转换。
pdf·格式转换
星马梦缘2 天前
CSDN转PDF【无水印且免费!!!】
pdf·免费·pandoc·转pdf·无水印·csdn转pdf·wkhtmlpdf
画月的亮2 天前
前端处理导出PDF。Vue导出pdf
前端·vue.js·pdf
伊织code2 天前
pdfminer.six
python·pdf·图片·提取·文本·pdfminer·pdfminer.six
HAPPY酷3 天前
给纯小白的Python操作 PDF 笔记
开发语言·python·pdf
代码AI弗森4 天前
PDF OCR + 大模型:让文档理解不止停留在识字
pdf·ocr
小周同学:4 天前
在 Vue2 中使用 pdf.js + pdf-lib 实现 PDF 预览、手写签名、文字批注与高保真导出
开发语言·前端·javascript·vue.js·pdf
Kyln.Wu5 天前
【python实用小脚本-187】Python一键批量改PDF文字:拖进来秒出新文件——再也不用Acrobat来回导
python·pdf·c#
迪尔~6 天前
Apache POI中通过WorkBook写入图片后出现导出PDF文件时在不同页重复写入该图片问题,如何在通过sheet获取绘图对象清除该图片
java·pdf·excel