栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

PDF模板数据填充

PDF模板数据填充

PDF模板数据填充

pom依赖

   
      freemarker
      freemarker
      2.3.9
    
    
      com.itextpdf
      itextpdf
      5.5.13.2 
 	
public static String processHtmlParam(Map param,byte[] bytes,String fileName) throws Exception {

    File modelFile = new File(System.getProperty("user.dir"));
    if (!modelFile.exists()) {
      modelFile.mkdirs();
    }

    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(System.getProperty("user.dir") + "/" + fileName);
      fos.write(bytes);
    }catch (Exception e) {
      System.out.println("文件写入失败 : " + e.getMessage());
    } finally {
      if ( null!=fos ) {
        fos.close();
      }
    }
    Configuration configuration = new Configuration();
    configuration.setDirectoryForTemplateLoading(modelFile);
    configuration.setObjectWrapper(new DefaultObjectWrapper());
    configuration.setDefaultEncoding("UTF-8");
    configuration.setClassicCompatible(true);

    Template template = configuration.getTemplate(fileName);

    StringWriter stringWriter = new StringWriter();
    BufferedWriter writer = new BufferedWriter(stringWriter);
    template.process(param, writer);
    String htmlStr = stringWriter.toString();

    writer.flush();
    writer.close();

    delFile(System.getProperty("user.dir"),fileName);

    return htmlStr;
  }
  public static void delFile(File file) {
    if (file.exists() && file.isFile()) {
      file.delete();
    }
  }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/663355.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号