您现在的位置:首页 >> 前端 >> 内容

Controller 写的方法——生成pdf

时间:2017/3/28 9:12:00 点击:

  核心提示:Controller 写的方法生成pdfController 写的方法/** 生成pdf */@RequestMapping(value = /down/down_applyTabInfo, meth...

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 = new HashMap();

dataMap.put("httpstring", getHttpString(request));

makePdfInMem(response, dataMap,"applytableprint.xhtml", "物资管理.pdf");

}catch (Exception e) {

e.printStackTrace();

}

return null;

}

 

 

private voidmakePdfInMem(HttpServletResponse response, Map dataMap,String templateFile, String filename) {

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文件

Tags:CO ON NT TR 
作者:网络 来源:零星世界