KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > reports > PDFCustomerReport


1 package reports;
2
3 import com.lowagie.text.*;
4 import com.lowagie.text.pdf.*;
5 import java.io.ByteArrayOutputStream JavaDoc;
6 import dinamica.*;
7
8 /**
9  * Customer Profile Report
10  * <br><br>
11  * (c) 2004 Martin Cordova<br>
12  * This code is released under the LGPL license<br>
13  * Dinamica Framework - http://www.martincordova.com
14  * @author Martin Cordova (dinamica@martincordova.com)
15  * */

16 public class PDFCustomerReport extends AbstractPDFOutput
17 {
18
19     /* (non-Javadoc)
20      * @see dinamica.AbstractPDFOutput#createPDF(dinamica.GenericTransaction, java.io.ByteArrayOutputStream)
21      */

22     protected void createPDF(
23         GenericTransaction data,
24         ByteArrayOutputStream JavaDoc buf)
25         throws Throwable JavaDoc
26     {
27
28         //init pdf - pagesize, margins, etc.
29
Document doc = new Document();
30         PdfWriter docWriter = PdfWriter.getInstance(doc, buf);
31         doc.setPageSize(PageSize.LETTER);
32         
33         //header
34
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         //footer
40
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         //title
48
Paragraph t = new Paragraph(getReportTitle(),new Font(Font.HELVETICA, 18f));
49         t.setAlignment(Rectangle.ALIGN_CENTER);
50         doc.add(t);
51
52         //customer basic info
53
PdfPTable custinfo = getCustomerSection(data);
54         custinfo.setSpacingBefore(20f);
55         doc.add(custinfo);
56
57         //blank line
58
doc.add(new Paragraph(" "));
59
60         //customer orders table
61
PdfPTable orders = getOrdersSection(data);
62         doc.add(orders);
63         
64         //new page
65
doc.newPage();
66
67         //will use exact x,y positioning for complex page layout
68
//all measures are points - 72 points = 1 inch = 2.54cm
69
PdfContentByte cb = docWriter.getDirectContent();
70
71         //customer orders table
72
PdfPTable totals = getTotalsByYearSection(data);
73
74         //chart
75
String JavaDoc 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         //calculate x for image
80
float imgX = doc.right() - jpeg.width();
81         
82         //calculate y for table (top of printable area)
83
float imgY = doc.top() - doc.topMargin();
84         
85         //place image - images use the lower lefthand corner for the X,Y coordinates!
86
jpeg.setAbsolutePosition(imgX, imgY - jpeg.height());
87         cb.addImage(jpeg);
88         
89         //place table - tables use the upper lefthand corner for the X,Y coordinates!
90
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     /* (non-Javadoc)
99      * @see dinamica.AbstractPDFOutput#getReportTitle()
100      */

101     protected String JavaDoc getReportTitle()
102     {
103         return "Customer Profile Report";
104     }
105
106     /**
107      * Return a report section formatted as a table
108      * @param data
109      * @return
110      */

111     PdfPTable getCustomerSection(GenericTransaction data) throws Throwable JavaDoc
112     {
113
114         /*
115         * customer basic info
116         */

117         
118         //get recordset
119
Recordset custinfo = data.getRecordset("customerinfo.sql");
120         custinfo.first();
121         
122         //cols
123
PdfPTable datatable = new PdfPTable(2);
124             
125         //header
126
datatable.getDefaultCell().setPadding(3);
127         int headerwidths[] = {30, 70}; // percentage
128
datatable.setWidths(headerwidths);
129         datatable.setWidthPercentage(50); // percentage
130
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     /**
159      * Return a report section formatted as a table
160      * @param data
161      * @return
162      */

163     PdfPTable getOrdersSection(GenericTransaction data) throws Throwable JavaDoc
164     {
165
166         /*
167         * customer orders
168         */

169         
170         //get recordsets
171
Recordset rs = data.getRecordset("query.sql");
172         rs.top();
173         
174         Recordset rsTotal = data.getRecordset("total.sql");
175         rsTotal.first();
176         
177         //cols
178
PdfPTable datatable = new PdfPTable(3);
179             
180         //header
181
datatable.getDefaultCell().setPadding(3);
182         int headerwidths[] = {20, 40, 40}; // percentage
183
datatable.setWidths(headerwidths);
184         datatable.setWidthPercentage(60); // percentage
185
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         //orders
196
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         //total
207
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     /**
220      * Return a report section formatted as a table
221      * @param data
222      * @return
223      */

224     PdfPTable getTotalsByYearSection(GenericTransaction data) throws Throwable JavaDoc
225     {
226
227         /*
228         * totals by year
229         */

230         
231         //get recordsets
232
Recordset rs = data.getRecordset("total_per_year.sql");
233         rs.top();
234         
235         //cols
236
PdfPTable datatable = new PdfPTable(2);
237             
238         //header
239
datatable.getDefaultCell().setPadding(3);
240         int headerwidths[] = {40, 60}; // percentage
241
datatable.setWidths(headerwidths);
242         datatable.setWidthPercentage(40); // percentage
243
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         //orders
253
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