java 后台 生成的表格通过HtmlImageGenerator(Html2Image) 生成图片,以及解决汉字乱码的问题

本人因为业务需求,需要在后台生成数据表格,然后推送到公众号里面,所以整理了下面这篇文章,可以生产图片

转:https://blog.csdn.net/jishoujiang/article/details/78566759  参考的是这个博客;

在他的基础上改的:但是上面没有说明缺点,下面我将详细的进行介绍:

上面的那篇博客已经很完善了,但是没有给出生产表格的入参,本人就加上了;

package tk.mybatis.web.controller;

import java.util.ArrayList;
import java.util.List;
import gui.ava.html.image.generator.HtmlImageGenerator;

public class TestTable2 {

	private static String getSingleImageHtml(String title, List<String> headTitle, List<List<String>> contentArray) {
		// String html = "<!DOCTYPE html><html><body style=\"width:570px;\">";
		String html = "<table border=\"1\" style=\"border-collapse:collapse;width:570px;border:1px solid black;background: white;\" cellpadding=\"0\" cellspacing=\"0\">";

		html += "<thead>";
		html += "<tr ><th colspan=\"4\" style=\"text-align: center;width: 570px;height:40px;font-size: 22px;\">" + title
				+ "</th></tr>";
		html += "<tr>";
		for (int i = 0; i < 4; i++) {
			String thEle = "";
			thEle = "<th style=\"height:30px;width:" + (i == 2 ? "52%" : "16%")
					+ ";text-align: center;margin:0;color: #ffffff;background:#0070c0;border:1px solid black;font-size: 20px;\">"
					+ headTitle.get(i) + "</th>";
			html += thEle;
		}
		html += "</tr>";
		html += "</thead>";

		html += "<tbody>";
		for (List<String> contents : contentArray) {
			html += "<tr>";
			for (int j = 0; j < 4; j++) {
				String tdEle = "";
				tdEle = "<td style=\"height:30px;width:" + (j == 2 ? "52%" : "16%")
						+ ";text-align: center;margin:0;border:1px solid black;font-size: 18px;\">" + contents.get(j)
						+ "</td>";
				html += tdEle;
			}
			html += "</tr>";
		}

		html += "</tbody>";
		html += "</table>";
		// html+="</body></html>";
		return html;
	}

	public static String graphicsHtmlGeneration(List<List<List<String>>> allValue, List<String> titles,
			List<List<String>> headers) throws Exception {
		int i = 0;
		String html = "";
		for (List<List<String>> list : allValue) {
			String title = titles.get(i);
			List<String> headTitle = headers.get(i);
			html += getSingleImageHtml(title, headTitle, list);
			i++;
		}
		if (html != null && html.length() > 0) {
			HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
			imageGenerator.loadHtml(html);
			try {
				// Thread.sleep(5000);
				imageGenerator.getBufferedImage();
				// Thread.sleep(8000);
				String path = "d:\\1.png";
				imageGenerator.saveAsImage(path);
				return path;
			} catch (Exception e) {
			}
		}
		return null;

	}

	public static void main(String[] args) {
		List<String> allValue1 = new ArrayList<String>();
		allValue1.add("汉字");
		allValue1.add("这个行不11111");
		allValue1.add("最后");
		allValue1.add("6");
		List<List<String>> allValue2 = new ArrayList<>();
		allValue2.add(allValue1);
		allValue2.add(allValue1);
		allValue2.add(allValue1);
		List<List<List<String>>> allValue = new ArrayList<>();
		allValue.add(allValue2);
		List<String> titles = new ArrayList<>();
		titles.add("标题可以识汉字不");
		List<List<String>> headers = new ArrayList<>();
		List<String> header = new ArrayList<>();
		header.add("测试一");
		header.add("北京武汉");
		header.add("哈尔滨");
		header.add("7月营业额");
		headers.add(header);
		try {
			TestTable2.graphicsHtmlGeneration(allValue, titles, headers);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

注意:第一:html不能够添加head,只能是现在这种样子;

          第二:在linux上运行可能会出现中文汉字乱码(中文汉字空格)。这个时候有两种解决办法。第一:https://blog.csdn.net/hu_wen/article/details/78543645;第二:https://blog.csdn.net/eric_hwp/article/details/69524407(这个方法中的第三种);我这边一开始使用的是第一种办法,是在自己电脑的虚拟机上centos,解决了问题;但是到了公司的redhat服务器上就不好使了。我找了很多办法,都没有生效,后来又看了下这个jar包的源码,看用到了awt这个包,后来就去从awt乱码这个方向查了下,就招到了第二种方法,就解决了;如果你项目也用到了awt,也是乱码,原因就是应用没有找到系统的字符集;

记住操作完成需要重启下web服务器(tomcat);

非常感谢上面的两篇博客,在这里解决了我的问题;如有版权问题,请联系我;

顺便把需要的jar包给大家吧:

<dependency>
                <groupId>com.github.xuwei-k</groupId>              
                  <artifactId>html2image</artifactId>
                  <version>0.1.0</version>
</dependency>

有什么问题可以给我留言,如果觉得文章不错,希望得到点赞!

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值