温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

根据freemarker模板写入数据并生成图片的方法

发布时间:2021-06-18 16:02:12 来源:亿速云 阅读:2570 作者:chen 栏目:编程语言

这篇文章主要介绍“根据freemarker模板写入数据并生成图片的方法”,在日常操作中,相信很多人在根据freemarker模板写入数据并生成图片的方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”根据freemarker模板写入数据并生成图片的方法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

需要使用到core-renderer-R8这个工具,加入以下依赖:

<dependency>
  <groupId>org.xhtmlrenderer</groupId>
  <artifactId>core-renderer</artifactId>
  <version>R8</version>
</dependency>

工具类FtlUtil:

public class FtlUtil {
	
	private static String getTemplate(String template, Map<String, Object> map) throws IOException, TemplateException {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_30);
        String templatePath = FtlUtil.class.getResource("/").getPath() + "/templates";
        cfg.setDirectoryForTemplateLoading(new File(templatePath));
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        cfg.setLogTemplateExceptions(false);
        
        Template temp = cfg.getTemplate(template);
        StringWriter stringWriter = new StringWriter();
        temp.process(map, stringWriter);
        
        String result = stringWriter.getBuffer().toString();
        
        stringWriter.flush();
        stringWriter.close();
        
        return result;
    }

    /**
	 * 模板文件转图片
	 * @param template 模板文件地址
	 * @param map 数据map
	 * @param tagPath 保存图片地址
	 * @param width 图片宽度
	 * @param height 图片高度
	 * @throws Exception
	 */
    public static void turnImage(String template, Map<String, Object> map, String tagPath, int width, int height) throws Exception {
        String html = getTemplate(template, map);

        byte[] bytes = html.getBytes();
        ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
        
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(bin);
        
        Java2DRenderer renderer = new Java2DRenderer(document, width, height);
        BufferedImage img = renderer.getImage();
        
        ImageIO.write(img, "jpg", new File(tagPath));
    }

}

freemarker模板文件report.html:

<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="utf-8"></meta>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"></meta>
  <title>报表模板</title>
  <style type="text/css">
  	header div {
	    font-size: 16px;
	}
	table {
	    border-collapse: collapse;
	    width: 100%;
	    margin-bottom: 1rem;
	    color: #212529;
	    border-color: rgba(77, 82, 89, 0.07)!important;
	    border: 1px solid #dee2e6;
	}
	table td, table th {
	    border: 1px solid #dee2e6;
	}
  </style>
</head>

<body>

  <div>
      
      <header><div>${title}</div></header>

      <table>
        <thead>
          <tr>
            <th>#</th>
            <#list fieldNames as fieldNames>
            <th>${fieldName}</th>
            </#list>
          </tr>
        </thead>
        <tbody>
          <#list list as row>
          <tr>
            <th scope="row">${row_index + 1}</th>
            <#list fields as field>
            <td>${row[field]!''}</td>
            </#list>
          </tr>
          </#list>
        </tbody>
      </table>
      
    </div>
      
</body>
</html>

调用FtlUtil.turnImage生成图片:

Map<String,Object> map = new HashMap<>();
map.put("title", "测试报表"); // 标题
map.put("fieldNames", fieldNames); // fieldNames是字段中文名
map.put("fields", fields); // fields是字段名
map.put("list", list); // list是数据列表

FtlUtil.turnImage("report.html", map, "result.jpg", 600, 700);

效果:

根据freemarker模板写入数据并生成图片的方法

注意:

模板文件中的外部css和js是无法加载和起作用的,所以样式要直接写在模板文件中。

到此,关于“根据freemarker模板写入数据并生成图片的方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI