1/* $Id: businesscardServlet.java 14574 2005-10-29 16:27:43Z bonefish $
2 *
3 * PDFlib client: hello servlet example in Java
4 */
5
6import java.io.*;
7import javax.servlet.*;
8
9import com.pdflib.pdflib;
10import com.pdflib.PDFlibException;
11
12public class businesscardServlet extends GenericServlet
13{
14    public void service(ServletRequest request, ServletResponse response)
15    {
16	int font;
17	pdflib p = null ;
18	int i, blockcontainer, page;
19	String infile = "boilerplate.pdf";
20        /* This is where font/image/PDF input files live. Adjust as necessary.
21         *
22         * Note that this directory must also contain the LuciduxSans font
23         * outline and metrics files.
24         */
25	String searchpath = "../data";
26	String[][] data = {
27	    { "name",                   "Victor Kraxi" },
28	    { "business.title",         "Chief Paper Officer" },
29	    { "business.address.line1", "17, Aviation Road" },
30	    { "business.address.city",  "Paperfield" },
31	    { "business.telephone.voice","phone +1 234 567-89" },
32	    { "business.telephone.fax", "fax +1 234 567-98" },
33	    { "business.email",         "victor@kraxi.com" },
34	    { "business.homepage",      "www.kraxi.com" },
35	    };
36	byte[] buf;
37	ServletOutputStream out;
38
39	try{
40	    p = new pdflib();
41
42	// Generate a PDF in memory; insert a file name to create PDF on disk
43	    if (p.open_file("") == -1) {
44		throw new Exception("Error: " + p.get_errmsg());
45	    }
46
47	    p.set_parameter("SearchPath", searchpath);
48
49	    p.set_info("Creator", "businesscard.java");
50	    p.set_info("Author", "Thomas Merz");
51	    p.set_info("Title","PDFlib block processing sample (Java)");
52
53	    blockcontainer = p.open_pdi(infile, "", 0);
54	    if (blockcontainer == -1) {
55		throw new Exception("Error: " + p.get_errmsg());
56	    }
57
58	    page = p.open_pdi_page(blockcontainer, 1, "");
59	    if (page == -1) {
60		throw new Exception("Error: " + p.get_errmsg());
61	    }
62
63	    p.begin_page(20, 20);              // dummy page size
64
65	    // This will adjust the page size to the block container's size.
66	    p.fit_pdi_page(page, 0, 0, "adjustpage");
67
68	    // Fill all text blocks with dynamic data
69	    for (i = 0; i < (int) data.length; i++) {
70		if (p.fill_textblock(page, data[i][0], data[i][1],
71			"embedding encoding=winansi") == -1) {
72		    System.err.println("Warning: " + p.get_errmsg());
73		}
74	    }
75
76	    p.end_page();                        // close page
77	    p.close_pdi_page(page);
78
79	    p.close();                           // close PDF document
80	    p.close_pdi(blockcontainer);
81
82	    buf = p.get_buffer();
83
84	    response.setContentType("application/pdf");
85	    response.setContentLength(buf.length);
86
87	    out = response.getOutputStream();
88	    out.write(buf);
89	    out.close();
90
91        } catch (PDFlibException e) {
92	    System.err.print("PDFlib exception occurred in businesscard sample:\n");
93	    System.err.print("[" + e.get_errnum() + "] " + e.get_apiname() +
94			    ": " + e.getMessage() + "\n");
95        } catch (Exception e) {
96            System.err.println(e.getMessage());
97        } finally {
98            if (p != null) {
99		p.delete();			/* delete the PDFlib object */
100            }
101        }
102    }
103}
104