public void print() throws IOException, DocumentException {
// freemark模板填充解析
String htmlData = parseTemplate();
// 设置文档格式,数字边距 A4横向
Document document = new Document(new RectangleReadOnly(842F, 595F));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
// 添加页码
PDFBuilder builder = new PDFBuilder();
writer.setPageEvent(builder);
// 打开文档
document.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(htmlData.getBytes()), Charset.forName("UTF-8"), new MyFontsProvider());
// 关闭文档
document.close();
}
table.ftl模板 样式由html控制
<div style="text-align: center;font-size: 50px;font-weight: bold">排班设置</div>
<table style="repeat-header:yes;repeat-footer:yes;width: 100%" border="1" cellspacing="0"
cellpadding="0">
<thead style=" background: #d6f2f1;">
<tr style="font-size: 12px;height: 50px; line-height: 50px; color: #0a8783; text-align: center;">
<td style="width:100px; ">组名</td>
<td style="width:120px; ">工号</td>
<td style="width:100px;">姓名</td>
<td style="width:100px;">工时</td>
<td style="width:100px; ">欠休</td>
<td style="width:200px;font-size: 10px;">星期一(5-13)</td>
<td style="width:200px;font-size: 10px;">星期一(5-13)</td>
<td style="width:200px;font-size: 10px;">星期一(5-13)</td>
<td style="width:200px;font-size: 10px;">星期一(5-13)</td>
<td style="width:200px;font-size: 10px;">星期一(5-13)</td>
<td style="width:200px;font-size: 10px;">星期一(5-13)</td>
<td style="width:200px;font-size: 10px;">星期日(5-13)</td>
</tr>
</thead>
<tbody>
<#list lists as item>
<tr style="width: 100%;font-size: 12px; height: 40px; line-height: 40px; color: #333; text-align: center;">
<td>${item.groupName}</td>
<td>${item.workerNo}</td>
<td>
${item.name}
</td>
<td>${item.workerTime}</td>
<td>${item.oweToTake}</td>
<td>${item.groupName}</td>
<td>${item.groupName}</td>
<td>${item.groupName}</td>
<td>${item.groupName}</td>
<td>${item.groupName}</td>
<td>${item.groupName}</td>
<td>${item.groupName}</td>
</tr>
</#list>
</tbody>
</table>
pdf效果: