package com.nfcswp.common.util;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.nfcswp.common.model.ResultReport;
import com.nfcswp.entity.ResultInfo;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* 描述:根据投保单号 生成pdf<br>
* 作者:lihengjun <br>
* 修改日期:2012-9-14上午10:38:15 <br>
* E-mail:
*/
public class PdfUtil extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String pdfName;
/**
*
* 描述:生成电子投保凭证
* @author lihengjun
* 修改日期:2012-9-14上午09:50:47
* @param policyNo 投保单号
*/
public void createPdf(HttpServletResponse response,ResultReport resReport) throws Exception{
response.reset();
OutputStream os = response.getOutputStream();// 取得输出流
response.setContentType("application/pdf");//可以在网页中用pdf reader打开
String fname="TestReport"+DateUtil.currentDate2YMDMethod();
Document document = new Document();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PdfWriter.getInstance(document, buffer);
response.setHeader("Content-disposition", "attachment; filename=" + new String(("123123.pdf").getBytes(), "iso-8859-1"));
try {
// 设定文档的远东字符集
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 9, Font.NORMAL);// 小五号黑体
Font FontChineseFooter = new Font(bfChinese, 10, Font.BOLD);// 五号黑体加粗
Font FontChineseUnderline = new Font(bfChinese, 9, Font.UNDERLINE);// 小五号黑体下划线
FontChineseUnderline.setColor(0, 0, 255);
Font FontHeadBold = new Font(bfChinese, 18, Font.BOLD);// 小二号黑体加粗
Font FontBold = new Font(bfChinese, 9, Font.BOLD);// 小五号黑体加粗
Font FontContent = new Font(bfChinese, 7, Font.NORMAL);// 小六号黑体
// 打开文档
document.open();
/** 保险止期 */
// 设定文档的标题
Paragraph Title = new Paragraph("Automated Testing Report", FontHeadBold);
Paragraph row021 = new Paragraph("2013/07/02", FontBold);
Paragraph row031 = new Paragraph("Statistical Information", FontBold);
Paragraph row041 = new Paragraph("scenes name", FontBold);
Paragraph row042 = new Paragraph("create time", FontBold);
Paragraph row043 = new Paragraph("total executed", FontBold);
Paragraph row044 = new Paragraph("pass", FontBold);
Paragraph row045 = new Paragraph("fail", FontBold);
Paragraph row051 = new Paragraph(resReport.getScenesName(),FontChinese);
Paragraph row052 = new Paragraph(resReport.getTimeTag(),FontChinese);
Paragraph row053 = new Paragraph(String.valueOf(resReport.getTotalNum()),FontChinese);
Paragraph row054 = new Paragraph(resReport.getSuccessResultNum()+"",FontChinese);
Paragraph row055 = new Paragraph(resReport.getFailResultNum()+"",FontChinese);
Paragraph row061 = new Paragraph("Test Case Information", FontBold);
Paragraph row071 = new Paragraph("case name", FontBold);
Paragraph row072 = new Paragraph("case number", FontBold);
Paragraph row073 = new Paragraph("test result", FontBold);
Paragraph row074 = new Paragraph("error message", FontBold);
PdfPTable tableScenes = new PdfPTable(5);
tableScenes.setHorizontalAlignment(Element.ALIGN_MIDDLE);
int hws[] = {15,15,15,15,15};
tableScenes.setWidths(hws);
tableScenes.setTotalWidth(15);
tableScenes.setSpacingBefore(30);
// 设定头
PdfPCell cell = new PdfPCell(Title);
cell.setUseDescender(true);
cell.setHorizontalAlignment(1);
cell.setBorderWidth(0);
// cell.setHeader(true);
cell.setRowspan(2);
cell.setColspan(5);
tableScenes.addCell(cell);
// 第二行第一列
cell = new PdfPCell(row021);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setUseDescender(true);
cell.setBorderWidth(0);
// cell.setBorderWidthBottom(1);
cell.setColspan(5);
tableScenes.addCell(cell);
// 第三行第一列
cell = new PdfPCell(row031);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setUseDescender(false);
cell.setBorderWidth(0);
cell.setColspan(5);
tableScenes.addCell(cell);
// 第四行第一列
cell = new PdfPCell(row041);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableScenes.addCell(cell);
// 第四行第二列
cell = new PdfPCell(row042);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableScenes.addCell(cell);
// 第四行第三列
cell = new PdfPCell(row043);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableScenes.addCell(cell);
// 第四行第四列
cell = new PdfPCell(row044);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
// cell.setNoWrap(true);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableScenes.addCell(cell);
// 第四行第五列
cell = new PdfPCell(row045);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableScenes.addCell(cell);
// 第五行第一列
cell = new PdfPCell(row051);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableScenes.addCell(cell);
// 第五行第二列
cell = new PdfPCell(row052);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableScenes.addCell(cell);
// 第五行第三列
cell = new PdfPCell(row053);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableScenes.addCell(cell);
// 第五行第四列
cell = new PdfPCell(row054);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableScenes.addCell(cell);
// 第五行第五列
cell = new PdfPCell(row055);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableScenes.addCell(cell);
PdfPTable tableCase = new PdfPTable(4);
tableCase.setHorizontalAlignment(Element.ALIGN_MIDDLE);
int hws2[] = {15,15,15,15};
tableCase.setWidths(hws2);
tableCase.setTotalWidth(15);
tableCase.setSpacingBefore(30);
// 第六行第一列
cell = new PdfPCell(row061);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setUseDescender(true);
cell.setBorderWidth(0);
cell.setColspan(4);
tableCase.addCell(cell);
// 第七行第一列
cell = new PdfPCell(row071);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableCase.addCell(cell);
// 第七行第二列
cell = new PdfPCell(row072);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableCase.addCell(cell);
// 第七行第三列
cell = new PdfPCell(row073);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableCase.addCell(cell);
// 第七行第四列
cell = new PdfPCell(row074);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableCase.addCell(cell);
List<ResultInfo> tmpResList = resReport.getResultList();
if(tmpResList!=null && tmpResList.size()>0){
for(int i=0;i<tmpResList.size();i++){
Paragraph row_caseResult_1 = new Paragraph(tmpResList.get(i).getCase_info().getName()+"", FontChinese);
Paragraph row_caseResult_2 = new Paragraph(tmpResList.get(i).getCase_info().getCase_number()+"", FontChinese);
Paragraph row_caseResult_3 = new Paragraph(tmpResList.get(i).getRun_state().getName()+"", FontChinese);
Paragraph row_caseResult_4 = new Paragraph(tmpResList.get(i).getError_message(), FontChinese);
cell = new PdfPCell(row_caseResult_1);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableCase.addCell(cell);
cell = new PdfPCell(row_caseResult_2);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableCase.addCell(cell);
cell = new PdfPCell(row_caseResult_3);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableCase.addCell(cell);
cell = new PdfPCell(row_caseResult_4);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setUseDescender(true);
cell.setBorderWidth(1);
cell.setColspan(1);
tableCase.addCell(cell);
}
}
document.add(tableScenes);
document.add(tableCase);
document.close();
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentLength(buffer.size());
//创建servletoutputstream流对象,即为response。getoutputstream
ServletOutputStream out = response.getOutputStream();
buffer.writeTo(out);//将字节输出流写入到servletoutputstream
out.flush();//out。flush输出内容
} catch (Exception e) {
e.printStackTrace();
}
}
public String getPdfName() {
return pdfName;
}
public void setPdfName(String pdfName) {
this.pdfName = pdfName;
}
public static void main(String[] args) {
PdfUtil pdf = new PdfUtil();
//pdf.createPdf();
System.exit(0);
}
}