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