freemarker 生成pdf

1 导入依赖

<!-- freemarker 读取html模板文件 -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>
        <!-- xml 将html模板文件转换成pdf -->
        <dependency>
            <groupId>org.xhtmlrenderer</groupId>
            <artifactId>flying-saucer-pdf</artifactId>
            <version>9.1.16</version>
        </dependency>

2 编写test.ftl文件

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title></title>
    <style type="text/css">
    body {
    font-family: FangSong;
    }
    </style>
</head>
<body>
<p>${name}:</p>
<p style="text-indent: 2em">哈哈哈</p>
</body>
</html>

3  PdfController

@ApiOperation("pdf生成")
    @PostMapping(value = "pdfCreate")
    public void pdfCreate(@RequestBody Map map) throws IOException {
        pdfService.pdfCreate(map);
    }

4  PdfServiceImpl

@Override
    public void pdfCreate(Map map) throws IOException {
        PdfUtil.pdfCreate(map,"test.ftl");
    }

5  PdfUtil 工具类

public class PdfUtil {
    public static void pdfCreate(Map map,String templateFtlName) throws IOException {
        // 创建一个FreeMarker实例, 负责管理FreeMarker模板的Configuration实例
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        // 指定FreeMarker模板文件的位置
        configuration.setClassForTemplateLoading(PdfUtil.class, "/template");
        ITextRenderer renderer = new ITextRenderer();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        StringWriter writer = new StringWriter();
        String fileName = String.valueOf(System.currentTimeMillis()) + ".pdf";
        File file = new File("src/main/resources/template/result/" + fileName);
        OutputStream outputStream = new FileOutputStream(file);

        try {
            // 设置 css中 的字体样式(暂时仅支持宋体和黑体) 必须,不然中文不显示
            ITextFontResolver fontResolver = renderer.getFontResolver();
            fontResolver.addFont("src/main/resources/template/font/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            fontResolver.addFont("src/main/resources/template/font/SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            fontResolver.addFont("src/main/resources/template/font/MSYH.TTC", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            fontResolver.addFont("src/main/resources/template/font/MSYHBD.TTC", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            fontResolver.addFont("src/main/resources/template/font/MSYHL.TTC", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            fontResolver.addFont("src/main/resources/template/font/SIMLI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            // 设置模板的编码格式
            configuration.setEncoding(Locale.CHINA, "UTF-8");
            // 获取模板文件
            Template template = configuration.getTemplate(templateFtlName, "UTF-8");
            // 将数据输出到html中
            template.process(map, writer);
            writer.flush();
            String html = writer.toString();
            // 把html代码传入渲染器中
            renderer.setDocumentFromString(html);

            // 设置模板中的图片路径 (这里的images在resources目录下) 模板中img标签src路径需要相对路径加图片名 如<img src="images/xh.jpg"/>
//            String url = PdfUtil.class.getClassLoader().getResource("static/images").toURI().toString();
//            renderer.getSharedContext().setBaseURL(url);
            renderer.layout();
            renderer.createPDF(out, false);
            out.flush();
            renderer.finishPDF();
            out.writeTo(outputStream);
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}

 

posted @ 2021-01-13 15:09  行智障  阅读(1644)  评论(0)    收藏  举报