View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.pdf;
17  
18  import com.lowagie.text.*;
19  import com.lowagie.text.pdf.*;
20  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
21  import org.kuali.ole.sys.context.SpringContext;
22  import org.kuali.rice.core.api.datetime.DateTimeService;
23  import org.kuali.rice.core.api.util.type.KualiDecimal;
24  import org.kuali.rice.krad.util.GlobalVariables;
25  import org.kuali.rice.krad.util.KRADUtils;
26  import org.kuali.rice.krad.util.ObjectUtils;
27  
28  import java.io.File;
29  
30  /**
31   * Base class to be extended for implementing PDF documents in Purchasing/Accounts Payable module.
32   */
33  public class PurapPdf extends PdfPageEventHelper {
34      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PurapPdf.class);
35  
36      /**
37       * headerTable pieces need to be public
38       */
39      public PdfTemplate tpl; // A template that will hold the total number of pages.
40      public PdfContentByte cb;
41      public Image logo;
42      public PdfPTable headerTable;
43      public PdfPTable nestedHeaderTable;
44      public String campusName;
45      public PurchaseOrderDocument po;
46      public String logoImage;
47      public BaseFont helv;
48      public String environment;
49      public boolean isPreview = false;
50      public boolean isRetransmit = false;
51  
52      Font ver_4_normal = FontFactory.getFont("VERDANA", 4, 0);
53      Font ver_5_normal = FontFactory.getFont("VERDANA", 5, 0);
54      Font ver_6_normal = FontFactory.getFont("VERDANA", 6, 0);
55      Font ver_8_normal = FontFactory.getFont("VERDANA", 8, 0);
56      Font ver_10_normal = FontFactory.getFont("VERDANA", 10, 0);
57      Font ver_11_normal = FontFactory.getFont("VERDANA", 11, 0);
58      Font ver_12_normal = FontFactory.getFont("VERDANA", 12, 0);
59      Font ver_13_normal = FontFactory.getFont("VERDANA", 13, 0);
60      Font ver_14_normal = FontFactory.getFont("VERDANA", 14, 0);
61      Font ver_15_normal = FontFactory.getFont("VERDANA", 15, 0);
62      Font ver_16_normal = FontFactory.getFont("VERDANA", 16, 0);
63      Font ver_17_normal = FontFactory.getFont("VERDANA", 17, 0);
64  
65      Font ver_6_bold = FontFactory.getFont("VERDANA", 6, 1);
66      Font ver_8_bold = FontFactory.getFont("VERDANA", 8, 1);
67      Font ver_10_bold = FontFactory.getFont("VERDANA", 10, 1);
68  
69      Font cour_7_normal = FontFactory.getFont("COURIER", 7, 0);
70      Font cour_10_normal = FontFactory.getFont("COURIER", 10, 0);
71  
72      static KualiDecimal zero = KualiDecimal.ZERO;
73  
74      private DateTimeService dateTimeService;
75  
76      public PurapPdf() {
77          super();
78      }
79  
80      public DateTimeService getDateTimeService() {
81          if (ObjectUtils.isNull(dateTimeService)) {
82              this.dateTimeService = SpringContext.getBean(DateTimeService.class);
83          }
84          return this.dateTimeService;
85      }
86  
87      /**
88       * Overrides the method in PdfPageEventHelper from itext to include our watermark text to indicate that
89       * this is a Test document and include the environment, if the environment is not a production environment.
90       *
91       * @param writer   The PdfWriter for this document.
92       * @param document The document.
93       * @see com.lowagie.text.pdf.PdfPageEventHelper#onStartPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
94       */
95      public void onStartPage(PdfWriter writer, Document document) {
96          if (!KRADUtils.isProductionEnvironment()) {
97              PdfContentByte cb = writer.getDirectContentUnder();
98              cb.saveState();
99              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 }