目录
freemarker官方地址:
项目
maven
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.freemarker</groupId>
<artifactId>freemarkerdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.13.2</version>
</dependency>
</dependencies>
</project>
freemarker模版
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>test</title>
<style type="text/css">
table {
font-family: "simsun";
width: 100%;
border-collapse: collapse;
}
td, th {
font-size: 1em;
border: 1px solid #5B4A42;
padding: 3px 7px 2px 7px;
}
th {
font-size: 1.1em;
text-align: center;
padding-top: 5px;
padding-bottom: 4px;
background-color: #24A9E1;
color: #ffffff;
}
</style>
</head>
<body>
<div>
<table id="customers">
<tr>
<th>MessageCode</th>
<th>MessageStatus</th>
<th>Cause</th>
</tr>
<tr>
<td>${messageCode}</td>
<td>${messageStatus}</td>
<td>${cause}</td>
</tr>
</table>
</div>
</body>
</html>
代码
public void freeMarkerDemo() throws Exception {
Map<String, String> hashMap = new HashMap<>();
hashMap.put("messageCode","消息");
hashMap.put("messageStatus","状态");
hashMap.put("cause","cause");
Template template = freeMarkerConfigurer.getConfiguration().getTemplate("freemarker.ftl");
String freeStr = FreeMarkerTemplateUtils.processTemplateIntoString(template, hashMap);
Document documentText = new Document();
String s = "D:\\t.pdf";
File file = new File(s);
if(!file.exists()){
file.createNewFile();
}
PdfWriter pdfWriter = PdfWriter.getInstance(documentText,new FileOutputStream(file));
documentText.open();
//设置中文
XMLWorkerFontProvider fontProvider=new XMLWorkerFontProvider();
fontProvider.register(new ClassPathResource("templates/simsun.ttc").getPath());
XMLWorkerHelper.getInstance()
.parseXHtml(pdfWriter,documentText,new ByteArrayInputStream(freeStr.getBytes(StandardCharsets.UTF_8)),null,StandardCharsets.UTF_8, fontProvider);
documentText.close();
}