一、制作文件模板 pdf.ftl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"></meta>
<style type="text/css">
body {
font-family: SimSun;
}
.title_item {
width: 100%;
text-align: center
}
.tr_item2{
width: 650px;
word-wrap:break-word;
}
</style>
</head>
<body>
<p class="title_item">${paperTiele}</p>
<table style="word-wrap:break-word; word-break:break-all;width: 693px;table-layout: fixed">
<tbody>
<#list vms as vm>
<tr>
<td style="width:20px;text-align:top;vertical-align: text-top;">${vm.sortNum}、</td>
<td class="tr_item2" style="word-wrap:break-word;word-break:break-all;">${vm.exerciseInfo}</td>
</tr>
<#list vm.options as optItem>
<tr>
<td style="width:20px;text-align:top;;"></td>
<td class="tr_item2"> ${optItem.optionIndex}:${optItem.optionContent}</td>
</tr>
</#list>
<tr>
<td style="width: 20px;px;text-align:top;;"></td>
<td class="tr_item2">【正确答案】:${vm.answer}</td>
</tr>
<tr>
<td style="width:20px;text-align:top;;"></td>
<td class="tr_item2">【解析】:${vm.explain}</td>
</tr>
<tr style="height:20px;"><td style="width:20px;text-align:top;;"></td><td></td></tr>
<tr style="height:20px;"><td style="width:20px;text-align:top;;"></td><td></td></tr>
</#list>
</tbody>
</table>
</body>
</html>
二、安装字体
三、模板工具
package com.xw.ssm.util.pdf;
import com.lowagie.text.pdf.BaseFont;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.Locale;
/**
* freemarker模板生成PDF工具类
*
* @author ZSX
*/
public class PDFTemplateUtil {
/**
* classpath路径
*/
private String classpath = getClass().getResource("/").getPath();
/**
* 指定FreeMarker模板文件的位置
*/
private String