java springMVC pdf 下载


用户信息表下载功能

第一步  前台ajax 调用。

第二部  controller层方法

/**
* Description:用户信息PDF下载
*
* @author rolax
* @param request
* @param attr
* @return 2016-11-10
*/
@RequestMapping(value = "/downloadUserInfopdf")
@ResponseBody
public ResponseEntity<byte[]> downloadUserInfopdf(HttpServletRequest request,
RedirectAttributes attr) {
byte[] pdfdata=null;
String UserId = request.getParameter("UserId");
try {
UserId=URLDecoder.decode(UserId,"UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (UserId!=null && !("").equals(UserId)) {
pdfdata=this.accountManagerService.downloadUserInfopdf(UserId);
}
String fileName=null;
try {
fileName=new String("会员信息.pdf".getBytes("GBK"),"iso-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
return new ResponseEntity<byte[]>(pdfdata,headers, HttpStatus.OK);

}

 

第三步  service层方法

/**
* 根据用户ID获取用户信息PDF
* @return
*/
public byte[] downloadUserInfopdf(String userId) {
byte[] pdfbytes=null;
// 用户信息
TbUserExample tbuserexample = new TbUserExample();
com.nstc.lgip.pojo.TbUserExample.Criteria usercriteria = tbuserexample.createCriteria();
usercriteria.andAccountEqualTo(userId);
List<TbUser> userinfolist=tbuserDao.selectByExample(tbuserexample);
StringBuffer industryTypeSb=new StringBuffer();
if(userinfolist!=null&&userinfolist.size()>0){
TbUser userinfo=userinfolist.get(0);
if(userinfo.getFirmIdentify()!=null){
//投资方向
TbIntentionExample tbintentionexample=new TbIntentionExample();
com.nstc.lgip.pojo.TbIntentionExample.Criteria tbintentioncriteria=tbintentionexample.createCriteria();
tbintentioncriteria.andFirmIdEqualTo(userinfo.getId());
//行业为1
tbintentioncriteria.andTypeEqualTo(1);
List<TbIntention> tbintentionlist=tbintentionDao.selectByExample(tbintentionexample);
if(tbintentionlist!=null && tbintentionlist.size()>0){
for(TbIntention tbintention:tbintentionlist ){
if(tbintention.getValue()!=null&&tbintention.getValue().indexOf(",")>0){
String[] intentions=tbintention.getValue().split(",");
for(String intention:intentions){
industryTypeSb.append(IndustryMap.getInstance().containsKey(intention)?IndustryMap.getInstance().get(intention):intention);
industryTypeSb.append(";");
}
}else{
industryTypeSb.append(IndustryMap.getInstance().get(tbintention.getValue())+",");
}

}
if(industryTypeSb.toString().length() > 0){
industryTypeSb = industryTypeSb.deleteCharAt(industryTypeSb.length()-1);
}
}



}
//附件
TbAppendixExample tbappendexample = new TbAppendixExample();
Criteria criteria = tbappendexample.createCriteria();
criteria.andLinkIdEqualTo(userinfo.getId());
criteria.andFlagEqualTo(0);
List<TbAppendix> appendlist =tbappendixDao.selectByExample(tbappendexample);
//单位类型
SysDictionaryUtil sdutil=new SysDictionaryUtil();
List<SysDictionary> sdlist=sdutil.getObjectByType("U_TYPE");
HashMap<String,String> typemap=new HashMap<String,String>();
if(sdlist!=null && sdlist.size()>0){
for(SysDictionary sd:sdlist){
typemap.put(sd.getDataValue(), sd.getDataDesc());
}

}
String path=this.getClass().getClassLoader().getResource("/").getPath();
path = path.substring(1, path.indexOf("classes"));
String temppath=userinfo.getUserRank()==null?("/"+path+"resource/pdfTemp/membertmp.pdf"):("/"+path+"resource/pdfTemp/UserInfoTmp.pdf");//linux 路径
// String temppath=userinfo.getUserRank()==null?(path+"resource/pdfTemp/membertmp.pdf"):(path+"resource/pdfTemp/UserInfoTmp.pdf"); //windows 路径
// path = path.substring(1, path.indexOf("lgip"));
// String temppath=(path+"resource/pdfTemp/UserInfoTmp.pdf");
// String temppath=(path+"lgip/src/main/webapp/WEB-INF/resource/pdfTemp/membertmp.pdf");;
System.out.println("pdf path:"+temppath);
try {
PdfReader reader = new PdfReader(temppath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfStamper ps = new PdfStamper(reader, bos);
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
fontList.add(bf);
AcroFields s = ps.getAcroFields();
s.setSubstitutionFonts(fontList);
s.setField("investment_type_str", industryTypeSb.toString());

PdfUtil.fillPdfTbUserData(s,userinfo);//填充数据
//s.setField("investment_type_str", industryTypeSb.toString());


TbIntentionExample tbintentionexample1=new TbIntentionExample();
com.nstc.lgip.pojo.TbIntentionExample.Criteria tbintentioncriteria1=tbintentionexample1.createCriteria();
tbintentioncriteria1.andFirmIdEqualTo(userinfo.getId());
//区域为2
tbintentioncriteria1.andTypeEqualTo(2);
List<TbIntention> tbintentionlist1=tbintentionDao.selectByExample(tbintentionexample1);
if(tbintentionlist1.size()>0){
s.setField("industry_area",AreaMap.getValueByKey(tbintentionlist1.get(0).getValue()));//投资区域
}

s.setField("firm_type_str", userinfo.getFirmType()==null?"":typemap.get(userinfo.getFirmType().toString()));
if(appendlist!=null && appendlist.size()>0){
StringBuffer dixnamesb=new StringBuffer();
for(TbAppendix append:appendlist){
dixnamesb.append(append.getDixName()+";\r\n");
}
s.setField("dix_name", dixnamesb.toString());
}
ps.setFormFlattening(true);
ps.close();
pdfbytes=bos.toByteArray();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return pdfbytes;

}

 

第四步 填充数据方法类

public class PdfUtil {


public static void fillPdfTbUserData(AcroFields form,TbUser data){
SimpleDateFormat df=new SimpleDateFormat("yyyy年MM月dd日");
try {
form.setField("firm_name", data.getFirmName());
form.setField("firm_orgcode", data.getFirmOrgcode());
form.setField("invest_type_str",data.getInvestType()==null?"":data.getInvestType()==1?"非金融机构":data.getInvestType()==2?"非银行类金融机构":data.getInvestType()==3?"银行":""); //投资者类型:1金融机构 2投资机构 3一般企业

form.setField("user_rank_str", data.getUserRank()==null?"":data.getUserRank()==0?"普通会员":data.getUserRank()==1?"高级会员":""); //会员级别 :一般会员 0 高级会员 1
form.setField("firm_expirydate", data.getFirmExpirydate()==null?"":df.format(data.getFirmExpirydate()));
form.setField("firm_expirydate2", data.getFirmExpirydate2()==null?"长期":df.format(data.getFirmExpirydate2()));
form.setField("firm_legal", data.getFirmLegal());
form.setField("user_num", data.getUserNum()==null?"":data.getUserNum());
StringBuffer firmbus=new StringBuffer("");
if(data.getFirmBus()!=null&&data.getFirmBus().indexOf(",")>0){
String[] buscodes=data.getFirmBus().split(",");
for(String buscode:buscodes){
firmbus.append(IndustryMap.getInstance().containsKey(buscode)?IndustryMap.getInstance().get(buscode):buscode);
firmbus.append(";");
}
}else{
firmbus.append(IndustryMap.getInstance().containsKey(data.getFirmBus())?IndustryMap.getInstance().get(data.getFirmBus()):data.getFirmBus());
}
form.setField("firm_bus", firmbus.toString());
form.setField("firm_capital", data.getFirmCapital()+" 万元");
// form.setField("firm_type_str", data.getFirmType());//单位类型:
form.setField("firm_abstract", data.getFirmAbstract());
form.setField("contact_name", data.getContactName());
form.setField("phone",data.getPhone());
form.setField("contact_name2", data.getContactName2());
form.setField("phone2",data.getPhone2());
form.setField("contactEmail",data.getContactEmail());
form.setField("contactTel",data.getContactTel());
form.setField("contactEmail2",data.getContactEmail2());
form.setField("contactTel2",data.getContactTel2());
form.setField("contactPostcode",data.getContactPostcode());
form.setField("contactFax",data.getContactFax());
form.setField("firmCreateTime", data.getFirmCreateTime()==null?"":df.format(data.getFirmCreateTime()));
if (!StringUtils.isNullOrBlank(data.getFirmLicenseAb())) {
form.setField("firmLicenseAb",data.getFirmLicenseAb());
}
if (data.getFirmIntention()!=null) {
if (data.getFirmIntention()==1) {
form.setField("prefer_type","投资行业");//优先选择
}else if(data.getFirmIntention()==2){
form.setField("prefer_type","投资区域");
}
}

if (!StringUtils.isNullOrBlank(data.getRegisteredLand())) {
form.setField("registeredLand",AreaMap.getValueByKey(data.getRegisteredLand())+(data.getRegisteredLandAddr()==null?"":data.getRegisteredLandAddr()));
}
form.setField("contactAddr",data.getFirmAddress());

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM