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