freemarker将HTML生成PDF保存本地或者输出流

//参数
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("aa", aa);
//本地路径
String outPath = "D:\\printingPdf.pdf";
//模板文件夹路径
String templateDirectory = "D:\\";
String templateContent = null;
try {   
   //生成模板
   templateContent = PdfUtilTest.getTemplateContent(templateDirectory, "src\\main\\resources\\templates\\po\\printingPdf2.html", paramMap);
} catch (Exception e) {
   e.printStackTrace();
}
//下载到本地
PdfUtilTest.html2Pdf(templateContent, outPath);

1、生成模板工具

/**
 * 获取模板内容
 * @param templateDirectory 模板文件夹
 * @param templateName      模板文件名
 * @param paramMap          模板参数
 * @return
 * @throws Exception
 */
public static String getTemplateContent(String templateDirectory, String templateName, Map<String, Object> paramMap) throws Exception {
    Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
    try {
        configuration.setDirectoryForTemplateLoading(new File(templateDirectory));
    } catch (Exception e) {
        System.out.println("-- exception --");
    }

    Writer out = new StringWriter();
    Template template = configuration.getTemplate(templateName,"UTF-8");
    template.process(paramMap, out);
    out.flush();
    out.close();
    return out.toString();
}

2、下载到本地工具

/**
 * HTML 转 PDF
 * @param fileString html内容
 * @return 下载到本地
 */
public static boolean  html2Pdf(String fileString, String outPath) {
    try {
        Document document = new Document();
       // ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outPath));
        document.open();
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,new ByteArrayInputStream(fileString.getBytes()), null, Charset.forName("UTF-8"));

        System.out.println("生成的合同" + fileString);

        //baos.close();
        document.close();
    } catch (Exception e) {
        System.out.println("生成模板内容失败"+e.fillInStackTrace());
        return false;
    }
    return true;
}

3、返回字节数组util

/**
 * HTML 转 PDF
 * @param content html内容
 * @return PDF字节数组
 */
public static byte[] htmlToPdf(String content) {
    ByteArrayOutputStream outputStream = null;
    try {
        Document document = new Document();
        outputStream = new ByteArrayOutputStream();
        PdfWriter writer = PdfWriter.getInstance(document, outputStream);
        document.open();
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,new ByteArrayInputStream(content.getBytes()), null, Charset.forName("UTF-8"));
        document.close();
    } catch (Exception e) {
        System.out.println("------生成pdf失败-------");
    }
    return outputStream.toByteArray();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值