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/n/373212.html