Freemarker生成HTML页面及字符串,HTML页面生成PDF文件

Freemarker生成生成HTML三种情况
1、通过.flt模板生成file
2、根据模板字符串生成file
3、根据模板字符串生成HTML文件字符串

开始

模板数据准备

 private static void autoGenHTMLTest()
    {
        //给模板文件组装数据
        Map dataMap = new HashMap();
        SpecialInfo specialInfo = new SpecialInfo();
        specialInfo.setSpecialName("xxx专项");
        specialInfo.setSpecialProjectName("xxx项目");
           /*${policyBasis.title}
            
            
                政策文号
                ${policyBasis.documentNumber}*/
            PolicyBasis policyBasis = new PolicyBasis();
           policyBasis.setTitle("xxx标题");
           policyBasis.setDocumentNumber("xxx文号");
        dataMap.put("project",specialInfo);
        dataMap.put("policyBasis",policyBasis);
        dataMap.put("title","标题");

        String DEST_PATH = "e:/freemarker";//目标路径
        genFileWithTemplate(TEMPLATE_PATH,DEST_PATH,"test.ftl","hello.html",dataMap);
    }

一、通过.flt模板生成file

//通过.flt模板生成file
    private static void genFileWithTemplate(String templateDir,String destDir,String templateFileName,String destFileName,Map dataMap)
    {
        //创建freeMarker配置实例
        Configuration configuration = new Configuration();
        Writer out = null;
        try {
            //设置模版路径
            configuration.setDirectoryForTemplateLoading(new File(templateDir));
            //加载模版文件
            Template template = configuration.getTemplate(templateFileName);
            //生成数据
            File docFile = new File(destDir + "\\" + destFileName);
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            //输出文件
            template.process(dataMap, out);
            System.out.println(destFileName+" 模板文件创建成功 !");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != out) {
                    out.flush();
                    out.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

二、根据模板字符串生成file

//根据模板字符串自动创建html-示例
    private static void autoGenHTMLStrTest(){
        //给模板文件组装数据
        Map dataMap = new HashMap();
        SpecialInfo specialInfo = new SpecialInfo();
        specialInfo.setSpecialName("xxx专项");
        specialInfo.setSpecialProjectName("xxx项目");
           /*${policyBasis.title}
            
            
                政策文号
                ${policyBasis.documentNumber}*/
           List policyBasisList = new ArrayList<>();
           for(int i=0;i<10;i++){
               PolicyBasis policyBasis = new PolicyBasis();
               policyBasis.setTitle("xxx标题");
               policyBasis.setDocumentNumber("xxx文号");
               policyBasisList.add(policyBasis);
           }

        dataMap.put("policyBasis",policyBasisList);
        //dataMap.put("project",specialInfo);

        dataMap.put("title","标题");

        String templateString="${title}
<#list policyBasis as item>
政策标题政策文号:
${item.title}${item.documentNumber}
\n"; String DEST_PATH = "e:/freemarker";//目标路径 genFileWithTemplate(DEST_PATH,"hello7.html",dataMap,templateString); //genFileWithTemplate(dataMap,templateString); } //html字符串生成模板 private static void genFileWithTemplate(String destDir,String destFileName,Map dataMap,String templateString){ //使用一个模板加载器变成模板 StringTemplateLoader stringTemplateLoader = new StringTemplateLoader(); stringTemplateLoader.putTemplate("template",templateString); //在配置中设置模板加载器 Configuration configuration = new Configuration(); configuration.setTemplateLoader(stringTemplateLoader); //创建freeMarker配置实例 Writer out = null; try { //获取模板的内容 Template template = configuration.getTemplate("template","utf-8"); //生成数据 File docFile = new File(destDir + "\\" + destFileName); out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile))); //输出文件 template.process(dataMap, out); System.out.println(" 模板文件创建成功 !"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != out) { out.flush(); out.close(); } } catch (Exception e2) { e2.printStackTrace(); } } }

三、根据模板字符串生成HTML文件字符串

 private static void  genFileWithTemplate(Map dataMap,String templateString){
        StringWriter stringWriter = new StringWriter();
        //读取模板的位置在resources 下的templates下contractAgreement.ftl
        //使用一个模板加载器变成模板
        StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
        stringTemplateLoader.putTemplate("template",templateString);
        //在配置中设置模板加载器
        Configuration configuration = new Configuration();
        configuration.setTemplateLoader(stringTemplateLoader);

        try {
            Template template = configuration.getTemplate("template","utf-8");
            template.process(dataMap, stringWriter);
        } catch (Exception e) {
            e.printStackTrace();
        }
        stringWriter.flush();
        System.out.println(stringWriter.toString());
    }

HTML转PDF

Java Html转pdf实战
https://www.jianshu.com/p/d07bca50daed?tdsourcetag=s_pctim_aiomsg

安装文中步骤来,注意修改js中和代码中文件路径。

补充:提取HTML或fit文件字符串中要求的某段字符串

例子

private static void getAnchorPoint(String str) {
        // 把要匹配的字符串写成正则表达式,然后要提取的字符使用括号括起来
        // 在这里,我们要提取最后一个数字,正则规则就是“一个数字加上大于等于0个非数字再加上结束符”
        //
Pattern pattern = Pattern.compile("(\\d)[^\\d]*$"); Matcher matcher = pattern.matcher(str); if (matcher.find()){ System.out.println(matcher.group(1)); } }

业务实例

 //
//正则表达式获取锚点id Pattern pattern = Pattern.compile("
"); Matcher matcher = pattern.matcher(str); while (matcher.find()){ System.out.println(matcher.group(2)); }

你可能感兴趣的:(java及javaweb)