前言
当需要使用JAVA转换HTML为PDF或图片时,使用wkhtmltox工具生成的方法如下
相关文章(本文与下面两篇有密切关系):
使用方法
1、创建工具类
创建HtmlControlsUtil
工具类
其中分别有htmlToPdf
、htmlToImg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import java.io.*;
public class HtmlControlsUtil {
protected static final Logger logger = LoggerFactory.getLogger(HtmlControlsUtil.class);
private static final String WKHTMLTOX_PDF_PATH = "C:\wkhtmltox\wkhtmltopdf\bin\wkhtmltopdf.exe"; private static final String WKHTMLTOX_IMG_PATH = "C:\wkhtmltox\wkhtmltopdf\bin\wkhtmltoimage.exe"; private static final String FILE_PDF_PATH = "C:\wkhtmltox\testFile"; private static final String FILE_NAME = UuidUtil.get32UUID();
private static final String HTML_URL = "https://wkhtmltopdf.org/downloads.html";
public static void htmlToPdf(String htmlUrl) { String pdfPath = FILE_PDF_PATH + FILE_NAME + ".pdf"; File file = new File(pdfPath); File parent = file.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } logger.info("----------------------- 开始html转pdf操作"); StringBuilder cmd = new StringBuilder(); cmd.append(WKHTMLTOX_PDF_PATH); cmd.append(" "); cmd.append("-B 0 -L 0 -R 0 -T 0 "); cmd.append("--enable-local-file-access "); cmd.append(" --margin-top 10mm "); cmd.append(" --margin-bottom 10mm "); cmd.append(" --margin-left 8mm "); cmd.append(" --margin-right 8mm "); cmd.append(" --page-offset 1 "); cmd.append(" --page-size A4 "); cmd.append(" --disable-javascript "); cmd.append(" --debug-javascript "); cmd.append(htmlUrl); cmd.append(" "); cmd.append(pdfPath); try { Process proc = Runtime.getRuntime().exec(cmd.toString()); HtmlToXInterceptor error = new HtmlToXInterceptor(proc.getErrorStream()); HtmlToXInterceptor output = new HtmlToXInterceptor(proc.getInputStream()); error.start(); output.start(); proc.waitFor(); logger.info("----------------------- html转pdf成功:html路径:{} pdf保存路径:{}", htmlUrl, pdfPath); } catch (Exception e) { e.printStackTrace(); logger.error("----------------------- html转pdf失败:html路径:{} pdf保存路径:{}", htmlUrl, pdfPath); } finally { logger.info("----------------------- 结束html转pdf操作"); } }
public static void htmlToImg(String htmlUrl) { String imgPath = SAVE_DIR + FILE_NAME + ".jpg"; File file = new File(imgPath); File parent = file.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } StringBuilder cmd = new StringBuilder(); cmd.append(WKHTMLTOX_IMG_PATH); cmd.append(" "); cmd.append(htmlUrl); cmd.append(" "); cmd.append(imgPath); try { Process proc = Runtime.getRuntime().exec(cmd.toString()); HtmlToXInterceptor error = new HtmlToXInterceptor(proc.getErrorStream()); HtmlToXInterceptor output = new HtmlToXInterceptor(proc.getInputStream()); error.start(); output.start(); proc.waitFor(); } catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args) { htmlToPdf(HTML_URL); htmlToImg(HTML_URL); }
}
|
2、工具辅助类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader;
public class HtmlToXInterceptor extends Thread {
protected static final Logger logger = LoggerFactory.getLogger(HtmlToXInterceptor.class);
private InputStream is;
public HtmlToXInterceptor(InputStream is) { this.is = is; }
public void run() { try { InputStreamReader isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { System.out.println(line.toString()); } } catch (IOException e) { e.printStackTrace(); } }
}
|
3、执行代码
执行HtmlControlsUtil
工具类的main方法
生成图片或PDF文件
借鉴链接
wkhtmltox使用(html转pdf或图片)
更多关于wkhtmltox扩展工具的知识持续更新中!!!
声明
借鉴部分均注明了原文出处,可在文章的`借鉴链接`处获取原文出处
文中若内容有涉及原版权,请邮件联系elvin-chen@qq.com,涉及的相关文章或内容将会及时更改或取消发布