1/* $Id: invoiceServlet.java 14574 2005-10-29 16:27:43Z bonefish $
2 *
3 * PDFlib client: invoice servlet example in Java
4 */
5
6import java.io.*;
7import java.text.*;             // DateFormat
8import java.util.*;             // Date
9import javax.servlet.*;
10
11import com.pdflib.pdflib;
12import com.pdflib.PDFlibException;
13
14public class invoiceServlet extends GenericServlet
15{
16    public void service(ServletRequest request, ServletResponse response)
17    {
18
19	pdflib p = null ;
20	int         i, form, page, regularfont, boldfont;
21	String      infile = "stationery.pdf";
22	/* This is where font/image/PDF input files live. Adjust as necessary.*/
23	String      searchpath = "../data";
24	final float col1 = 55;
25	final float col2 = 100;
26	final float col3 = 330;
27	final float col4 = 430;
28	final float col5 = 530;
29	float       fontsize = 12, leading, y;
30	float       sum, total;
31	float       pagewidth = 595, pageheight = 842;
32	Date now = new Date();
33	DateFormat fulldate = DateFormat.getDateInstance(DateFormat.LONG);
34	byte[] buf;
35	ServletOutputStream out;
36
37	String      closingtext =
38	    "30 days warranty starting at the day of sale. " +
39	    "This warranty covers defects in workmanship only. " +
40	    "Kraxi Systems, Inc. will, at its option, repair or replace the " +
41	    "product under the warranty. This warranty is not transferable. " +
42	    "No returns or exchanges will be accepted for wet products.";
43
44	String[][] data = {
45	    { "Super Kite",         "20",     "2"},
46	    { "Turbo Flyer",        "40",     "5"},
47	    { "Giga Trash",         "180",    "1"},
48	    { "Bare Bone Kit",      "50",     "3"},
49	    { "Nitty Gritty",       "20",     "10"},
50	    { "Pretty Dark Flyer",  "75",     "1"},
51	    { "Free Gift",          "0",      "1"},
52	};
53
54	String[] months = {
55	    "January", "February", "March", "April", "May", "June",
56	    "July", "August", "September", "October", "November", "December"
57	};
58
59	try{
60	    p = new pdflib();
61
62	// Generate a PDF in memory; insert a file name to create PDF on disk
63	    if (p.open_file("") == -1) {
64		throw new Exception("Error: " + p.get_errmsg());
65	    }
66
67	    p.set_parameter("SearchPath", searchpath);
68
69	    p.set_info("Creator", "invoiceServlet.java");
70	    p.set_info("Author", "Rainer Ploeckl");
71	    p.set_info("Title",
72		"PDFlib invoice generation demo (Java/Servlet)");
73
74	    form = p.open_pdi(infile, "", 0);
75	    if (form == -1) {
76		throw new Exception("Error: " + p.get_errmsg());
77	    }
78
79	    page = p.open_pdi_page(form, 1, "");
80	    if (page == -1) {
81		throw new Exception("Error: " + p.get_errmsg());
82	    }
83
84	    boldfont = p.load_font("Helvetica-Bold", "winansi", "");
85	    regularfont = p.load_font("Helvetica", "winansi", "");
86	    leading = fontsize + 2;
87
88	    // Establish coordinates with the origin in the upper left corner.
89	    p.set_parameter("topdown", "true");
90
91	    p.begin_page(pagewidth, pageheight);       // A4 page
92
93	    p.fit_pdi_page(page, 0, pageheight, "");
94	    p.close_pdi_page(page);
95
96	    p.setfont(regularfont, fontsize);
97
98	    // Print the address
99	    y = 170;
100	    p.set_value("leading", leading);
101
102	    p.show_xy("John Q. Doe", col1, y);
103	    p.continue_text("255 Customer Lane");
104	    p.continue_text("Suite B");
105	    p.continue_text("12345 User Town");
106	    p.continue_text("Everland");
107
108	    // Print the header and date
109
110	    p.setfont(boldfont, fontsize);
111	    y = 300;
112	    p.show_xy("INVOICE", col1, y);
113
114	    p.fit_textline(fulldate.format(now), col5, y, "position {100 0}");
115
116	    // Print the invoice header line
117	    p.setfont(boldfont, fontsize);
118
119	    // "position {0 0}" is left-aligned, "position {100 0}" right-aligned
120	    y = 370;
121	    p.fit_textline("ITEM",             col1, y, "position {0 0}");
122	    p.fit_textline("DESCRIPTION",      col2, y, "position {0 0}");
123	    p.fit_textline("QUANTITY",         col3, y, "position {100 0}");
124	    p.fit_textline("PRICE",            col4, y, "position {100 0}");
125	    p.fit_textline("AMOUNT",           col5, y, "position {100 0}");
126
127	    // Print the article list
128
129	    p.setfont(regularfont, fontsize);
130	    y += 2*leading;
131	    total = 0;
132
133	    for (i = 0; i < data.length; i++) {
134		p.show_xy(Integer.toString(i+1), col1, y);
135		p.show_xy(data[i][0], col2, y);
136		p.fit_textline(data[i][2], col3, y, "position {100 0}");
137		p.fit_textline(data[i][1], col4, y, "position {100 0}");
138		sum = 0;
139
140		sum = Integer.parseInt(data[i][2]) * Integer.parseInt(data[i][1]);
141		p.fit_textline(Float.toString(sum), col5, y, "position {100 0}");
142
143		y += leading;
144		total += sum;
145	    }
146
147	    y += leading;
148	    p.setfont(boldfont, fontsize);
149	    p.fit_textline(Float.toString(total), col5, y, "position {100 0}");
150
151	    // Print the closing text
152
153	    y += 5*leading;
154	    p.setfont(regularfont, fontsize);
155	    p.set_value("leading", leading);
156	    p.show_boxed(closingtext,
157		col1, y + 4*leading, col5-col1, 4*leading, "justify", "");
158
159	    p.end_page();
160	    p.close();
161	    p.close_pdi(form);
162
163	    buf = p.get_buffer();
164
165	    response.setContentType("application/pdf");
166	    response.setContentLength(buf.length);
167
168	    out = response.getOutputStream();
169	    out.write(buf);
170	    out.close();
171
172        } catch (PDFlibException e) {
173	    System.err.print("PDFlib exception occurred in invoice sample:\n");
174	    System.err.print("[" + e.get_errnum() + "] " + e.get_apiname() +
175			    ": " + e.getMessage() + "\n");
176        } catch (Exception e) {
177            System.err.println(e.getMessage());
178        } finally {
179            if (p != null) {
180		p.delete();			/* delete the PDFlib object */
181            }
182        }
183    }
184}
185