Controller 写的方法——生成pdf
Controller 写的方法
/** 生成pdf */
@RequestMapping(value = "/down/down_applyTabInfo", method = RequestMethod.GET)
public String down_reviexpertInfo(HttpServletRequest request,HttpServletResponse response, String enterMstCd, ModelMap model) {
try {
Map
dataMap.put("httpstring", getHttpString(request));
makePdfInMem(response, dataMap,"applytableprint.xhtml", "物资管理.pdf");
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
private voidmakePdfInMem(HttpServletResponse response, Map
try {
DocumentHandler dh = new DocumentHandler(this.getClass().getResource(templateBaseDir).toURI().getPath());
StringBuffer sb =dh.createDocString(dataMap, templateFile);
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename,"UTF-8"));
GenPdf.makePdf(sb,response.getOutputStream());
response.flushBuffer();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/** 获取HTTP 路径 */
private String getHttpString(HttpServletRequest request) {
String basePath = request.getScheme() + "://" + request.getServerName()+ ":" + request.getServerPort() + request.getContextPath() + "/";
//当服务器在内网环境里无法使用外网固定ip访问系统,把地址固定位localhost
//String basePath = request.getScheme() +"://localhost:" + request.getServerPort() +request.getContextPath() + "/";
return basePath;
}
引用相应jar文件