001: package dinamica;
002:
003: import javax.servlet.*;
004: import java.io.ByteArrayOutputStream;
005: import com.lowagie.text.*;
006: import com.lowagie.text.pdf.*;
007:
008: /**
009: * Base class to produce PDF output modules (hand made reports)
010: * based on the powerful IText-PDF open source component.<br>
011: * This super class provides several common utility methods, including
012: * the abiity to auto-read the report header, footer and title from web.xml
013: * or config.xml, and to retrieve images from URLs (local or remotes), which is
014: * used to insert charts into the PDF document by reusing a server-side chart Action,
015: * or to insert another dinamically generated image, like a BarCode. The method to retrieve
016: * via HTTP is session sensitive, meaning that it can reuse the same session ID, which is
017: * a requirement when accessing local resources that are session-sensitive, like chart Actions.
018: * <br><br>
019: * In order to reuse this class, you must override the method createPDF(), you may use
020: * the default implementation as a code template. For more information look into
021: * the /source and /templates resources included with Dinamica distribution.
022: * <br><br>
023: * (c) 2004 Martin Cordova<br>
024: * This code is released under the LGPL license<br>
025: * Dinamica Framework - http://www.martincordova.com
026: * @author Martin Cordova (dinamica@martincordova.com)
027: * */
028: public class AbstractPDFOutput extends GenericOutput {
029:
030: /* (non-Javadoc)
031: * @see dinamica.GenericOutput#print(dinamica.GenericTransaction)
032: */
033: public void print(GenericTransaction data) throws Throwable {
034: ByteArrayOutputStream buf = new ByteArrayOutputStream();
035: createPDF(data, buf);
036: getResponse().setContentType("application/pdf");
037: getResponse().setContentLength(buf.size());
038: ServletOutputStream out = getResponse().getOutputStream();
039: buf.writeTo(out);
040: out.close();
041: }
042:
043: /**
044: * Receives a byte buffer that should be filled with resulting PDF.
045: * @param data Data module that provides recordsets to this output module
046: * @param buf Buffer to print PDF, then used to send to browser
047: * @throws Throwable
048: */
049: protected void createPDF(GenericTransaction data,
050: ByteArrayOutputStream buf) throws Throwable {
051:
052: //pdf objects
053: Document doc = new Document();
054: PdfWriter docWriter = PdfWriter.getInstance(doc, buf);
055:
056: //header
057: HeaderFooter header = new HeaderFooter(new Phrase(getHeader()),
058: false);
059: header.setBorder(Rectangle.BOTTOM);
060: header.setAlignment(Rectangle.ALIGN_CENTER);
061: doc.setHeader(header);
062:
063: //footer
064: HeaderFooter footer = new HeaderFooter(new Phrase(getFooter()),
065: true);
066: footer.setBorder(Rectangle.TOP);
067: footer.setAlignment(Rectangle.ALIGN_RIGHT);
068: doc.setFooter(footer);
069:
070: //pagesize
071: doc.setPageSize(PageSize.LETTER);
072:
073: doc.open();
074:
075: //title
076: Paragraph t = new Paragraph(getReportTitle(), new Font(
077: Font.HELVETICA, 18f));
078: t.setAlignment(Rectangle.ALIGN_CENTER);
079: doc.add(t);
080:
081: //paragraph
082: Paragraph p = new Paragraph("Hello World");
083: p.setAlignment(Rectangle.ALIGN_CENTER);
084: doc.add(p);
085:
086: doc.close();
087: docWriter.close();
088:
089: }
090:
091: /**
092: * Get default report header (parameter pdf-header) from web.xml (context-param) or current config.xml (custom element).
093: * Any subclass may override this method.
094: * @return Report header text or NULL
095: */
096: protected String getHeader() {
097:
098: return getPDFConfigValue("pdf-header");
099:
100: }
101:
102: /**
103: * Get default report footer (parameter pdf-footer) from web.xml (context-param) or current config.xml (custom element).
104: * Any subclass may override this method.
105: * @return Report footer text or NULL
106: */
107: protected String getFooter() {
108: return getPDFConfigValue("pdf-footer") + " ";
109: }
110:
111: /**
112: * Get default report title (parameter pdf-title) from web.xml (context-param) or current config.xml (custom element).
113: * Most of the time the report title should be defined in config.xml because it is
114: * specific to the report.
115: * Any subclass may override this method.
116: * @return Report title text or NULL
117: */
118: protected String getReportTitle() {
119: return getPDFConfigValue("pdf-title");
120: }
121:
122: /**
123: * Read a PDF-related config parameter. Will search first
124: * in the Action config.xml, then in web.xml file.
125: * @param param
126: * @return The corresponding value or NULL if not found.
127: */
128: protected String getPDFConfigValue(String param) {
129: String value = null;
130: try {
131: value = getConfig().getConfigValue(param);
132: if (value == null || value.trim().equals("")) {
133: value = getContext().getInitParameter(param);
134: }
135: } catch (Throwable e) {
136: value = getContext().getInitParameter(param);
137: if (value == null)
138: value = "";
139: }
140: return value;
141: }
142:
143: }
|