SpringBoot使用Thymeleaf生成PDF
SpringBoot使用Thymeleaf生成PDF
您可能不了解SpringBoot使用Thymeleaf生成PDF的相关话题,接下来IT人网小编为大家介绍。
在现代Web应用中,生成PDF文件是一个常见的需求。
为了满足这一需求,我们可以利用Spring Boot集成Thymeleaf和Flying Saucer PDF来生成具有丰富内容的PDF文件。
Thymeleaf作为模板引擎,提供了简单而强大的模板语法,而Flying Saucer PDF则是一个用于将HTML转换为PDF的工具
1. 选择Thymeleaf和Flying Saucer的原因
Thymeleaf的强大模板引擎
Thymeleaf是一款为HTML和XML文档提供自然模板语法的模板引擎。
它的语法清晰简单,易于学习和使用,同时支持强大的逻辑操作。与Spring Boot紧密集成,使得在Spring Boot应用中使用Thymeleaf非常方便。
Flying Saucer PDF的HTML转PDF能力
Flying Saucer PDF是一个用于将HTML和CSS转换为PDF的Java库。它支持CSS2.1和部分CSS3,因此我们可以使用Thymeleaf生成的HTML作为输入,从而实现更丰富的PDF内容
2. 代码实现步骤
完整项目结构如下:
【步骤一】:在pom.xml中添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.7.15</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-generate-pdf-thymeleaf</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- Spring Boot Starter Thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Flying Saucer PDF -->
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.3.1</version>
</dependency>
</dependencies>
</project>
【步骤二】:在yml中添加thymeleaf配置
相关阅读
-
excel里面的自动换算 excel怎么自动换算单位
小编为你讲解excel怎么自动换算单位方面的讲解,excel自动换算单位可以通过其他函数公式完成。下面就为大家带来具体的操作步骤:
-
word打印预览在哪里 word2010的打印预览在哪里
关于这方面的知识你知道吗?word打印预览在哪里方面的介绍,word打印预览在哪里需两个步骤,先进入需要打印预览的文档,再打印预览。具体操作步骤如下:
-
excel表格文字怎么换行 excel办公软件基础知识
这些方法你知道吗?excel表格文字怎么换行和excel办公软件基础知识的相关话题,下面来一起了解一下吧。 第一步:选中要换行的单元格,双击单元格,确认光标在单元格内要换行的位置。 第
-
excel页面设置灰色 excel功能栏灰色怎么办
如果想了解excel功能栏灰色怎么办的IT小经验,关于excel功能栏灰色怎么办 跟 excel页面设置灰色,接下来IT人带大家一起了解。 在使用excel时遇到了工具栏是灰色的怎么办呢?表格的工具栏不能用