美文网首页
HTML转PDF(Thymeleaf做模板)

HTML转PDF(Thymeleaf做模板)

作者: 有梦想的农民 | 来源:发表于2020-07-27 19:58 被阅读0次

工作中经常碰到一些可能需要导出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("=======解析完毕=======");

    }
}
image.png

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

image.png

直接说解决办法吧,这种情况
首先不能设置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

相关文章

网友评论

      本文标题:HTML转PDF(Thymeleaf做模板)

      本文链接:https://www.haomeiwen.com/subject/hextrktx.html