package com.cz.order.util;
import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;
import com.mybase.util.FileUtil;
public class Html2ImageBizImpl {
/**
* wkhtmltox的使用命令,对window来讲是exe的地址,对于linux是安装的命令地址(如/usr/local/bin/wkhtmltoimg)
*/
private String wkcmd = "D:\\wkhtmltopdf\\bin\\wkhtmltoimage.exe";
//生成图片宽度
private String width = "1899";
/**
* 用于存放临时文件的文件夹,由于wkhtmltox为系统插件独立进程,只能作为文件操作后再按流进行读取
*/
private static String tmpFilePath = "D:/wkhtmltopdf/temp_image";
/**
* html转图片
* @param htmlString
* @return
*/
public String getToPngBase64(String htmlStr) {
String base64Img = "";
String htmlFileName = null;
String pngFileName = null;
try {
// 存HTML文件(临时html)
htmlFileName = saveHtml2File(htmlStr);
// 转为PNG(临时图片),获取其文件名
pngFileName = html2Png(htmlFileName);
return pngFileName;
} finally {
// 删除临时文件
if (htmlFileName != null) {
//removeTmpFile(htmlFileName);
}
if (pngFileName != null) {
//removeTmpFile(pngFileName);
}
}
}
public byte[] stringToPng(String htmlString) {
String htmlFileName = null;
String pngFileName = null;
try {
// 存HTML文件
htmlFileName = saveHtml2File(htmlString);
// 转为PNG,获取其文件名
pngFileName = html2Png(htmlFileName);
// 将PNG读取为bytes
return readFile2ByteArray(pngFileName);
} finally {
// 删除临时文件
if (htmlFileName != null) {
removeTmpFile(htmlFileName);
}
if (pngFileName != null) {
removeTmpFile(pngFileName);
}
}
}
/**
* 将HTML内容写入文件
* @param htmlString HTML文件内容
*/
private String saveHtml2File(String htmlString) {
String fileName = getRandomFileName(".html");
try (FileWriter fileWriter = new FileWriter(new File(fileName))) {
fileWriter.write(htmlString);
} catch (Exception e) {
throw new RuntimeException(e);
}
//System.out.println("保存HTML成功: {"+fileName+"}");
return fileName;
}
/**
* 使用wkhtmltox将HTML转为PNG(都在硬盘上操作
*
* @param htmlFileName HTML文件路径
*/
private String html2Png(String htmlFileName) {
String pngFileName = getRandomFileName(".png");
StringBuilder cmd = new StringBuilder();
cmd.append(wkcmd);
cmd.append(" --width "+width);
cmd.append(" ");
cmd.append(htmlFileName);
cmd.append(" ");
cmd.append(pngFileName);
try {
Process proc = Runtime.getRuntime().exec(cmd.toString());
proc.getInputStream();
proc.waitFor();
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("转PNG成功: {"+htmlFileName+"}, {"+pngFileName+"}");
return pngFileName;
}
/**
* 读取文件二进制数组
*
* @param fileName
*/
private byte[] readFile2ByteArray(String fileName) {
File file = new File(fileName);
try {
return Files.readAllBytes(file.toPath());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 删除临时文件
*
* @param fileName 文件名
*/
private void removeTmpFile(String fileName) {
File file = new File(fileName);
if (file.isDirectory()) {
throw new IllegalArgumentException("不能删除文件夹");
}
try {
Files.delete(file.toPath());
System.out.println("删除临时文件成功:{"+fileName+"}");
} catch (Exception e) {
System.out.println("删除临时文件失败");
}
}
/**
* 构建随机的文件地址(采用IdGenerator)
* @param suffix 如.html
*/
private String getRandomFileName(String suffix) {
try {
File folder = new File(tmpFilePath);
if (!folder.exists()) { // 如果文件夹不存在则进行创建
folder.mkdirs();
}
} catch (Exception e) {
}
return tmpFilePath + "/" + "test_template" + suffix;
}
public static void main(String[] args) {
//需要生成的html图片
String textHtml = FileUtil.getPathToString("C:\\Users\\Administrator\\Desktop\\封面\\jt_template.html");
System.out.println(new Html2ImageBizImpl().getToPngBase64(textHtml));
}
}
需要下载exe程序,然后安装后,代码运行的路径改成你安装的路径
1.下载地址入口:wkhtmltopdf
2.根据自己电脑进行下载适应的类型的
最终效果图