Spring Boot Freemark HTML 生成 PDF、生成水印Logo、docx文件生成PDF,Jar包运行可读取模板文件、字体文件

用于通过模板生成PDF,在项目中生成个人授权协议函、个人电子保单、流水报表,数据报表等,将HTML静态模板写出来后,将数据替换成动态数据即可。

<!-- html2pdf -->
<dependency>
    <groupId>org.xhtmlrenderer</groupId>
    <artifactId>flying-saucer-pdf</artifactId>
    <version>9.1.16</version>
</dependency>

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13</version>
</dependency>

<!-- freemarker -->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.31</version>
</dependency>

<!-- docx4j docx2pdf -->
<dependency>
   <groupId>org.docx4j</groupId>
   <artifactId>docx4j</artifactId>
   <version>6.1.2</version>
   <exclusions>
      <exclusion>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      </exclusion>
   </exclusions>
</dependency>
<dependency>
   <groupId>org.docx4j</groupId>
   <artifactId>docx4j-export-fo</artifactId>
   <version>6.0.0</version>
</dependency>

 templates.ftl 放入resource/templates文件目录中

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Title</title>
    <style>
        @page {
            size:  210mm 290mm; /* size: 210mm 297mm;  设置纸张大小:A4(210mm 297mm)、A3(297mm 420mm) 横向则反过来 */
            /*margin-bottom: 1cm;*/
            /*padding: 1em;*/

            /*页眉*/
            /*@top-center {*/
            /*    content: counter(page);*/
            /*    font-family: SimSun;*/
            /*    font-size: 15px;*/
            /*    color:#000;*/
            /*};*/
            /*@top-right{*/
            /*    background: url("https://dpxdata.oss-cn-beijing.aliyuncs.com/upload/kbt/null_1660715685801.png") top right no-repeat;*/
            /*    width: 195px;height: 50px;*/
            /*    content: "logo";*/
            /*    font-family: SimSun;*/
            /*    font-size: 15px;*/
            /*    !*color:#000;*!*/
            /*};*/

            /*页脚*/
            @bottom-center{
                content: counter(page);
                font-family: SimSun;
                font-size: 15px;
                color:#000;
            };

            /*@bottom-right{*/
            /*    content: "第" counter(page) "页 共" counter(pages) "页";*/
            /*    font-family: SimSun;*/
            /*    font-size: 15px;*/
            /*    color:#000;*/
            /*};*/
        }
        * {
            page-break-inside: always;
        }
    </style>
</head>

<body style="font-family: SimSun; font-size: 14px">
<div style="">
    ${text!''}
</div>

</body>
</html>
simsun.ttc 为字体文件

1.HTML 生成 PDF

HttpHeaders 写法

@GetMapping("/pdf")
    public ResponseEntity<?> export(HttpServletResponse response) {
        try {
            HttpHeaders headers = new HttpHeaders();
            Map<String, Object> dataMap = new HashMap<>(16);
            dataMap.put("text", "张三");
            String htmlStr = FreeMarkUtils.freemarkerRender(dataMap, "templates.ftl");
            byte[] pdfBytes = FreeMarkUtils.createPDF(htmlStr, "simsun.ttc");
            if (pdfBytes != null && pdfBytes.length > 0) {
                String fileName = System.currentTimeMillis() + (int) (Math.random() * 90000 + 10000) + ".pdf";
                headers.setContentDispositionFormData("attachment", fileName);
                headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
                return new ResponseEntity<byte[]>(pdfBytes, headers, HttpStatus.OK);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        return new ResponseEntity<>("{ \"code\" : \"404\", \"message\" : \"not found\" }", headers, HttpStatus.NOT_FOUND);
    }

 Response 写法

    @GetMapping("/pdf")
    public void export(HttpServletResponse response) {
  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值