這篇文章主要介紹“根據(jù)freemarker模板寫(xiě)入數(shù)據(jù)并生成圖片的方法”,在日常操作中,相信很多人在根據(jù)freemarker模板寫(xiě)入數(shù)據(jù)并生成圖片的方法問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”根據(jù)freemarker模板寫(xiě)入數(shù)據(jù)并生成圖片的方法”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
贛縣網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站開(kāi)發(fā)等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)建站于2013年開(kāi)始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)建站。
需要使用到core-renderer-R8這個(gè)工具,加入以下依賴(lài):
<dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>core-renderer</artifactId> <version>R8</version> </dependency>
工具類(lèi)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;
}
/**
* 模板文件轉(zhuǎn)圖片
* @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>報(bào)表模板</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>調(diào)用FtlUtil.turnImage生成圖片:
Map<String,Object> map = new HashMap<>();
map.put("title", "測(cè)試報(bào)表"); // 標(biāo)題
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);效果:

注意:
模板文件中的外部css和js是無(wú)法加載和起作用的,所以樣式要直接寫(xiě)在模板文件中。
到此,關(guān)于“根據(jù)freemarker模板寫(xiě)入數(shù)據(jù)并生成圖片的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
標(biāo)題名稱(chēng):根據(jù)freemarker模板寫(xiě)入數(shù)據(jù)并生成圖片的方法
本文網(wǎng)址:http://www.chinadenli.net/article24/peehje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、App設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)