java將html轉(zhuǎn)成圖片代碼實(shí)例(html2image)
前言
最近有一個(gè)需求需要根據(jù)指定的樣式生成圖片,使用java原生技術(shù)有些麻煩,所以上網(wǎng)搜了下案例,最后發(fā)現(xiàn)最好用的還是html2image,這里進(jìn)行簡(jiǎn)單總結(jié)下。
1.導(dǎo)入jar
<!-- 用于將html轉(zhuǎn)圖片-->
<dependency>
<groupId>gui.ava</groupId>
<artifactId>html2image</artifactId>
<version>2.0.1</version>
</dependency>
2.代碼
下面是示例代碼,這里使用String的方式傳入html信息,除了直接傳入還支持傳入文件流、傳入文件等其他方式,差距不大,這里不列舉其他方式的細(xì)節(jié)了。
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 class TestTableToImage {
public static void main(String[] args) throws Exception {
HtmlParser htmlParser = new HtmlParserImpl();
String HtmlTemplateStr = "<html lang=\"zh-CN\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>入庫(kù)單</title>\n" +
"</head>\n" +
"<body>\n" +
"\t<!--外層的表格 -->\n" +
"\t<table style=\"border: 2px solid black;padding: 10px \" cellpadding=\"5\" align=\"center\" >\n" +
"\t\n" +
"\t\t<tr >\n" +
"\t\t\t<th style=\"font-size:25px;height: 60px;\" align=\"center\">入庫(kù)單</th>\n" +
"\t\t</tr>\n" +
"\t\t\n" +
"\t\t<tr>\n" +
"\t\t\t<th>\n" +
"\t\t\t\t<table border=\"1\" style=\"border-color: #ccc;\" cellspacing=\"0\" cellpadding=\"15\" width=\"900px\">\n" +
"\t\t\t\t\t<tr style=\"color: #696969;height: 60px;\">\n" +
"\t\t\t\t\t\t<th style=\"width: 150px;\">調(diào)度單號(hào)</th> <th colspan=\"2\" style=\"width: 300px;\">調(diào)度單號(hào)22</th>\n" +
"\t\t\t\t\t\t<th style=\"width: 150px;\">客戶(hù)名稱(chēng)</th> <th colspan=\"2\" style=\"width: 300px;\">客戶(hù)名稱(chēng)22</th>\n" +
"\t\t\t\t\t</tr>\n" +
"\t\t\t\t\t\n" +
"\t\t\t\t\t<tr style=\"color: #696969;height: 50px;\">\n" +
"\t\t\t\t\t\t<th >車(chē)牌照號(hào)</th> <th colspan=\"2\" style=\"width: 300px;\">車(chē)牌照號(hào)22</th>\n" +
"\t\t\t\t\t\t<th>裝運(yùn)時(shí)間</th> <th colspan=\"2\" style=\"width: 300px;\">裝運(yùn)時(shí)間22</th>\n" +
"\t\t\t\t\t</tr>\n" +
"\t\t\t\t\t<tr height=\"50px;height: 50px;\">\n" +
"\t\t\t\t\t\t<td colspan=\"6\" style=\"font-size:23px;color: #696969\" align=\"center\">貨物信息</td>\n" +
"\t\t\t\t\t</tr>\n" +
"\t\t\t\t\t<tr style=\"color: #696969;height: 50px;\">\n" +
"\t\t\t\t\t\t<th>鋼卷號(hào)</th> <th colspan=\"2\" style=\"width: 300px;\">鋼卷號(hào)22</th>\n" +
"\t\t\t\t\t\t<th>磅單號(hào)</th> <th colspan=\"2\" style=\"width: 300px;\">磅單號(hào)22</th>\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t</tr>\n" +
"\t\t\t\t\t<tr style=\"color: #696969;height: 50px;\">\n" +
"\t\t\t\t\t\t<th>材質(zhì)</th><th style=\"width: 150px;\">材質(zhì)22</th>\n" +
"\t\t\t\t\t\t<th>規(guī)格型號(hào)</th><th style=\"width: 150px;\">規(guī)格型號(hào)22</th>\n" +
"\t\t\t\t\t\t<td>貨物名稱(chēng)</td><th style=\"width: 150px;\">貨物名稱(chēng)22</th>\n" +
"\t\t\t\t\t</tr>\n" +
"\t\t\t\t\t<tr style=\"color: #696969;height: 50px;\" align=\"center\">\n" +
"\t\t\t\t\t\t\n" +
"\t\t\t\t\t\t<td>件數(shù)</td> <th colspan=\"2\" style=\"width: 300px;\">件數(shù)22</th>\n" +
"\t\t\t\t\t\t<td>重量</td> <th colspan=\"2\" style=\"width: 300px;\">重量22</th>\n" +
"\t\t\t\t\t</tr>\n" +
"\t\t\t\t\t<tr style=\"color: #696969;height: 50px;\" align=\"center\">\n" +
"\t\t\t\t\t\t<td colspan=\"6\" style=\"font-size:23px;color: #696969\">客戶(hù)信息</td>\n" +
"\t\t\t\t\t</tr>\n" +
"\t\t\t\t\t<tr style=\"color: #696969;height: 50px;\">\n" +
"\t\t\t\t\t\t<th>收貨聯(lián)系人</th> <th colspan=\"2\" style=\"width: 300px;\">收貨聯(lián)系人22</th>\n" +
"\t\t\t\t\t\t<th>收貨人電話(huà)</th> <th colspan=\"2\" style=\"width: 300px;\">收貨人電話(huà)22</th>\n" +
"\t\t\t\t\t</tr>\n" +
"\t\t\t\t\t<tr style=\"color: #696969;height: 50px;\">\n" +
"\t\t\t\t\t\t<th >庫(kù)房名稱(chēng)</th> <th colspan=\"2\" style=\"width: 300px;\">庫(kù)房名稱(chēng)22</th>\n" +
"\t\t\t\t\t\t<th >庫(kù)房地址</th> <th colspan=\"2\" style=\"width: 300px;\">庫(kù)房地址22</th>\n" +
"\t\t\t\t\t</tr>\n" +
"\t\t\t\t\t\n" +
"\t\t\t\t\t<!--\n" +
"\t\t\t\t\t<tr style=\"color: #696969;height: 50px;\">\n" +
"\t\t\t\t\t\t<th >收貨地點(diǎn)</th> <th colspan=\"5\" style=\"width: 750px;\">收貨地點(diǎn)22</th>\n" +
"\t\t\t\t\t</tr>\n" +
"\t\t\t\t\t -->\n" +
"\t\t\t\t</table>\n" +
"\t\t\t</th>\n" +
"\t\t\t\n" +
"\t\t</tr>\n" +
"\t\t\n" +
"\t\t<tr style=\"height: 150px;\">\n" +
"\t\t\t<th></th>\n" +
"\t\t</tr>\n" +
"\t\t\n" +
"\t</table>\n" +
"</body>\n" +
"</html>";
htmlParser.loadHtml(HtmlTemplateStr);
ImageRenderer imageRenderer = new ImageRendererImpl(htmlParser);
imageRenderer.setWidth(955);
imageRenderer.saveImage("D:\\66.png");
}
}3.展示結(jié)果
執(zhí)行上面的代碼生成的圖片如下,可以看到效果還是不錯(cuò)的,相比于其他的三方j(luò)ar來(lái)說(shuō)這個(gè)jar還是挺方便,筆者也是嘗試了多個(gè)jar最后決定用的這個(gè)三方包,推進(jìn)使用,可以看到生成的效果還是不錯(cuò)的。

4.注意事項(xiàng)
我去maven倉(cāng)庫(kù)中找這個(gè)jar時(shí),發(fā)現(xiàn)這個(gè)包已經(jīng)很多年沒(méi)有更新了,不過(guò)還是很好用,可以不用在意這個(gè)事。
- 需要注意的是有一些格式如果不放到style中直接寫(xiě)可能識(shí)別不了,所以在使用時(shí)盡量所有格式寫(xiě)在style中,防止不生效
- 此外還有一點(diǎn),自動(dòng)生成圖片的寬度需要自己調(diào)整,這個(gè)寬度可能會(huì)有一些白邊,使用imageRenderer.setWidth進(jìn)行調(diào)整寬度即可
附:轉(zhuǎn)換jpg變紅的問(wèn)題處理
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) {
;
}
}
}
}
/**
* 獲取圖像類(lèi)型
* 根據(jù)圖像的格式
*/
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;
}
}
總結(jié)
到此這篇關(guān)于java將html轉(zhuǎn)成圖片的文章就介紹到這了,更多相關(guān)java將html轉(zhuǎn)成圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Lock接口實(shí)現(xiàn)原理及實(shí)例解析
這篇文章主要介紹了Java Lock接口實(shí)現(xiàn)原理及實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Spring Boot緩存實(shí)戰(zhàn) Caffeine示例
本篇文章主要介紹了Spring Boot緩存實(shí)戰(zhàn) Caffeine示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
Springboot整合easyexcel實(shí)現(xiàn)一個(gè)接口任意表的Excel導(dǎo)入導(dǎo)出
本文主要介紹了Springboot整合easyexcel實(shí)現(xiàn)一個(gè)接口任意表的Excel導(dǎo)入導(dǎo)出,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02
數(shù)據(jù)結(jié)構(gòu)與算法之手撕排序算法
排序算法看似簡(jiǎn)單,其實(shí)不同的算法中蘊(yùn)涵著經(jīng)典的算法策略。通過(guò)熟練掌握排序算法,就可以掌握基本的算法設(shè)計(jì)思想,本文主要介紹了Java中的排序算法,需要的朋友歡迎閱讀2023-04-04
Java設(shè)計(jì)模式以虹貓藍(lán)兔的故事講解橋接模式
橋接是用于把抽象化與實(shí)現(xiàn)化解耦,使二者可以獨(dú)立變化。這種類(lèi)型的設(shè)計(jì)模式屬于結(jié)構(gòu)型模式,它通過(guò)提供抽象化和實(shí)現(xiàn)化之間的橋接結(jié)構(gòu),來(lái)實(shí)現(xiàn)二者的解耦。這種模式涉及到一個(gè)作為橋接的接口,使得實(shí)體類(lèi)的功能獨(dú)立于接口實(shí)現(xiàn)類(lèi)。這兩種類(lèi)型的類(lèi)可被結(jié)構(gòu)化改變而互不影響2022-04-04
IO流:java中解碼和編碼出現(xiàn)亂碼說(shuō)明以及代碼實(shí)現(xiàn)方法
最近使用Java編寫(xiě)一些讀取文件的小工具的時(shí)候,經(jīng)常與IO流打交道,但是自己對(duì)IO流的理解不是特別深刻,趁機(jī)整理下,這篇文章主要給大家介紹了關(guān)于IO流:java中解碼和編碼出現(xiàn)亂碼說(shuō)明以及代碼實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-11-11

