创建字体目录以及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">
    <meta charset="UTF-8"/>
    <title>Title</title>
    <style>
        body {
            font-family: "Microsoft YaHei UI";
    </style>
</head>
<h1>检测报告</h1>
        <span>ID:</span>
        <span>007</span>
        <span>来源:</span>
        <span th:text="${sys}"></span>
    <table border="1">
        <tr style="background-color: #f6f6f6">
            <th>id</th>
            <th>name</th>
            <th>age</th>
            <td>1</td>
            <td>tom</td>
            <td>20</td>
    </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效果

Thymeleaf是一个Java库。它是一个XML / XHTML / HTML5模板引擎,能够应用于换模板文件,以显示您的应用程序产生的数据和文本。 它尤其适合于基于XHTML / HTML5的web服务应用程序,同时它可以处理任何XML文件,作为web或独立的应用程序。 Thymeleaf的主要目的是提供一个优雅和格式良好的方式创建模板。为了实现这一目标,它把预定义的逻辑放在XML的标记和属性上,而不是显式放在XML标记的内容上。 依靠智能缓存去解析文件,致使其执行期间的I / O操作达到了最少数量,因此其处理的模板的能力实非常快速的。
工具类(注意字体路径,如果没有字体到他的Gitee上面下载) import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.xml.xmp.XmpWriter; import lombok.extern.slf4j.Slf4j; import org.thymeleaf.Templat When allowCredentials is true, allowedOrigins cannot contain the special value “*“ since that cannot
CSDN-Ada助手: 恭喜作者成功发布第14篇博客,并分享了关于Element Plus全局修改组件默认属性的知识。阅读了您的文章,我对这个主题有了更深入的了解。希望您能继续保持创作热情,分享更多优质的技术文章。下一步的创作建议是可以探讨一下如何在Vue项目中更好地使用Element Plus组件。期待您的下一篇文章。 CSDN 会根据你创作的博客的质量,给予优秀的博主博客红包奖励。请关注 https://bbs.csdn.net/forums/csdnnews?typeId=116148&utm_source=csdn_ai_ada_blog_reply14 看奖励名单。 MybatisPlus 查询条件为空字符串或null问题 老炒肉配烧饼儿: .isnull() 这个条件 Springboot Thymeleaf Html转Pdf 想望着太阳: 不是中文字体的话中文显示不出来的 Springboot Thymeleaf Html转Pdf qq_53782518: src/main/resources/font/cn.ttf *(cn.ttf必须是中文字体),是什么意思, MybatisPlus 查询条件为空字符串或null问题 一直向前看: 不对啊,不拼接不就少了一个为空的条件吗