Springboot Thymeleaf Html转Pdf

新建项目

说明

用itextpdf写pdf,样式实在是太折磨了,这里选用Thymeleaf模板生成html转pdf,html+css写样式排版好太多了。

引入依赖

<!--html转pdf-->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>html2pdf</artifactId>
    <version>4.0.2</version>
</dependency>

配置Thymeleaf

application.yml
spring:
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML
    encoding: UTF-8

创建字体目录以及Pdf模板

创建以下文件

src/main/resources/font/cn.ttf *(cn.ttf必须是中文字体)
src/main/resources/templates/pdf.html

pdf.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
    <style>
        body {
            font-family: "Microsoft YaHei UI";
        }
    </style>
</head>
<body>
<h1>检测报告</h1>
<div>
    <p>
        <span>ID:</span>
        <span>007</span>
    </p>
    <p>
        <span>来源:</span>
        <span th:text="${sys}"></span>
    </p>
    <table border="1">
        <tr style="background-color: #f6f6f6">
            <th>id</th>
            <th>name</th>
            <th>age</th>
        </tr>
        <tr>
            <td>1</td>
            <td>tom</td>
            <td>20</td>
        </tr>
    </table>
</div>
</body>
</html>

创建Controller

TestControlle
package top.yxlhsx.thymeleafdemo;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.io.font.FontProgram;
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.layout.font.FontProvider;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/test")
public class TestController {
	@Resource
    private TemplateEngine templateEngine;

    @GetMapping("pdf")
    public void importPdf() throws IOException{
        String outputFolder = "C:\\Users\\你的用户名\\Desktop\\新建文件夹\\test.pdf";
        OutputStream outputStream = Files.newOutputStream(Paths.get(outputFolder));

        Context context = new Context();
        context.setVariable("sys", "linux");

        String htmlStr = templateEngine.process("pdf", context);

        ConverterProperties properties = new ConverterProperties();
        FontProvider fontProvider = new DefaultFontProvider();
        FontProgram fontProgram = FontProgramFactory.createFont("font/cn.ttf");
        fontProvider.addFont(fontProgram);
        properties.setFontProvider(fontProvider);

        HtmlConverter.convertToPdf(htmlStr, outputStream, properties);
    }

}

注意

如果生成Html字符串时报 【java.lang.ClassNotFoundException: ognl.DefaultMemberAccess】 异常
<!--添加这个依赖即可-->
<!--而且最好是3.1.x版本,不然也报错-->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>html2pdf</artifactId>
    <version>4.0.2</version>
</dependency>

Pdf效果

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值