使用wkhtmltopdf工具将HTML文件转为PDF文件供使用(非截图生成PDF)

一、去官网下载好相关工具
wkhtmltopdf官网
选择相应机器及版本下载,我这里使用的是Windows版本。
在这里插入图片描述
下载安装好的目录为:
在这里插入图片描述
二、编写调用这个工具的Java代码:

package com.nxs;
import java.io.IOException;

public class HtmlToPdfConverter {


    public static void convertHtmlToPdf(String sourceHtmlFilePath, String targetPdfFilePath) throws IOException, InterruptedException {
        String command = String.format("D:\\Self\\Developer\\Kits\\wkhtmltopdf\\bin\\wkhtmltopdf %s %s", sourceHtmlFilePath, targetPdfFilePath);
        System.out.println("command = " + command);
        Process process = Runtime.getRuntime().exec(command);
        int exitCode = process.waitFor();
        if (exitCode == 0) {
            System.out.println("success" + targetPdfFilePath);
        } else {
            System.err.println("error" + exitCode);
        }
    }

    public static void main(String[] args) {
        try {
            String sourceHtml = "D:\\testHtmlToPdf\\test.html";
            String targetPdf = "D:\\testHtmlToPdf\\test.pdf";
            convertHtmlToPdf(sourceHtml, targetPdf);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

三、相应文件路径
在这里插入图片描述
四、通过调用相应程序,最后将test.html转为test.pdf
html文件
五、打印结果
打印结果

六、查看文件
PDF文件查看

  • 25
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值