FileDocCategorySizeDatePackage
TermsServletPDF.javaAPI DocExample2755Sun Feb 08 21:34:02 GMT 2004None

TermsServletPDF

public class TermsServletPDF extends HttpServlet
Output the dictionary in fancy(?) PDF. This version uses "PDFlib", from PDFLib.GmbH (www.pdflib.com).
author
Ian Darwin
version
$Id: TermsServletPDF.java,v 1.5 2004/02/09 03:34:01 ian Exp $

Fields Summary
PrintWriter
out
A printwriter for getting the response.
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Handle the get request.


		try {

			out = new PrintWriter(response.getOutputStream());

			int font;
			pdflib p = new pdflib();

			if (p.open_file("") == -1) {
				warning(response,
					"Couldn't create in-memory PDF file", null);
				return;
			}

			p.set_info("Title", "Dictionary Project");
			p.set_info("Author", "Ian F. Darwin, http://www.darwinsys.com/");
			p.set_info("Creator", "www.darwinsys.com/dictionary");

			p.begin_page(595, 842);

			font = p.findfont("Helvetica", "host", 0);

			p.setfont(font, 14);

			// XXX for now just use one term from the Iterator
			Iterator e = new TermsAccessor("terms.txt").iterator();
			Term t = (Term)e.next();
			p.set_text_pos(50, 700);
			p.show("Term: ");
			p.continue_text(t.term);
			p.set_text_pos(70, 666);
			p.show("Definition: ");
			p.continue_text(t.definition);
			p.end_page();

			p.close();

			byte[] data = p.get_buffer();

			response.setContentType("application/pdf");
			response.getOutputStream().write(data);
		} catch (IOException e) {
			warning(response, "pdflib IO error:", e);
			return;
		} catch (Exception e) {
			warning(response, "pdflib error:", e);
			return;
		}
    
protected voidwarning(javax.servlet.http.HttpServletResponse response, java.lang.String error, java.lang.Exception e)
Generic error handler. Can only use before writing to response's output stream.


		response.setContentType("text/html");
		try {
			PrintWriter out = response.getWriter();
		} catch (IOException exc) {
			// egad - we can't tell the user a thing!
			System.err.println("EGAD! IO error " + exc + " trying to tell user about " + error + " " + e);
			return;
		}
	    out.println("<H1>Error</H1>");
		out.print("<P>Oh dear. You seem to have run across an error in ");
		out.print("our dictionary formatter. We apologize for the inconvenience");
		out.print("<P>Error message is ");
		out.println(error);

		if (e != null) {
			out.print("<P>Exception is: ");
			out.println(e.toString());
			out.print("Traceback is: ");
			out.print("<PRE>");
			e.printStackTrace(out);
			out.print("</PRE>");
		}
		System.out.print("DictionaryServletPDF: ");
		System.out.println(error);
		if (e != null) {
			System.out.println(e.toString());
		}