KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > reports > PDFSalesReport


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  * Class description.
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 PDFSalesReport 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         //totals table
53
PdfPTable totals = getTotalsSection(data);
54         totals.setSpacingBefore(20);
55         doc.add(totals);
56
57         //blank line
58
doc.add(new Paragraph(" "));
59
60         //chart
61
String JavaDoc ctx = getRequest().getContextPath();
62         int port = getRequest().getServerPort();
63         Image img = Image.getInstance(getImage("http://localhost:" + port + ctx + "/action/saleschart-pdf/chart", false));
64         img.setAlignment(Element.ALIGN_CENTER);
65         doc.add(img);
66         
67         doc.close();
68         docWriter.close();
69         
70     }
71
72     /**
73      * Return a report section formatted as a table
74      * @param data
75      * @return
76      */

77     PdfPTable getTotalsSection(GenericTransaction data) throws Throwable JavaDoc
78     {
79
80         /*
81         * totals by year
82         */

83         
84         //get recordsets
85
Recordset rs = data.getRecordset("sales.sql");
86         rs.top();
87         
88         //cols
89
PdfPTable datatable = new PdfPTable(2);
90             
91         //header
92
datatable.getDefaultCell().setPadding(3);
93         int headerwidths[] = {70, 30}; // percentage
94
datatable.setWidths(headerwidths);
95         datatable.setWidthPercentage(50); // percentage
96
datatable.setHorizontalAlignment(Element.ALIGN_CENTER);
97         datatable.getDefaultCell().setBorderWidth(1);
98         datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
99         datatable.getDefaultCell().setGrayFill(0.9f);
100         datatable.addCell("Product Category");
101         datatable.addCell("Total Sales US$");
102         datatable.setHeaderRows(1);
103         datatable.getDefaultCell().setGrayFill(0.0f);
104         
105         //orders
106
while (rs.next())
107         {
108             datatable.addCell(rs.getString("categoryname"));
109             
110             PdfPCell c = new PdfPCell(new Phrase(StringUtil.formatNumber(rs.getValue("subtotal"), "#,##0.00")));
111             c.setHorizontalAlignment(Element.ALIGN_RIGHT);
112             datatable.addCell(c);
113         }
114
115         return datatable;
116         
117     }
118
119 }
120
Popular Tags