1 package reports; 2 3 import com.lowagie.text.*; 4 import com.lowagie.text.pdf.*; 5 import java.io.ByteArrayOutputStream ; 6 import dinamica.*; 7 8 16 public class PDFCustomerReport extends AbstractPDFOutput 17 { 18 19 22 protected void createPDF( 23 GenericTransaction data, 24 ByteArrayOutputStream buf) 25 throws Throwable 26 { 27 28 Document doc = new Document(); 30 PdfWriter docWriter = PdfWriter.getInstance(doc, buf); 31 doc.setPageSize(PageSize.LETTER); 32 33 HeaderFooter header = new HeaderFooter(new Phrase(getHeader()), false); 35 header.setBorder(Rectangle.BOTTOM); 36 header.setAlignment(Rectangle.ALIGN_CENTER); 37 doc.setHeader(header); 38 39 HeaderFooter footer = new HeaderFooter(new Phrase(getFooter()), true); 41 footer.setBorder(Rectangle.TOP); 42 footer.setAlignment(Rectangle.ALIGN_RIGHT); 43 doc.setFooter(footer); 44 45 doc.open(); 46 47 Paragraph t = new Paragraph(getReportTitle(),new Font(Font.HELVETICA, 18f)); 49 t.setAlignment(Rectangle.ALIGN_CENTER); 50 doc.add(t); 51 52 PdfPTable custinfo = getCustomerSection(data); 54 custinfo.setSpacingBefore(20f); 55 doc.add(custinfo); 56 57 doc.add(new Paragraph(" ")); 59 60 PdfPTable orders = getOrdersSection(data); 62 doc.add(orders); 63 64 doc.newPage(); 66 67 PdfContentByte cb = docWriter.getDirectContent(); 70 71 PdfPTable totals = getTotalsByYearSection(data); 73 74 String ctx = getRequest().getContextPath(); 76 int port = getRequest().getServerPort(); 77 Image jpeg = Image.getInstance(getImage("http://localhost:" + port + ctx + "/action/customers/sales/chart", false)); 78 79 float imgX = doc.right() - jpeg.width(); 81 82 float imgY = doc.top() - doc.topMargin(); 84 85 jpeg.setAbsolutePosition(imgX, imgY - jpeg.height()); 87 cb.addImage(jpeg); 88 89 totals.setTotalWidth(imgX - doc.left() - 36); 91 totals.writeSelectedRows(0, -1, doc.left(), imgY, cb); 92 93 doc.close(); 94 docWriter.close(); 95 96 } 97 98 101 protected String getReportTitle() 102 { 103 return "Customer Profile Report"; 104 } 105 106 111 PdfPTable getCustomerSection(GenericTransaction data) throws Throwable 112 { 113 114 117 118 Recordset custinfo = data.getRecordset("customerinfo.sql"); 120 custinfo.first(); 121 122 PdfPTable datatable = new PdfPTable(2); 124 125 datatable.getDefaultCell().setPadding(3); 127 int headerwidths[] = {30, 70}; datatable.setWidths(headerwidths); 129 datatable.setWidthPercentage(50); datatable.setHorizontalAlignment(Element.ALIGN_CENTER); 131 datatable.getDefaultCell().setBorderWidth(1); 132 datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); 133 134 PdfPCell c1 = new PdfPCell(new Phrase("Contacto")); 135 c1.setGrayFill(0.9f); 136 datatable.addCell(c1); 137 datatable.addCell(custinfo.getString("contactname")); 138 139 PdfPCell c2 = new PdfPCell(new Phrase("Empresa")); 140 c2.setGrayFill(0.9f); 141 datatable.addCell(c2); 142 datatable.addCell(custinfo.getString("companyname")); 143 144 PdfPCell c3 = new PdfPCell(new Phrase("Teléfono")); 145 c3.setGrayFill(0.9f); 146 datatable.addCell(c3); 147 datatable.addCell(custinfo.getString("phone")); 148 149 PdfPCell c4 = new PdfPCell(new Phrase("País")); 150 c4.setGrayFill(0.9f); 151 datatable.addCell(c4); 152 datatable.addCell(custinfo.getString("country")); 153 154 return datatable; 155 156 } 157 158 163 PdfPTable getOrdersSection(GenericTransaction data) throws Throwable 164 { 165 166 169 170 Recordset rs = data.getRecordset("query.sql"); 172 rs.top(); 173 174 Recordset rsTotal = data.getRecordset("total.sql"); 175 rsTotal.first(); 176 177 PdfPTable datatable = new PdfPTable(3); 179 180 datatable.getDefaultCell().setPadding(3); 182 int headerwidths[] = {20, 40, 40}; datatable.setWidths(headerwidths); 184 datatable.setWidthPercentage(60); datatable.setHorizontalAlignment(Element.ALIGN_CENTER); 186 datatable.getDefaultCell().setBorderWidth(1); 187 datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); 188 datatable.getDefaultCell().setGrayFill(0.9f); 189 datatable.addCell("Order #"); 190 datatable.addCell("Date"); 191 datatable.addCell("Sub-Total"); 192 datatable.setHeaderRows(1); 193 datatable.getDefaultCell().setGrayFill(0.0f); 194 195 while (rs.next()) 197 { 198 datatable.addCell(rs.getString("orderid")); 199 datatable.addCell(StringUtil.formatDate(rs.getDate("orderdate"), "dd-MM-yyyy")); 200 201 PdfPCell c = new PdfPCell(new Phrase(StringUtil.formatNumber(rs.getValue("subtotal"), "#,##0.00"))); 202 c.setHorizontalAlignment(Element.ALIGN_RIGHT); 203 datatable.addCell(c); 204 } 205 206 PdfPCell c1 = new PdfPCell(new Phrase("TOTAL", new Font(Font.HELVETICA, 10f, Font.BOLD))); 208 c1.setColspan(2); 209 datatable.addCell(c1); 210 211 PdfPCell c2 = new PdfPCell(new Phrase(StringUtil.formatNumber(rsTotal.getValue("total"), "#,##0.00"))); 212 c2.setHorizontalAlignment(Element.ALIGN_RIGHT); 213 datatable.addCell(c2); 214 215 return datatable; 216 217 } 218 219 224 PdfPTable getTotalsByYearSection(GenericTransaction data) throws Throwable 225 { 226 227 230 231 Recordset rs = data.getRecordset("total_per_year.sql"); 233 rs.top(); 234 235 PdfPTable datatable = new PdfPTable(2); 237 238 datatable.getDefaultCell().setPadding(3); 240 int headerwidths[] = {40, 60}; datatable.setWidths(headerwidths); 242 datatable.setWidthPercentage(40); datatable.setHorizontalAlignment(Element.ALIGN_CENTER); 244 datatable.getDefaultCell().setBorderWidth(1); 245 datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); 246 datatable.getDefaultCell().setGrayFill(0.9f); 247 datatable.addCell("Year"); 248 datatable.addCell("Total Purchased"); 249 datatable.setHeaderRows(1); 250 datatable.getDefaultCell().setGrayFill(0.0f); 251 252 while (rs.next()) 254 { 255 datatable.addCell(rs.getString("the_year")); 256 257 PdfPCell c = new PdfPCell(new Phrase(StringUtil.formatNumber(rs.getValue("subtotal"), "#,##0.00"))); 258 c.setHorizontalAlignment(Element.ALIGN_RIGHT); 259 datatable.addCell(c); 260 } 261 262 return datatable; 263 264 } 265 266 } 267 | Popular Tags |