<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<title>模板</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
font-family: SimSun;--设置字体-解决中文乱码
font-size: 18px;
color: #000;
}
@page {
size: landscape; --纸张横向展示
}
form,
img,
table,
td {
border: 0;
}
h1,
h2,
h3,
h4,
h5,
h6 {
font-size: 100%;
font-weight: normal;
}
input,
textarea {
font-family: SimSun, Arial;--中文乱码
font-size: 18px;
}
.clear {
clear: both;
height: 0px;
font-size: 0px;
visibility: hidden;
line-height: 0px;
}
.none {
display: none;
}
/*--------------head---------------*/
.page {
width: 900px;
margin: 0 auto;
overflow: hidden;
position: relative;
}
.box {
position: relative;
overflow: hidden;
width: 100%;
height: 100%;
}
input {
font-size: 18px;
border: none;
background: none;
font-family: "微软雅黑";
outline: none;
width: 360px;
}
.text {
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
}
li {
height: 16px;
line-height: 16px;
position: absolute;
font-size: 15px;
}
li span {
float: left;
text-align: center;
}
.li1 {
width: 123px;
height: 30px;
line-height: 30px;
overflow: hidden;
left: 114px;
top: 61px;
font-size: 24px;
}
.li2 {
width: 293px;
height: 14px;
line-height: 14px;
overflow: hidden;
top: 109px;
left: 566px;
font-size: 12px;
}
.li3 {
width: 360px;
overflow: hidden;
left: 175px;
top: 150px;
}
.li4 {
width: 193px;
overflow: hidden;
left: 144px;
top: 181px;
}
.li5 {
width: 62px;
overflow: hidden;
left: 400px;
top: 308px;
}
.li6 {
width: 160px;
overflow: hidden;
left: 671px;
top: 373px;
font-size: 12px;
line-height: 13px;
height: 13px;
}
.li7 {
width: 47px;
overflow: hidden;
left: 665px;
top: 437px;
transform: scale(.7);
color: #999;
font-size: 12px;
}
</style>
</head>
<body>
<div class="page ">
<div class="box">
<ul class="text">
<li class="li1">${areaName}</li>
<li class="li2">
<span style="width: 62px; margin-right: 62px;">${areaName}</span>
<span style="width: 37px; margin-right: 14px;">${year}</span>
<span style="width: 34px;">${num}</span>
</li>
<li class="li3">
<span style="width: 53px; margin-right: 15px;">${year}</span>
<span style="width:32px; margin-right:14px">${month}</span>
<span style="width: 32px; margin-right: 60px;">${date}</span>
<span style="width: 61px;">${areaName}</span>
</li>
<li class="li4">${orgName}</li>
<li class="li5">${areaName}</li>
<li class="li6">
<span style="width: 30px; margin-right: 12px;">${nowYear}</span>
<span style="width: 26px; margin-right: 10px;">${nowMonth}</span>
<span style="width: 27px; ">${nowDate}</span>
</li>
<li class="li7">${areaName}</li>
</ul>
<#--<img style="width: 100%" src="file:///C:\GitKu\gmall\src\main\webapp\WEB-INF\ftl\image\bg.png" class="bg"/>-->
<img style="width: 100%" src="${imgPath}" class="bg"/>--背景图设置-前缀file:///
</div>
</div>
</body>
</html>
Map map = new HashMap();
int num1 = 9;
String num = String.format("%05d", num1);
map.put("num", num);
map.put("date", DateUtil.format(micRecord.getUpdateTime(), "dd"));
map.put("month", DateUtil.format(micRecord.getUpdateTime(), "MM"));
map.put("year",DateUtil.format(micRecord.getUpdateTime(), "yyyy"));
map.put("areaName", "北京");
map.put("orgName", micOrgInfo.getOrgName());
Calendar now = Calendar.getInstance();
int currentMonth=now.get(Calendar.MONTH) + 1;
String currMonth = String.format("%02d", currentMonth);
int currentDay=now.get(Calendar.DAY_OF_MONTH);
String currDay= String.format("%02d", currentDay);
map.put("nowYear", now.get(Calendar.YEAR)+"");
map.put("nowMonth", currMonth);
map.put("nowDate", currDay);
String basePath =request.getSession().getServletContext().getRealPath("/");
String imgPath="file:///"+basePath + "/WEB-INF/ftl/image/bg.png";
//模板名称
map.put("imgPath",imgPath);
String fileName = "XXX_测试模板.pdf";
String ftlName = "test.ftl";
ByteArrayOutputStream baos = PDFUtil.createPDF(request, ftlName,map);//创建pdf,使用map填充页面数据
---将流输出到文件,保存pdf文件到指定目录
String saveFileName="d:/pdfFile/"+System.currentTimeMillis()+".pdf";
FileOutputStream fos = new FileOutputStream(saveFileName);
baos.writeTo(fos);
baos.flush();
baos.close();
fos.close();
/**
* 创建pdf 不写入服务器只返回字节流
* @param request 请求对象
* @param ftlName 模板名称
* @param root 给模板上赋值的对象,在模板上取值 通过key值取value..
* @return
* @throws Exception
*/
public static ByteArrayOutputStream createPDF(HttpServletRequest request, String ftlName,Map root) throws Exception {
String basePath =request.getSession().getServletContext().getRealPath("/");//绝对路径
String basePath2=basePath.replaceAll("\\\\", "/");
Configuration cfg = new Configuration();
try {
cfg.setLocale(Locale.CHINA);
cfg.setEncoding(Locale.CHINA, "UTF-8");
//设置编码
cfg.setDefaultEncoding("UTF-8");
//设置模板路径
cfg.setDirectoryForTemplateLoading(new File(basePath + "/WEB-INF/ftl/"));
//获取模板
Template template = cfg.getTemplate(ftlName);
template.setEncoding("UTF-8");
Writer writer = new StringWriter();
//数据填充模板
template.process(root, writer);
String str = writer.toString();
String code = getEncoding(str);
//pdf生成
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
ITextRenderer iTextRenderer = new ITextRenderer();
String data = new String(str.getBytes(),Charset.forName(code));
iTextRenderer.setDocumentFromString(data);
//设置字体 其他字体需要添加字体库
ITextFontResolver fontResolver = iTextRenderer.getFontResolver();
fontResolver.addFont(basePath + "/res/fonts/simsun.ttc", BaseFont.IDENTITY_H, false);
iTextRenderer.setDocument(builder.parse(new ByteArrayInputStream(str.getBytes("UTF-8"))),null);
iTextRenderer.layout();
//生成PDF
ByteArrayOutputStream baos = new ByteArrayOutputStream();
iTextRenderer.createPDF(baos);
baos.close();
return baos;
} catch(Exception e) {
throw new Exception(e);
}
}
---将流输出到文件,保存pdf文件到指定目录
String saveFileName="d:/pdfFile/"+System.currentTimeMillis()+".pdf";
FileOutputStream fos = new FileOutputStream(saveFileName);
out.writeTo(fos);
out.flush();
out.close();
fos.close();