Freemarker是一款模板引擎,在Java中非常流行。在項目中,我們經常需要根據數據生成文件,如生成HTML、PDF等。這時候,我們可以使用Freemarker代碼生成器,它能夠幫助我們方便快捷地生成這些文件。
一、Freemarker生成圖片
在某些場景下,我們需要根據數據生成圖片。使用Freemarker可以很方便地實現這個功能。比如,我們需要根據商品數據生成商品圖片,那麼可以使用如下代碼:
public static void generateImage() throws Exception {
//創建數據模型
Map dataModel = new HashMap();
dataModel.put("product_name", "iPhone12");
dataModel.put("price", "8999");
dataModel.put("description", "全球產能缺口嚴重");
//創建配置對象
Configuration config = new Configuration();
config.setClassForTemplateLoading(FreemarkerGenerator.class, "/templates");
//獲取模板對象
Template template = config.getTemplate("product.ftl");
//創建文件輸出流
File outFile = new File("product.jpg");
FileOutputStream fos = new FileOutputStream(outFile);
//合併模板和數據模型
BufferedImage bi = ImageIO.read(new ByteArrayInputStream(FreeMarkerTemplateUtils.processTemplateIntoString(template, dataModel).getBytes()));
//輸出文件
ImageIO.write(bi, "jpg", fos);
}
以上代碼中,我們先創建了一個數據模型,包含商品名稱、價格和描述。然後使用了Configuration類來設置模板的加載路徑,我們在/resources/templates/目錄下創建了product.ftl的模板文件。接着獲取模板對象,使用FreeMarkerTemplateUtils.processTemplateIntoString()方法將模板和數據模型合併,最後通過ImageIO.write()方法將生成的圖片輸出到文件中。
二、Freemarker生成PDF
在某些場景下,我們需要根據數據生成PDF文件。使用Freemarker可以很方便地實現這個功能。比如,我們需要根據訂單數據生成訂單PDF文件,那麼可以使用如下代碼:
public static void generatePdf() throws Exception {
//創建數據模型
List<Map> dataList = new ArrayList();
Map order1 = new HashMap();
order1.put("order_no", "10001");
order1.put("product_name", "iPhone12");
order1.put("price", "8999");
order1.put("count", 1);
Map order2 = new HashMap();
order2.put("order_no", "10002");
order2.put("product_name", "iPad Pro");
order2.put("price", "8999");
order2.put("count", 2);
dataList.add(order1);
dataList.add(order2);
Map dataModel = new HashMap();
dataModel.put("dataList", dataList);
//創建配置對象
Configuration config = new Configuration();
config.setClassForTemplateLoading(FreemarkerGenerator.class, "/templates");
//獲取模板對象
Template template = config.getTemplate("order.ftl");
//合併模板和數據模型
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, dataModel);
ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes("utf-8"));
//設置字體
BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont, 12);
//創建文件輸出流
FileOutputStream fos = new FileOutputStream("order.pdf");
//生成PDF
Document document = new Document();
PdfWriter.getInstance(document, fos);
document.open();
InputStreamReader isr = new InputStreamReader(bis, "utf-8");
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
Paragraph paragraph = new Paragraph(line, font);
document.add(paragraph);
}
document.close();
}
以上代碼中,我們先創建了一個數據模型,包含訂單列表。然後使用了Configuration類來設置模板的加載路徑,我們在/resources/templates/目錄下創建了order.ftl的模板文件。接着獲取模板對象,使用FreeMarkerTemplateUtils.processTemplateIntoString()方法將模板和數據模型合併成字符串,然後通過ByteArrayInputStream來轉換成字節流。接下來設置字體、創建文件輸出流和生成PDF,這裡使用了iText庫來生成。
三、總結
通過以上兩個示例,我們可以看到使用Freemarker代碼生成器能夠快速方便地生成圖片、PDF等文件。我們只需要關注數據模型和模板,就能夠生成我們所需要的文件。
需要注意的是,在生成PDF時需要使用字體,並且設置字體的方法在不同的庫中可能略有不同。同時,在實際使用中,還需要考慮數據的安全性和性能等問題,我們應該選擇合適的解決方案。
原創文章,作者:KNAZQ,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/373212.html