1、先用word只做好模板,另存为pdf格式,然后用pdf打开,启用表单编辑功能,设置好表单域。如下:
2、把模板放入项目中
3、jar包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
代码:
工具类::
public class GeneratePDFUtil {
/**
* @param response
* @param filePath 生成文件的路径
* @param showFileName 显示的文件名
* @throws IOException
*/
protected static void previewpdf(HttpServletResponse response, String filePath, String showFileName) throws IOException {
File f = new File(filePath);
if (!f.isFile() || !f.exists()) {// 文件异常
throw new FileNotFoundException("文件异常,请检查文件是否正确:" + filePath);
}
// 获取文件流
InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.setCharacterEncoding("UTF-8");
//response.setContentType("application/octet-stream;charset=UTF-8");
//response.addHeader("Content-Disposition", "attachment1;filename=" + new String(showFileName.getBytes("gb2312"), "ISO8859-1"));
response.setContentType("application/pdf;charset=UTF-8");
// 下载文件名处理
response.addHeader("Content-Disposition", "inline;filename=" + new String(showFileName.getBytes("gb2312"), "ISO8859-1"));
response.addHeader("Content-Length", "" + f.length());
// 文件流处理
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
toClient.close();
//删除临时文件
File file=new File(filePath);
file.delete();
}
/**
*
* @param templatePath 模板路径
* @param newPDFPath 新文件路径
* @param map
* @throws IOException
*/
public static void interviewReportPDF(HttpServletResponse response,String templatePath,String newPDFPath,Map<String, String> map,String showFileName) throws IOException {
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
out = new FileOutputStream(newPDFPath);// 输出流
reader = new PdfReader(templatePath);// 读取pdf模板
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
// 给表单添加中文字体 这里采用系统字体。不设置的话,中文可能无法显示
BaseFont bf = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
/*BaseFont bf = BaseFont.createFont(UtilPath.getRootPath() + "fonts/simsun.ttc,0", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);*/
form.addSubstitutionFont(bf);
//遍历map装入数据
for (Entry<String, String> entry : map.entrySet()) {
form.setField(entry.getKey(), entry.getValue());
}
stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.close();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
doc.close();
} catch (IOException e) {
} catch (DocumentException e) {
}
//到页面上进行展示
previewpdf(response,newPDFPath,showFileName);
}
controller:
@RequestMapping(value = "", method = RequestMethod.POST)
public void feeOrderTemplate(HttpServletRequest request,HttpServletResponse response,@RequestParam("orderId") String orderId) throws IOException {
Map<String,String> map = new HashMap<String,String>();
/**
* 把信息放进map
*/
//模板路径
String templatePath=request.getServletContext().getRealPath("/")+"static\\template\\order\\你的模板名字";
//生成文件的路径
String filePath=request.getServletContext().getRealPath("/")+"static\\template\\order\\"+System.currentTimeMillis()+".pdf";
GeneratePDFUtil.interviewReportPDF(response, templatePath, filePath, map,"展示时显示的名字.pdf");
}
页面:
function fyTemplate(id){
var myform = $("<form></form>");
myform.attr('method', 'post');
myform.attr('target', 'hiddenIframe');
myform.attr('action', "你的controller路径?orderId="+id);
myform.appendTo('body').submit();
myform.remove();
}
<iframe id="hiddenIframe" height="0" width="0"></iframe>
加iframe是为了新跳一个页面展示,而不是在原来的页面直接跳