001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.purap.pdf;
017:
018: import java.io.File;
019:
020: import org.kuali.core.service.DateTimeService;
021: import org.kuali.core.service.KualiConfigurationService;
022: import org.kuali.core.util.KualiDecimal;
023: import org.kuali.core.util.ObjectUtils;
024: import org.kuali.kfs.context.SpringContext;
025: import org.kuali.module.purap.document.PurchaseOrderDocument;
026:
027: import com.lowagie.text.Document;
028: import com.lowagie.text.Element;
029: import com.lowagie.text.Font;
030: import com.lowagie.text.FontFactory;
031: import com.lowagie.text.Image;
032: import com.lowagie.text.PageSize;
033: import com.lowagie.text.pdf.BaseFont;
034: import com.lowagie.text.pdf.PdfContentByte;
035: import com.lowagie.text.pdf.PdfPTable;
036: import com.lowagie.text.pdf.PdfPageEventHelper;
037: import com.lowagie.text.pdf.PdfTemplate;
038: import com.lowagie.text.pdf.PdfWriter;
039:
040: /**
041: * Base class to be extended for implementing PDF documents in Purchasing/Accounts Payable module.
042: */
043: public class PurapPdf extends PdfPageEventHelper {
044: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
045: .getLogger(PurapPdf.class);
046:
047: /** headerTable pieces need to be public */
048: public PdfTemplate tpl; // A template that will hold the total number of pages.
049: public PdfContentByte cb;
050: public Image logo;
051: public PdfPTable headerTable;
052: public PdfPTable nestedHeaderTable;
053: public String campusName;
054: public PurchaseOrderDocument po;
055: public String logoImage;
056: public BaseFont helv;
057: public String environment;
058: public boolean isRetransmit = false;
059:
060: Font ver_4_normal = FontFactory.getFont("VERDANA", 4, 0);
061: Font ver_5_normal = FontFactory.getFont("VERDANA", 5, 0);
062: Font ver_6_normal = FontFactory.getFont("VERDANA", 6, 0);
063: Font ver_8_normal = FontFactory.getFont("VERDANA", 8, 0);
064: Font ver_10_normal = FontFactory.getFont("VERDANA", 10, 0);
065: Font ver_11_normal = FontFactory.getFont("VERDANA", 11, 0);
066: Font ver_12_normal = FontFactory.getFont("VERDANA", 12, 0);
067: Font ver_13_normal = FontFactory.getFont("VERDANA", 13, 0);
068: Font ver_14_normal = FontFactory.getFont("VERDANA", 14, 0);
069: Font ver_15_normal = FontFactory.getFont("VERDANA", 15, 0);
070: Font ver_16_normal = FontFactory.getFont("VERDANA", 16, 0);
071: Font ver_17_normal = FontFactory.getFont("VERDANA", 17, 0);
072:
073: Font ver_6_bold = FontFactory.getFont("VERDANA", 6, 1);
074: Font ver_8_bold = FontFactory.getFont("VERDANA", 8, 1);
075: Font ver_10_bold = FontFactory.getFont("VERDANA", 10, 1);
076:
077: Font cour_10_normal = FontFactory.getFont("COURIER", 10, 0);
078:
079: static KualiDecimal zero = new KualiDecimal(0);
080:
081: private DateTimeService dateTimeService;
082:
083: public PurapPdf() {
084: super ();
085: }
086:
087: public DateTimeService getDateTimeService() {
088: if (ObjectUtils.isNull(dateTimeService)) {
089: this .dateTimeService = SpringContext
090: .getBean(DateTimeService.class);
091: }
092: return this .dateTimeService;
093: }
094:
095: /**
096: * Overrides the method in PdfPageEventHelper from itext to include our watermark text to indicate that
097: * this is a Test document and include the environment, if the environment is not a production environment.
098: *
099: * @param writer The PdfWriter for this document.
100: * @param document The document.
101: * @see com.lowagie.text.pdf.PdfPageEventHelper#onStartPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
102: */
103: public void onStartPage(PdfWriter writer, Document document) {
104: if (!SpringContext.getBean(KualiConfigurationService.class)
105: .isProductionEnvironment()) {
106: PdfContentByte cb = writer.getDirectContentUnder();
107: cb.saveState();
108: cb.beginText();
109: cb.setFontAndSize(helv, 48);
110: String watermarkText = "Test document (" + environment
111: + ")";
112: cb.showTextAligned(Element.ALIGN_CENTER, watermarkText,
113: document.getPageSize().width() / 2, document
114: .getPageSize().height() / 2, 45);
115: cb.endText();
116: cb.restoreState();
117: }
118: }
119:
120: /**
121: * Overrides the method in PdfPageEventHelper from itext to write the headerTable, compose the footer and show the
122: * footer.
123: *
124: * @param writer The PdfWriter for this document.
125: * @param document The document.
126: * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
127: */
128: public void onEndPage(PdfWriter writer, Document document) {
129: LOG.debug("onEndPage() started.");
130: PdfContentByte cb = writer.getDirectContent();
131: cb.saveState();
132: // write the headerTable
133: headerTable.setTotalWidth(document.right() - document.left());
134: headerTable.writeSelectedRows(0, -1, document.left(), document
135: .getPageSize().height() - 10, cb);
136: // compose the footer
137: String text = "Page " + writer.getPageNumber() + " of ";
138: float textSize = helv.getWidthPoint(text, 12);
139: float textBase = document.bottom() - 20;
140: cb.beginText();
141: cb.setFontAndSize(helv, 12);
142: // show the footer
143: float adjust = helv.getWidthPoint("0", 12);
144: cb
145: .setTextMatrix(document.right() - textSize - adjust,
146: textBase);
147: cb.showText(text);
148: cb.endText();
149: cb.addTemplate(tpl, document.right() - adjust, textBase);
150: cb.saveState();
151: }
152:
153: /**
154: * Overrides the method in the PdfPageEventHelper from itext to put the total number of pages into the template.
155: *
156: * @param writer The PdfWriter for this document.
157: * @param document The document.
158: * @see com.lowagie.text.pdf.PdfPageEventHelper#onCloseDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
159: */
160: public void onCloseDocument(PdfWriter writer, Document document) {
161: LOG.debug("onCloseDocument() started.");
162: tpl.beginText();
163: tpl.setFontAndSize(helv, 12);
164: tpl.setTextMatrix(0, 0);
165: tpl.showText("" + (writer.getPageNumber() - 1));
166: tpl.endText();
167: }
168:
169: /**
170: * Gets a PageEvents object.
171: *
172: * @return a new PageEvents object
173: */
174: public PurapPdf getPageEvents() {
175: LOG.debug("getPageEvents() started.");
176: return new PurapPdf();
177: }
178:
179: /**
180: * Creates an instance of a new Document and set its margins according to
181: * the given input parameters.
182: *
183: * @param f1 Left margin.
184: * @param f2 Right margin.
185: * @param f3 Top margin.
186: * @param f4 Bottom margin.
187: * @return The created Document object.
188: */
189: public Document getDocument(float f1, float f2, float f3, float f4) {
190: LOG.debug("getDocument() started");
191: Document document = new Document(PageSize.A4);
192: // Margins: 36pt = 0.5 inch, 72pt = 1 inch. Left, right, top, bottom.
193: document.setMargins(f1, f2, f3, f4);
194: return document;
195: }
196:
197: /**
198: * Deletes an already created PDF.
199: *
200: * @param pdfFileLocation The location of the pdf file.
201: * @param pdfFilename The name of the pdf file.
202: */
203: public void deletePdf(String pdfFileLocation, String pdfFilename) {
204: LOG
205: .debug("deletePdf() started for po pdf file: "
206: + pdfFilename);
207: File f = new File(pdfFileLocation + pdfFilename);
208: f.delete();
209: }
210:
211: }
|