Velocity模板语言导出PDF
引入依赖
pom文件添加
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
Spring配置文件中添加velocity的配置文件(yml注意层级)
spring:
velocity:
charset: utf-8
resourceLoaderPath: classpath:/template/
properties:
input:
encoding : utf-8
output:
encoding : utf-8
suffix: .vm
地址根据模板存放路径去填
Velocity模板service类
import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.stereotype.Service;
import java.io.StringWriter;
import java.util.Map;
@Service
@Slf4j
public class VelocityService {
private final VelocityEngine velocity;
public VelocityService(VelocityEngine velocity) {
this.velocity = velocity;
}
/**
* 获取velocity模板
*/
public String getVelocityContent(String template, Map<String, Object> args) {
try {
Template vt = velocity.getTemplate(template,"UTF-8");
VelocityContext ctx = new VelocityContext();
for (Map.Entry<String, Object> e : args.entrySet()) {
ctx.put(e.getKey(), e.getValue());
}
StringWriter writer = new StringWriter();
vt.merge(ctx, writer);
return writer.toString();
} catch (Exception e) {
log.error("", e);
return null;
}
}
}
使用velocity生成pdf的主要代码
String template = velocityService.getVelocityContent
("template/pxqk_template1.vm", maps);
ITextRenderer render = new ITextRenderer();
render.setDocumentFromString(template);
ITextFontResolver fontResolver = render.getFontResolver();
fontResolver.addFont(FONTS_PATH + "simsun.ttc", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
-- addFont的入参为字体文件所在地址
render.layout();
ByteArrayOutputStream pdfout = new ByteArrayOutputStream();
render.createPDF(pdfout);
此处方法第一个入参为模板存放位置,第二个入参为map<String,object>类型的数据集
获得输出流之后,可以翻翻所在项目是否有封装好的方法(处理流实现下载)
如果没有的话可以引入一个
Java工具类库 Hutool
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>{版本号}</version>
</dependency>
ServletUtil.write(response, new ByteArrayInputStream(pdfout.toByteArray()));
以下代码为模板vm文件正常按照html语言写就可以其参数用${}填充
参数为上文中提到的map<String,object>数据提供
支持foreach语法可以循环增加标签
常用语法建议百度网上很多,常用语法掌握即可
Velocity 常用语法
pxqk_template1.vm文件
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no"/>
<title></title>
<style>
body {margin: 0px; padding:0; border: 0;outline: none; list-style: none;font-family:SimSun;}
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
}
hr {
width: 80%;
height: 3px;
background-color: red;
border: none;
}
.table {
width: 80%;
}
</style>
</head>
<body>
<h1>学 员 培 训 档 案</h1>
<hr style="margin-bottom: 50px"/>
<h2>学员基本信息</h2>
<table class="table" border="1" cellspacing="0" cellpadding="0" align="center" style="margin-bottom: 50px">
<colgroup>
<col width="10%"/>
<col width="20%"/>
<col width="10%"/>
<col width="20%"/>
<col width="10%"/>
<col/>
</colgroup>
<tr>
<th>姓名</th>
<th>${name}</th>
<th>身份证</th>
<th>${idCard}</th>
<th>手机号</th>
<th>${phoneNumber}</th>
</tr>
<tr>
<th>培训机构</th>
<th>${orgName}</th>
<th>班次</th>
<th>${classRoomName}</th>
<th>项目</th>
<th>${projectName}</th>
</tr>
<tr>
<th>实际获得学时</th>
<th>${sumRealStudyTime}</th>
<th>学时指标</th>
<th>${studyTimeKpi}</th>
<th>达成率</th>
<th>${reachRate}</th>
</tr>
</table>
<h2>课程明细</h2>
<table class="table" border="1" cellspacing="0" cellpadding="0" align="center">
<colgroup>
<col width="15%"/>
<col width="15%"/>
<col width="15%"/>
<col width="15%"/>
<col width="10%"/>
<col width="10%"/>
<col width="10%"/>
</colgroup>
<tr>
<th>课程名称</th>
<th>章节名称</th>
<th>打卡时间</th>
<th>培训照片</th>
<th>培训地点</th>
<th>实际学时</th>
<th>类型</th>
</tr>
#foreach($conversionRate in $conversionRateList)
<tr>
<th>${conversionRate.courseName}</th>
<th>${conversionRate.title}</th>
<th>${conversionRate.takePictureTime}</th>
<th><img src="${conversionRate.imgOssUrl}" width="100px" alt=""/></th>
<th>${conversionRate.gpsAddress}</th>
<th>${conversionRate.realStudyTime}</th>
<th>${conversionRate.typeName}</th>
</tr>
#end
</table>
</body>
</html>