package com.bdjh.dcontrol.util;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import com.bdjh.dcontrol.entity.PurchaseRecordInfo;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class PDFReport {
Document document = new Document();// 建立一个Document对象
private static Font headfont;// 设置字体大小
private static Font keyfont;// 设置字体大小
private static Font textfont;// 设置字体大小
static{
BaseFont bfChinese;
try {
//bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
headfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小
keyfont = new Font(bfChinese, 8, Font.BOLD);// 设置字体大小
textfont = new Font(bfChinese, 8, Font.NORMAL);// 设置字体大小
} catch (Exception e) {
e.printStackTrace();
}
}
public PDFReport(File file) {
document.setPageSize(PageSize.A4);// 设置页面大小
try {
PdfWriter.getInstance(document,new FileOutputStream(file));
document.open();
} catch (Exception e) {
e.printStackTrace();
}
}
int maxWidth = 520;
public PdfPCell createCell(String value,com.lowagie.text.Font font,int align){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value,font));
return cell;
}
public PdfPCell createCell(String value,com.lowagie.text.Font font){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPhrase(new Phrase(value,font));
return cell;
}
public PdfPCell createCell(String value,com.lowagie.text.Font font,int align,int colspan){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value,font));
return cell;
}
public PdfPCell createCell(String value,com.lowagie.text.Font font,int align,int colspan,boolean boderFlag){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value,font));
cell.setPadding(3.0f);
if(!boderFlag){
cell.setBorder(0);
cell.setPaddingTop(15.0f);
cell.setPaddingBottom(8.0f);
}
return cell;
}
public PdfPTable createTable(int colNumber){
PdfPTable table = new PdfPTable(colNumber);
try{
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
}catch(Exception e){
e.printStackTrace();
}
return table;
}
public PdfPTable createTable(float[] widths){
PdfPTable table = new PdfPTable(widths);
try{
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
}catch(Exception e){
e.printStackTrace();
}
return table;
}
public PdfPTable createBlankTable(){
PdfPTable table = new PdfPTable(1);
table.getDefaultCell().setBorder(0);
table.addCell(createCell("", keyfont));
table.setSpacingAfter(20.0f);
table.setSpacingBefore(20.0f);
return table;
}
public void getPurchasedRecordPDF(String name,List<PurchaseRecordInfo> obj) throws Exception{
PdfPTable table = createTable(3);
table.addCell(createCell(name+"的购房记录:", keyfont,Element.ALIGN_LEFT,3,false));
table.addCell(createCell("时间", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("记录", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("备注", keyfont, Element.ALIGN_CENTER));
for (int i = 0; i < obj.size(); i++) {
table.addCell(createCell(obj.get(i).getOperationTime(), textfont));
if(obj.get(i).getReceptStage() == 1){
table.addCell(createCell(obj.get(i).getGuestVisitStage(), textfont));
}else{
String info = "";
String staffName = "";
if(obj.get(i).getStaffName() != null){
staffName = obj.get(i).getStaffName();
}
if(obj.get(i).getType() == 1){
info = staffName+" "+obj.get(i).getGuestVisitStage();
}else{
if(obj.get(i).getCallReult().equals("1")){
info = staffName+" 呼出电话"+(obj.get(i).getCallInterval()+"分钟");
}else{
info = staffName+" 呼出未接通";
}
}
table.addCell(createCell(info, textfont));
}
String info2 = "";
if(!obj.get(i).getRemark().equals("false")){
info2+=obj.get(i).getRemark();
}else{
info2+="";
}
table.addCell(createCell(info2, textfont));
}
document.add(table);
document.close();
}
}
上面这段代码为生成数据
下面为调用
@ResponseBody
public String getPurchasedRecordPDF(@RequestBody PurchaseRecordRequest request,HttpServletRequest httpRequset,HttpServletResponse resp) throws Exception{
PurchasedRecordPDFResponse response = new PurchasedRecordPDFResponse();
GuestPurchaseInfoRequest grequest = new GuestPurchaseInfoRequest();
grequest.setGuestID(request.getGuestID());
grequest.setOfficeID(request.getOfficeID());
GuestEntity guest = guestManageServiceImpl.getGuestPurchaseInfo(grequest);
List<Object> obj = ...//获取数据,该数据为传入接口数据
if(obj != null && obj.size() > 0){
String path = httpRequset.getSession().getServletContext().getRealPath("/"); //获取项目绝对路径
path = path.replace( '\\','/' );
String url = path+"data/"+request.getOfficeID()+"/file/other";
File folder = new File(url);
if (!folder.exists()) {//查看是否存在文件夹,创建文件夹
folder.mkdirs();
}
String fileUrl = path+"data/"+request.getOfficeID()+"/file/other/homebuy_history.pdf";//设置pdt创建路径
File file = new File(fileUrl);
file.createNewFile();//创建文件
new PDFReport(file).getPurchasedRecordPDF(guest.getName(),obj);//调用写入方法
response.setFileUrl("DControlWebServer/data/"+request.getOfficeID()+"/file/other/homebuy_history.pdf");//前台返回写入路径
response.setRetCode(ResultConstant.RESULT_CODE_SUCCESS);
response.setRetDesc("成功");
}else{
response.setRetCode(ResultConstant.RESULT_CODE_DEFEATED);
response.setRetDesc("没有数据");
}
return encrypt(response);
}