java將html轉(zhuǎn)成圖片代碼實例(html2image)
前言
最近有一個需求需要根據(jù)指定的樣式生成圖片,使用java原生技術(shù)有些麻煩,所以上網(wǎng)搜了下案例,最后發(fā)現(xiàn)最好用的還是html2image,這里進行簡單總結(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信息,除了直接傳入還支持傳入文件流、傳入文件等其他方式,差距不大,這里不列舉其他方式的細節(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>入庫單</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\">入庫單</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)度單號</th> <th colspan=\"2\" style=\"width: 300px;\">調(diào)度單號22</th>\n" + "\t\t\t\t\t\t<th style=\"width: 150px;\">客戶名稱</th> <th colspan=\"2\" style=\"width: 300px;\">客戶名稱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 >車牌照號</th> <th colspan=\"2\" style=\"width: 300px;\">車牌照號22</th>\n" + "\t\t\t\t\t\t<th>裝運時間</th> <th colspan=\"2\" style=\"width: 300px;\">裝運時間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>鋼卷號</th> <th colspan=\"2\" style=\"width: 300px;\">鋼卷號22</th>\n" + "\t\t\t\t\t\t<th>磅單號</th> <th colspan=\"2\" style=\"width: 300px;\">磅單號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ī)格型號</th><th style=\"width: 150px;\">規(guī)格型號22</th>\n" + "\t\t\t\t\t\t<td>貨物名稱</td><th style=\"width: 150px;\">貨物名稱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\">客戶信息</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>收貨人電話</th> <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;\">\n" + "\t\t\t\t\t\t<th >庫房名稱</th> <th colspan=\"2\" style=\"width: 300px;\">庫房名稱22</th>\n" + "\t\t\t\t\t\t<th >庫房地址</th> <th colspan=\"2\" style=\"width: 300px;\">庫房地址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 >收貨地點</th> <th colspan=\"5\" style=\"width: 750px;\">收貨地點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í)行上面的代碼生成的圖片如下,可以看到效果還是不錯的,相比于其他的三方j(luò)ar來說這個jar還是挺方便,筆者也是嘗試了多個jar最后決定用的這個三方包,推進使用,可以看到生成的效果還是不錯的。
4.注意事項
我去maven倉庫中找這個jar時,發(fā)現(xiàn)這個包已經(jīng)很多年沒有更新了,不過還是很好用,可以不用在意這個事。
- 需要注意的是有一些格式如果不放到style中直接寫可能識別不了,所以在使用時盡量所有格式寫在style中,防止不生效
- 此外還有一點,自動生成圖片的寬度需要自己調(diào)整,這個寬度可能會有一些白邊,使用imageRenderer.setWidth進行調(diào)整寬度即可
附:轉(zhuǎn)換jpg變紅的問題處理
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) { ; } } } } /** * 獲取圖像類型 * 根據(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot緩存實戰(zhàn) Caffeine示例
本篇文章主要介紹了Spring Boot緩存實戰(zhàn) Caffeine示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02Springboot整合easyexcel實現(xiàn)一個接口任意表的Excel導(dǎo)入導(dǎo)出
本文主要介紹了Springboot整合easyexcel實現(xiàn)一個接口任意表的Excel導(dǎo)入導(dǎo)出,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02數(shù)據(jù)結(jié)構(gòu)與算法之手撕排序算法
排序算法看似簡單,其實不同的算法中蘊涵著經(jīng)典的算法策略。通過熟練掌握排序算法,就可以掌握基本的算法設(shè)計思想,本文主要介紹了Java中的排序算法,需要的朋友歡迎閱讀2023-04-04IO流:java中解碼和編碼出現(xiàn)亂碼說明以及代碼實現(xiàn)方法
最近使用Java編寫一些讀取文件的小工具的時候,經(jīng)常與IO流打交道,但是自己對IO流的理解不是特別深刻,趁機整理下,這篇文章主要給大家介紹了關(guān)于IO流:java中解碼和編碼出現(xiàn)亂碼說明以及代碼實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2023-11-11