1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| List<WayBill> wayBills = wayBillService.findWayBills(model);
ServletActionContext.getResponse().setContentType("application/pdf"); String fileName = "运单数据.pdf"; String user_agent = ServletActionContext.getRequest().getHeader("user-agent");
String filename = FileUtils.encodeDownloadFilename(fileName, user_agent);
ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + filename);
Document document = new Document(); PdfWriter.getInstance(document, ServletActionContext.getResponse().getOutputStream()); document.open(); Table table = new Table(7); table.setWidth(90); table.setBorder(1); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER);
BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false); Font font = new Font(baseFont, 10, Font.NORMAL, Color.BLUE);
table.addCell(buildCell("运单号", font)); table.addCell(buildCell("寄件人", font)); table.addCell(buildCell("寄件人电话", font)); table.addCell(buildCell("寄件人地址", font)); table.addCell(buildCell("收件人", font)); table.addCell(buildCell("收件人电话", font)); table.addCell(buildCell("收件人地址", font));
for (WayBill wayBill : wayBills) { table.addCell(buildCell(wayBill.getWayBillNum(), font)); table.addCell(buildCell(wayBill.getSendName(), font)); table.addCell(buildCell(wayBill.getSendMobile(), font)); table.addCell(buildCell(wayBill.getSendAddress(), font)); table.addCell(buildCell(wayBill.getRecName(), font)); table.addCell(buildCell(wayBill.getRecMobile(), font)); table.addCell(buildCell(wayBill.getRecAddress(), font)); } document.add(table);
document.close();
private Cell buildCell(String content, Font font) throws BadElementException { Phrase phrase = new Phrase(content, font); return new Cell(phrase); }
|