溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

根據(jù)freemarker模板寫入數(shù)據(jù)并生成圖片的方法

發(fā)布時間:2021-06-18 16:02:12 來源:億速云 閱讀:2614 作者:chen 欄目:編程語言

這篇文章主要介紹“根據(jù)freemarker模板寫入數(shù)據(jù)并生成圖片的方法”,在日常操作中,相信很多人在根據(jù)freemarker模板寫入數(shù)據(jù)并生成圖片的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”根據(jù)freemarker模板寫入數(shù)據(jù)并生成圖片的方法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

需要使用到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 數(shù)據(jù)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是數(shù)據(jù)列表

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

效果:

根據(jù)freemarker模板寫入數(shù)據(jù)并生成圖片的方法

注意:

模板文件中的外部css和js是無法加載和起作用的,所以樣式要直接寫在模板文件中。

到此,關于“根據(jù)freemarker模板寫入數(shù)據(jù)并生成圖片的方法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI