HTML转PDF(Thymeleaf做模板)

本文档记录了使用SpringBoot+Thymeleaf搭建环境,通过flying-saucer库生成PDF时遇到的表格分页边框问题。解决方案在于CSS设置,避免table分页内部分割,并提供了页眉、页脚的定制方法。同时分享了字体文件下载链接,以确保PDF中中文的正常显示。
摘要由CSDN通过智能技术生成

工作中经常碰到一些可能需要导出pdf的需求,因此在这里记录一下用thymeleaf做模板导出的知识点。(每天进步一点😁)

项目搭建 springboot + thymeleaf
maven环境如下

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
   </dependency>
   <dependency>
            <groupId>org.xhtmlrenderer</groupId>
            <artifactId>flying-saucer-pdf</artifactId>
            <version>9.1.20</version>
    </dependency>

properties属性设置如下

spring:
  thymeleaf:
    cache: false
    mode: HTML
    prefix: classpath:/templates/
    suffix: .html
    encoding: UTF-8
    check-template-location: true

接下来就需要在templates下准备模板了

准备一个工具类

public static void generatorPdf(String content, OutputStream os) throws Exception{
        ITextRenderer renderer = new ITextRenderer();

        ITextFontResolver fontResolver = renderer.getFontResolver();
        fontResolver.addFont("/simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        renderer.setDocumentFromString(content);
        renderer.layout();
        renderer.createPDF(os);
        renderer.finishPDF();
    }

接下来就是就是解析并生成了

@RunWith(SpringRunner.class)
@SpringBootTest
public class ThymeleafTest {

    @Autowired
    private SpringTemplateEngine templateEngine;

    @Test
    public void parseDemoDataTemplateInfo() throws Exception {
        final Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("orderNum", "XH001001");
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (int i = 0; i < 40; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", "AA");
            map.put("age", "20");
            map.put("className", "三年级二班");
            mapList.add(map);
        }
        dataMap.put("stuList", mapList);
        //输出html脚本
        //Writer writer = new FileWriter("C:\\Users\\mike\\Desktop\\aDemo.html");
        //封装
        Context context = new Context(Locale.getDefault(), dataMap);
        //解析模板
        //this.templateEngine.process("aDemo.html", context, writer);
        String hml = this.templateEngine.process("aDemo.html", context);
        //pdf生成路径
        FileOutputStream fos = new FileOutputStream("F:\\aDemo.pdf");
        //开始生成
        PDFUtils.generatorPdf(hml, fos);
        System.out.println("=======解析完毕=======");

    }
}

很显然,存在在分页里面表格被分割的问题...
这是因为在table上设置border所导致

直接说解决办法吧,这种情况
首先不能设置border="0" 后面两个属性不能去掉,然后在css里面去设置

@page  {
            size: 210mm 297mm;
            margin: 15mm 5mm 25mm 5mm;
            padding: 1em 0.5em;
            @bottom-center {
                content: "good good study, day day up";
                font-family: SimSun;
                font-size: 14px;
                color:black;
            };
            @top-center {
                margin-bottom: 1.5in;
                content: element(header);  /*定义页眉元素*/
            }

            @bottom-right{
                margin-top: 1.5in;
                content:"第" counter(page) "页  共 " counter(pages) "页";
                font-family: SimSun;
                font-size: 14px;
                color:#000;
            };
        }
        body{
            margin: 0 0;
            padding: 0 0;
            width: 100%;
            height: 100%;
            font-family: SimSun;  /*字体样式不能忘记,否则转换pdf,中文可能无法显示*/
        }

        /*页眉样式*/
        .header {
            width: 100%;
            margin-bottom: 3%;
            position: running(header);  /*自定义页眉必不可少*/
        }

        .header .headerLeft {
            display: inline-block;
            width: 50%;
        }

        .header .headerRight {
            display: inline-block;
            width: 50%;
            text-align: right;
            height: 50px;
            line-height: 50px;
        }

        .headerTitle {
            display: inline-block;
            height: 50px;
            font-size: 12px;
            text-align: center;
            position: absolute;
            left: 60px;
            top: 20px;
        }

        .headerTitle span {
            display: block;
        }

        .headerImg {
            vertical-align: middle;
            display: inline-block;
        }
        
        /*解决table tr td分页边框问题*/
        table tr {
            page-break-inside: avoid;
        }

        td {
            border: 1px solid #333;
        }

以上就是这次写的过程中碰到的一些问题。。。就说到这里吧。
最后把字体文件上传一下,避免以后自己或者大家会用到。
链接:https://pan.baidu.com/s/1KvDRYT9in6rqlw8cxVSVfQ
提取码:n8pw

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值