引入Html2Image的Maven依赖
<dependency>
<groupId>com.github.xuwei-k</groupId>
<artifactId>html2image</artifactId>
<version>0.1.0</version>
</dependency>
import gui.ava.html.image.generator.HtmlImageGenerator;
public static void main(String[] args) {
HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
//加载html模版
imageGenerator.loadHtml(htmlTemplate);
//把html写入到图片
imageGenerator.saveAsImage("E:\\hello-world.png");
}
<dependency>
<groupId>gui.ava</groupId>
<artifactId>html2image</artifactId>
<version>2.0.1</version>
</dependency>
import gui.ava.html.parser.HtmlParser;
import gui.ava.html.parser.HtmlParserImpl;
import gui.ava.html.renderer.ImageRenderer;
import gui.ava.html.renderer.ImageRendererImpl;
public static void main(String[] args) {
HtmlParser htmlParser = new HtmlParserImpl();
htmlParser.loadHtml(HtmlTemplateStr);
// html 是我的html代码
ImageRenderer imageRenderer = new ImageRendererImpl(htmlParser);
imageRenderer.saveImage("E:\\lcxq1.png");
}
static String HtmlTemplateStr = "\t<div style=\"height: 500px;width: 500px;background: #aee0ff;\">\n" +
"\t\t这个是一个div\n" +
"\t\t<h1>标题</h1>\n" +
"\t\t<ol>\n" +
"\t\t\t<li>a</li>\n" +
"\t\t</ol>\n" +
"\t\t<img style=\"margin-left: 1500px;\" width=\"300px\" height=\"200px\" src=\"https://inews.gtimg.com/newsapp_bt/0/11911825373/1000\">\n" +
"\t</div>";
public class ImageRendererSubImpl extends ImageRendererImpl {
public ImageRendererSubImpl(DocumentHolder documentHolder) {
super(documentHolder);
}
private String getImageFormat(String filename) {
if (this.getImageFormat() != null) {
return this.getImageFormat();
} else {
return filename != null ? FormatNameUtil.formatForFilename(filename) : FormatNameUtil.getDefaultFormat();
}
}
private FSImageWriter getImageWriter(String imageFormat) {
FSImageWriter imageWriter = new FSImageWriter(imageFormat);
imageWriter.setWriteCompressionMode(this.getWriteCompressionMode());
imageWriter.setWriteCompressionQuality(this.getWriteCompressionQuality());
imageWriter.setWriteCompressionType(this.getWriteCompressionType());
return imageWriter;
}
public void saveImage(File file) {
try {
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
this.save(outputStream, file.getName(), true);
} catch (IOException var3) {
throw new RenderException("IOException while rendering image to " + file.getAbsolutePath(), var3);
}
}
public void saveImage(String filename) {
this.saveImage(new File(filename));
}
private void save(OutputStream outputStream, String filename, boolean closeStream) {
try {
String imageFormat = this.getImageFormat(filename);
FSImageWriter imageWriter = this.getImageWriter(imageFormat);
BufferedImage bufferedImage = this.getBufferedImage(getImageType(imageFormat));
imageWriter.write(bufferedImage, outputStream);
} catch (IOException var15) {
throw new RenderException("IOException while rendering image", var15);
} finally {
if (closeStream) {
try {
outputStream.close();
} catch (IOException var14) {
;
}
}
}
}
/**
* 获取图像类型
* 根据图像的格式
*/
public int getImageType(String imageFormat){
if ("jpg".equalsIgnoreCase(imageFormat)){
return BufferedImage.TYPE_3BYTE_BGR;
}
if ("bmp".equalsIgnoreCase(imageFormat)){
return BufferedImage.TYPE_INT_RGB;
}
return BufferedImage.BITMASK;
}
}
参考:
https://blog.csdn.net/superit401/article/details/82629524
https://www.cnblogs.com/zxf330301/p/5666592.html
https://www.cnblogs.com/wikid/p/3913070.html
https://blog.csdn.net/lijinzhou2017/article/details/88714381
jar包下载:https://download.csdn.net/download/qq_25775675/12511219
使用 wkhtmltox 进行转换:
https://blog.csdn.net/qq_25775675/article/details/110425304