描述:最近在工作过程中发现需要生成pdf,在网上不断地摸索发现可以通过html文件转换成pdf文件。特此在这边记录下。
create table student
(id int not null auto_increment primary key ,
name varchar(20) null ,
age int null ,
sex varchar(20) null ,
address varchar(20) null );
insert into student(name,age,sex,address) values
('张三',18,'男','湖南'),
('李四',19,'女','湖北'),
('王五',18,'男','广东'),
('赵六',19,'女','广西');
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.6.7version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
<version>2.6.7version>
dependency>
<dependency>
<groupId>org.xhtmlrenderergroupId>
<artifactId>flying-saucer-pdfartifactId>
<version>9.0.7version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.24version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<version>2.6.7version>
dependency>
dependencies>
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username: root
password: 123456
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML
encoding: UTF-8
servlet:
content-type: text/html
cache: false
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class HtmlToPdf {
public static void toPdf(String content, String path) throws DocumentException, FileNotFoundException {
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
try {
//设置字体,否则不支持中文,在html中使用字体,html{ font-family: SimSun;}
fontResolver.addFont("templates/SimSun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
} catch (IOException e) {
e.printStackTrace();
}
renderer.setDocumentFromString(content);
renderer.layout();
renderer.createPDF(new FileOutputStream(new File(path)));
}
}
DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.w3.org/1999/xhtml" layout:decorator="layout">
<head lang="en">
<title>Spring Boot Demo - PDFtitle>
<style>
@page {
size: 420mm 297mm; /*设置纸张大小:A4(210mm 297mm)、A3(297mm 420mm) 横向则反过来*/
margin: 0.25in;
padding: 1em;
@bottom-center{
content:"版权所有";
font-family: SimSun;
font-size: 12px;
color:red;
};
@top-center { content: element(header) };
@bottom-right{
content:"第" counter(page) "页 共 " counter(pages) "页";
font-family: SimSun;
font-size: 12px;
color:#000;
};
}
body{font-family: 'SimSun'}
td, th {
font-style: normal;
font-weight: normal;
text-align: center;
}
tr {
height: 40px;
}
.twoHead th {
width: 6.25%;
height: 40px;
padding: 0 10px;
font-size: 14px;
font-weight: normal;
}
table {
border: none;
border-collapse: collapse;
border-color: #D8DFE6;
}
table thead {
background: #F3FDFF;
}
style>
head>
<body style="font-family: 'SimSun'">
<div class="table">
<table border="1" cellspacing="0" cellpadding="10" width="100%">
<thead>
<tr class="twoHead">
<th>IDth>
<th>名称th>
<th>年龄th>
<th>性别th>
<th>籍贯th>
tr>
thead>
<tbody>
<tr th:each="demo : ${demoList}">
<td th:text="${demo.id}">td>
<td th:text="${demo.name}">td>
<td th:text="${demo.age}">td>
<td th:text="${demo.sex}">td>
<td th:text="${demo.address}">td>
tr>
tbody>
table>
div>
body>
html>
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer id;
private String name;
private Integer age;
private String sex;
private String address;
}
@Mapper
@Repository
public interface StudentDao extends BaseMapper<Student> {
}
public interface StudentService {
List<Student> findAll();
}
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public List<Student> findAll() {
return studentDao.selectList(null);
}
}
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@Autowired
private TemplateEngine templateEngine;
@GetMapping("/index")
public String index(HttpServletRequest request, HttpServletResponse response){
WebContext context = new WebContext(request,response, request.getServletContext(),request.getLocale());
List<Student> studentList = studentService.findAll();
System.out.println(studentList);
context.setVariable("demoList",studentList);
try {
String htmlContext = templateEngine.process("/pdfPage", context);
HtmlToPdf.toPdf(htmlContext,"D:/student.pdf");
} catch (Exception e) {
e.printStackTrace();
}
request.setAttribute("time", new Date());
return "/pdfPage";
}
}
生成的PDF文件: