package com.book.web; import java.awt.Color; import java.io.ByteArrayOutputStream; import java.util.List; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; import com.book.entity.Shop; import com.book.util.BaseDao; import com.book.util.DateFormatHelper; import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.Font; import com.lowagie.text.Phrase; import com.lowagie.text.Table; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfWriter; /**导出pdf * @author liuluogen 2011-01-20 * */ public class PDFAction extends DispatchAction { private BaseDao baseDao; @SuppressWarnings("unchecked") public ActionForward outPDF(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { try { Document document = new Document(); ByteArrayOutputStream ba = new ByteArrayOutputStream(); PdfWriter p = PdfWriter.getInstance(document, ba); document.open(); BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font fontChinese = new Font(bfChinese, 8, Font.NORMAL); Font fontHead = new Font(bfChinese, 10, Font.BOLD); Table datatable = new Table(4); // 新生成一个表,参数表示列 float[] headerwidths = { 30, 30, 24, 60 };// 设置每一列的宽度 datatable.setWidths(headerwidths); datatable.setPadding(3); datatable.setBorder(1); datatable.addCell(new Phrase("用户详细信息", fontHead)); Cell cell = new Cell(""); cell.setColspan(3); datatable.addCell(cell); datatable.addCell(new Phrase("客户编号", fontChinese)); datatable.addCell(new Phrase("HZ00000001", fontChinese)); datatable.addCell(new Phrase("联系人", fontChinese)); datatable.addCell(new Phrase(" Contact Name ", fontChinese)); // DefaultHorizontalAlignment // datatable.addColumns(2); datatable.addCell(new Phrase("客户名称(中文)", fontChinese)); datatable.addCell(new Phrase("HZ-CSPC", fontChinese)); datatable.addCell(new Phrase("联系电话(传真)", fontChinese)); datatable.addCell(new Phrase(" 0752-9999999(0752-0000000) ", fontChinese)); // // datatable.addColumns(3); datatable.addCell(new Phrase("客户名称(英文", fontChinese)); datatable.addCell(new Phrase("惠州-CSPC", fontChinese)); datatable.addCell(new Phrase("电子信箱", fontChinese)); datatable.addCell(new Phrase(" [email protected] ", fontChinese)); // datatable.addColumns(4); datatable.addCell(new Phrase("客户类型", fontChinese)); datatable.addCell(new Phrase("钻石客", fontChinese)); datatable.addCell(new Phrase("站点URL", fontChinese)); datatable.addCell(new Phrase(" www.cspc.com ", fontChinese)); // datatable.addColumns(5); datatable.addCell(new Phrase(" 地址 ", fontChinese)); datatable.addCell(new Phrase(" 惠州-中海壳 ", fontChinese)); cell = new Cell(""); cell.setColspan(2); datatable.addCell(cell); document.add(datatable); document.close(); response.setContentType("application/pdf"); response.setContentLength(ba.size()); ServletOutputStream out = response.getOutputStream(); ba.writeTo(out); out.flush(); } catch (Exception e){ e.printStackTrace(); } return null; } @SuppressWarnings("unchecked") public ActionForward outPDFShop(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { try{ Document document = new Document(); ByteArrayOutputStream ba = new ByteArrayOutputStream(); PdfWriter p = PdfWriter.getInstance(document, ba); document.open(); BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font fontChinese = new Font(bfChinese, 8, Font.NORMAL); Font fontHead = new Font(bfChinese, 10, Font.BOLD); Table datatable = new Table(4); // 新生成一个表,参数表示列 float[] headerwidths = { 10, 30, 24, 60 };// 设置每一列的宽度 datatable.setWidths(headerwidths); datatable.setPadding(3); datatable.setBorder(1); datatable.addCell(new Phrase("序号", fontHead)); datatable.addCell(new Phrase("书城编号", fontHead)); datatable.addCell(new Phrase("书城名称", fontHead)); datatable.addCell(new Phrase("创建时间", fontHead)); //添加书城列表 List<Shop> shop = baseDao.findAll(Shop.class); int i=1; for (Shop shop2 : shop){ datatable.addCell(new Phrase( String.valueOf(i++), fontChinese)); datatable.addCell(new Phrase(shop2.getWh(), fontChinese)); datatable.addCell(new Phrase(shop2.getShopName(), fontChinese)); datatable.addCell(new Phrase(DateFormatHelper.getStringDate(shop2.getCreateTime()), fontChinese)); } document.add(datatable); document.close(); response.setContentType("application/pdf"); response.setContentLength(ba.size()); ServletOutputStream out = response.getOutputStream(); ba.writeTo(out); out.flush(); } catch (Exception e) { e.printStackTrace(); } return null; } public void setBaseDao(BaseDao baseDao) { this.baseDao = baseDao; } }