1.带页眉页脚及list列表
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
<style>
@page{size:297mm 210mm;
/* 页眉
@top-center {content: element(header); }
*/
/* 页脚加页码 样例: 1/8
@bottom-center {
content:counter(page) " / " counter(pages);
font-family: Arial Unicode MS;
}
*/
}
body{
font-family:"STFangsong";
font-size:18px;
line-height: 26px;
}
p{ text-indent:2em;}
td{line-height:26px;}
/* 页眉样式
div.header {
font-size: 13px;
display: block;
text-align: right;
border-bottom: solid 1px black;
position: running(header);
}
*/
</style>
</head>
<body>
<table>
<tr>
<td style="width:30%"><div align="center"><img src="logo.png"></img></div></td>
<td align="center"><div class="STYLE1" align="center">测试生成pdf</div></td>
</tr>
</table>
<table width="100%" border="0">
<tr>
<td>字段0:${field0}</td>
</tr>
<tr>
<td>字段2:${field2}</td>
</tr>
<tr>
<td>字段4:${field4}</td>
</tr>
<tr>
<td>字段5:${field5}</td>
</tr>
</table>
<hr style="height:1px; border:1; border-top:1px solid;" ></hr>
<table width="100%" border="0" style="table-layout: fixed;">
<tr>
<td width="18%"><div align="center">字段0</div></td>
<td width="25%"><div align="center">字段1</div></td>
<td width="12%"><div align="center">字段2</div></td>
<td width="15%"><div align="center">字段3</div></td>
<td width="10%"><div align="center">字段4</div></td>
<td width="20%"><div align="center">字段5</div></td>
</tr>
<#list mylist as item>
<tr>
<td>${item.field0}</td>
<td>${item.field1}</td>
<td><div align="right">${item.field2}</div></td>
<td><div align="center">${item.field3}</div></td>
<td>${item.field4}</td>
<td><div align="left">${item.field5}</div></td>
</tr>
</#list>
</table>
</body>
</html>
2.java 代码
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfPageEvent;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import freemarker.template.Configuration;
import freemarker.template.Template;
public static String getHtmlString(Template template, Map<String, Object> data) {
Writer out = new StringWriter();
try {
template.process(data, out);
out.flush();
return out.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
}
public static Template getHtmlTemplate(String htmlDir, String htmlName) throws Exception {
File templateFile = new File(htmlDir);
if (!templateFile.exists()) {
return null;
}
Configuration freemarkerCfg = new Configuration();
freemarkerCfg.setDirectoryForTemplateLoading(templateFile);
Template template = freemarkerCfg.getTemplate(htmlName);
template.setEncoding("UTF-8");
return template;
}
public static void xxwDzd() throws Exception{
Map<String, Object> data = new HashMap<String, Object>();
data.put("field0", "adfasdf");
data.put("field1", "asdfasdf");
data.put("field2", "asdfasdf");
data.put("field3", "asdfasdf");
data.put("field4", "asdfasdf");
data.put("field5", "asdfasdf");
List<Map<String, String>> mylist = new LinkedList<Map<String, String>>();
data.put("mylist", mylist);
Map<String, String> item = new HashMap<String, String>();
item.put("field0", "123123");
item.put("field1", "123123");
item.put("field2", "123123");
item.put("field3", "123123");
item.put("field4", "123123");
item.put("field5", "123123");
mylist.add(item);
item = new HashMap<String, String>();
item.put("field0", "123123");
item.put("field1", "123123");
item.put("field2", "123123");
item.put("field3", "123123");
item.put("field4", "123123");
item.put("field5", "123123");
mylist.add(item);
item = new HashMap<String, String>();
item.put("field0", "123123");
item.put("field1", "123123");
item.put("field2", "123123");
item.put("field3", "123123");
item.put("field4", "123123");
item.put("field5", "123123");
mylist.add(item);
// 模板地址
String tempFile = "D:\\temp\\pdf\\template\\";
Template temple = getHtmlTemplate(tempFile, "aaa.html");
String content = getHtmlString(temple, data);
System.out.println(content);
// 生成ITextRenderer
ITextRenderer render = new ITextRenderer();
ITextFontResolver fontResolver = render.getFontResolver();
String fontPath = "D:\\temp\\pdf\\template\\hwfs.ttf";
fontResolver.addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
try {
String srcPdfPath = tempFile + "\\0001.pdf";
createPdf(content, fontPath, "D:/temp/pdf/template/", srcPdfPath);
System.out.println("生成文件地址:"+srcPdfPath);
} catch (Exception e) {
e.printStackTrace();
}
}