PDF模板数据填充

PDF模板数据填充,第1张

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();
    }
  }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5665308.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存