RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
根据freemarker模板写入数据并生成图片的方法

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

赣县网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。创新互联建站于2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站

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


  org.xhtmlrenderer
  core-renderer
  R8

工具类FtlUtil:

public class FtlUtil {
	
	private static String getTemplate(String template, Map 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 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:




  
  
  报表模板
  
  	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;
	}
  




  
             
${title}
                                                    <#list fieldNames as fieldNames>                                                                  <#list list as row>                        ${row_index + 1}             <#list fields as field>                                                                
#${fieldName}
${row[field]!''}
           
      

调用FtlUtil.turnImage生成图片:

Map 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模板写入数据并生成图片的方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


当前标题:根据freemarker模板写入数据并生成图片的方法
网站地址:http://scyingshan.cn/article/peehje.html