freemark方式讲HTML模版转换成PDF

/**
 * freemarker渲染html
 */
public static String freeMarkerRender(Map<String, Object> data, String htmlTmp, String path) {

    Writer out = new StringWriter();
    try {
        freemarkerCfg.setDirectoryForTemplateLoading(new File(path));
        // 获取模板,并设置编码方式
        Template template = freemarkerCfg.getTemplate(htmlTmp);
        template.setEncoding("UTF-8");
        // 合并数据模型与模板
        template.process(data, out); //将合并后的数据和模板写入到流中,这里使用的字符流
        out.flush();
        return out.toString();
    } catch (Exception e) {
        LOGGER.error("freemarker渲染html异常" + e);
    } finally {
        try {
            out.close();
        } catch (IOException ex) {
            LOGGER.error("freemarker渲染html异常" + ex);
        }
    }
    return null;
}
public static String createPdf(String content, String fileName, String imgCode) {
    String url = "";
    ByteArrayOutputStream baos = null;
    // step 1
    Document document = new Document();
    baos = new ByteArrayOutputStream();//构建字节输出流
    ByteArrayInputStream inStream = null;
    try {
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, baos);
        writer.setViewerPreferences(PdfWriter.HideToolbar);
     //添加水印
        if (imgCode.equals("1")) {
            writer.setPageEvent(new BackGroundImage(WAREHOUSEDISTRIBUTEIMG1)); //WAREHOUSEDISTRIBUTEIMG1 水印图片地址
        }
        if (imgCode.equals("2")) {
            writer.setPageEvent(new BackGroundImage(WAREHOUSEDISTRIBUTEIMG2));//WAREHOUSEDISTRIBUTEIMG2 水印图片地址
        }
        if (imgCode.equals("3")) {
            writer.setPageEvent(new BackGroundImage(WAREHOUSEDISTRIBUTEIMG3)); //WAREHOUSEDISTRIBUTEIMG3 水印图片地址
        }
        if (imgCode.equals("4")) {
            writer.setPageEvent(new BackGroundImage(WAREHOUSEDISTRIBUTEIMG4));//WAREHOUSEDISTRIBUTEIMG4 水印图片地址
        }
        // step 3
        document.open();
     //   BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new ByteArrayInputStream(content.getBytes()));
        // step 5
        document.close();
        String result = new String(baos.toByteArray(), "ISO-8859-1");//转字符串设置编码
        inStream = new ByteArrayInputStream(
                result.getBytes("ISO-8859-1"));
        url = FileOssUtil.uploadFile(inStream, fileName, ".pdf", 0);
    } catch (Exception e) {
        LOGGER.error("创建PDF异常" + e);
    } finally {
        try {
            if (baos != null) {
                baos.close();
            }
            if (inStream != null) {
                inStream.close();
            }
        } catch (IOException ex) {
            LOGGER.error("创建PDF异常" + ex);

        }
    }
    return url;
}

调用方法:

String content = JavaToPdfHtmlFreeMarker.freeMarkerRender(objectMap, "print.html",
        request.getSession().getServletContext().getRealPath("/WEB-INF/classes/conf") + File.separator + "htmlTemplate");
url = JavaToPdfHtmlFreeMarker.createPdf(content, "print.pdf", LOG_LSCU);

猜你喜欢

转载自blog.csdn.net/xu84557120/article/details/86537475