wkhtmltox,超简单的html转pdf(java调用)

本文介绍了如何通过Java调用wkhtmltopdf工具,将HTML内容便捷地转换为PDF文件。首先,你需要从官方网站下载并配置这个开源工具。在Windows系统中配置好环境后,可以通过简单的Java代码示例实现转换功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先去官网下载wkhtmltopdf  下载网址:https://wkhtmltopdf.org/downloads.html 

然后解压和配置环境,

window系统下示例:

环境配置完后去试试:

java调用:

示例代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.UUID;
 
/**
 * wkhtmltopdf工具类
 *
 * 约定:
 *      1. 插件安装位置,在Windows系统中将插件安装在D盘根目录下(D:/), 在Linux系统中将插件安装在opt目录下(/opt)
 *
 * 注意:
 *      1. wkhtmltopdf的Linux版本中,解压后,默认的文件名为"wkhtmltox",为了统一起见,一律将解压后的文件名,重命名为"wkhtmltopdf"(命令:mv wkhtmltox wkhtmltopdf)
 *
 * Created by kagome on 2016/7/26.
 */
public class html2pdf {
    // 临时目录的路径
    public static final String TEMP_DIR_PATH = html2pdf.class.getResource("/").getPath().substring(1) + "temp/";
 
    static {
        // 生成临时目录
        new File(TEMP_DIR_PATH).mkdirs();
    }
 
    public static void main(String[] args) throws Exception {
        htmlToPdf("http://47.100.100.55:8080/resume_jianli/1.html",  "d:\\1.pdf");
    }
 
    /**
     * 将HTML文件内容输出为PDF文件
     *
     * @param htmlFilePath HTML文件路径
     * @param pdfFilePath  PDF文件路径
     */
    public static void htmlToPdf(String htmlFilePath, String pdfFilePath) {
        try {
            Process process = Runtime.getRuntime().exec(getCommand(htmlFilePath, pdfFilePath));
            new Thread(new ClearBufferThread(process.getInputStream())).start();
            new Thread(new ClearBufferThread(process.getErrorStream())).start();
            process.waitFor();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
 
    /**
     * 将HTML字符串转换为HTML文件
     *
     * @param htmlStr HTML字符串
     * @return HTML文件的绝对路径
     */
    public static String strToHtmlFile(String htmlStr) {
        OutputStream outputStream = null;
        try {
            String htmlFilePath = TEMP_DIR_PATH + UUID.randomUUID().toString() + ".html";
            outputStream = new FileOutputStream(htmlFilePath);
            outputStream.write(htmlStr.getBytes("UTF-8"));
            return htmlFilePath;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                    outputStream = null;
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
 
    /**
     * 获得HTML转PDF的命令语句
     *
     * @param htmlFilePath HTML文件路径
     * @param pdfFilePath  PDF文件路径
     * @return HTML转PDF的命令语句
     */
    private static String getCommand(String htmlFilePath, String pdfFilePath) {
        String osName = System.getProperty("os.name");
        // Windows
        if (osName.startsWith("Windows")) {
            return String.format("D:/wkhtmltox/bin/wkhtmltopdf.exe %s %s", htmlFilePath, pdfFilePath);
        }
        // Linux
        else {
            return String.format("/opt/wkhtmltopdf/bin/wkhtmltopdf %s %s", htmlFilePath, pdfFilePath);
        }
    }
 
}

 

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
 
/**
 * 清理输入流缓存的线程
 * Created by kagome on 2016/8/9.
 */
public class ClearBufferThread implements Runnable {
    private InputStream inputStream;
 
    public ClearBufferThread(InputStream inputStream){
        this.inputStream = inputStream;
    }
 
    public void run() {
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            while(br.readLine() != null);
        } catch(Exception e){
            throw new RuntimeException(e);
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值