WebSphere PDF导出问题

wuning01234 2017-11-06 05:53:52
1、功能访问URL
window.location.href = '${ctx}/portfolio/userBonusData/pdfDown.do?s_time=' + s_time + '&e_time=' + e_time+"&oaId="+oaId;
2.service
public void pdfDown(String s_date, String e_date, String oaId, HttpServletRequest request, HttpServletResponse response) throws IOException, DocumentException {
OutputStream out =null;
PdfWriter writer = null;
try {
User user = (User) request.getSession().getAttribute("user");
List<Map> list = dealData(s_date, e_date, request, oaId, user);
//文件名称
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmssSSS");
String dateStr = dateFormat.format(new Date());
String fileName = "员工奖金变化" + dateStr; //
response.setContentType("application/pdf;charset=utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".pdf");
Rectangle rect = new Rectangle(PageSize.A4);// 设置页面大小
rect.setBackgroundColor(Color.WHITE);// 页面背景色
// 页边空白
Document document = new Document(rect);// 创建一个Document
document.setMargins(10, 10, 30, 40); // 左,右,上,下
out = response.getOutputStream();
writer = PdfWriter.getInstance(document, out);// 创建书写器(Writer) 与 document对象关联,通过书写器可以将文档写入磁盘中
writer.setPdfVersion(PdfWriter.PDF_VERSION_1_7);// 设置PDF版本(默认1.4)

// 文档属性
document.addTitle("Title@sample"); // 标题
document.addAuthor("Author@nicaisheng");// 作者
document.addSubject("Subject@iText sample");// 主题
document.addKeywords("Keywords@iText");// 关键字
document.addCreator("Creator@iText");// 创建者
// 定义页头和页尾
/* HeaderFooter header = new HeaderFooter(new PdfPara);//new PdfParagraph("页头", 20,true), false
header.setAlignment(Element.ALIGN_CENTER);
HeaderFooter footer = new HeaderFooter(new Phrase("This is page "), new Phrase("."));
footer.setAlignment(Element.ALIGN_CENTER);
document.setHeader(header);
document.setFooter(footer);
*/
//设置字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

//字体出错则按一下设置系统默认字体

//BaseFont chinese = BaseFont.createFont("C:/windows/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); //系统默认中文(宋体)
Font fontChinese = new Font(bfChinese, 8, Font.NORMAL);// 创建字体,设置family,size,style,还可以设置color
Font titleChinese = new Font(bfChinese, 20, Font.BOLD);
Font BoldChinese = new Font(bfChinese, 14, Font.BOLD);
Font subBoldFontChinese = new Font(bfChinese, 8, Font.BOLD);

document.open();// 打开文档
Paragraph title = new Paragraph("员工(" + oaId + ")" + s_date + "至" + e_date + "奖金变化", titleChinese);// 设置标题
title.setAlignment(Element.ALIGN_CENTER);// 设置对齐方式
title.setLeading(1f);// 设置行间距
document.add(title);
/***************************************添加报表开始**************************************/
//如果目录不存在则创建
File uploadDir = new File(Constants.UPLOAD_IMAGE);
if (!uploadDir.exists() && !uploadDir.mkdirs()) {
uploadDir.mkdirs();
}
//1:生成报表
JFreeChart chart = ChartFactory.createLineChart(
"员工奖金数据变化趋势",
"日期", "奖金(元)",
createDataset(list),
PlotOrientation.VERTICAL,
true, true, false
);
PDFUtil.configLineChartFont(chart);
String filePath = Constants.UPLOAD_IMAGE + "/员工奖金" + dateStr + ".png";
File file = new File(filePath);
ChartUtilities.saveChartAsPNG(file, chart, 500, 450);
//3:添加到PDF
Image gif = Image.getInstance(filePath);
document.add(gif);
/***************************************添加报表结束**************************************/

/***************************************添加table开始**************************************/
float[] widths = {25f, 30f};// 设置表格的列宽和列数 默认是4列
PdfPTable table = new PdfPTable(widths);// 建立一个pdf表格
table.setSpacingBefore(20f);
table.setWidthPercentage(100);// 设置表格宽度为100%

PdfPCell cell = null;
cell = new PdfPCell(new Paragraph("日期", subBoldFontChinese));//
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
table.addCell(cell);
cell = new PdfPCell(new Paragraph("奖金", subBoldFontChinese));//
cell.setBackgroundColor(Color.LIGHT_GRAY);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);

//填充数据
if (StrUtils.isNotEmpty(list)) {
for (Map map : list) {
String yearmonth = map.get("yearmonth").toString();
String bonus = map.get("bonus").toString();
//设置编号单元格
PdfPCell cell11 = new PdfPCell(new Paragraph(yearmonth, subBoldFontChinese));
PdfPCell cell22 = new PdfPCell(new Paragraph(bonus, subBoldFontChinese));
//单元格水平对齐方式
cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
//单元格垂直对齐方式
cell11.setVerticalAlignment(Element.ALIGN_CENTER);
cell22.setHorizontalAlignment(Element.ALIGN_CENTER);
cell22.setVerticalAlignment(Element.ALIGN_CENTER);
table.addCell(cell11);
table.addCell(cell22);
}
}

document.add(table);
/***************************************添加table结束**************************************/
document.close();
} catch (DocumentException e) {
e.printStackTrace();
System.out.println(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}

报错:java.lang.IllegalStateException: SRVE0199E: OutputStream already obtained
tomcat 可以正常导出;was环境失败。
...全文
83 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

5,655

社区成员

发帖
与我相关
我的任务
社区描述
Web开发应用服务器相关讨论专区
社区管理员
  • 应用服务器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧