1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
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  
38  
39      public PdfTemplate tpl; 
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  
89  
90  
91  
92  
93  
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 
121 
122 
123 
124 
125 
126 
127     public void onEndPage(PdfWriter writer, Document document) {
128         LOG.debug("onEndPage() started.");
129         PdfContentByte cb = writer.getDirectContent();
130         cb.saveState();
131         
132         headerTable.setTotalWidth(document.right() - document.left());
133         headerTable.writeSelectedRows(0, -1, document.left(), document.getPageSize().height() - 10, cb);
134         
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         
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 
152 
153 
154 
155 
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 
168 
169 
170 
171     public PurapPdf getPageEvents() {
172         LOG.debug("getPageEvents() started.");
173         return new PurapPdf();
174     }
175 
176     
177 
178 
179 
180 
181 
182 
183 
184 
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         
190         document.setMargins(f1, f2, f3, f4);
191         return document;
192     }
193 
194     
195 
196 
197 
198 
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 }