1 12 package org.displaytag.export; 13 14 import java.awt.Color ; 15 import java.io.OutputStream ; 16 import java.util.Iterator ; 17 18 import javax.servlet.jsp.JspException ; 19 20 import org.apache.commons.lang.ObjectUtils; 21 import org.apache.commons.lang.StringUtils; 22 import org.displaytag.Messages; 23 import org.displaytag.exception.BaseNestableJspTagException; 24 import org.displaytag.exception.SeverityEnum; 25 import org.displaytag.model.Column; 26 import org.displaytag.model.ColumnIterator; 27 import org.displaytag.model.HeaderCell; 28 import org.displaytag.model.Row; 29 import org.displaytag.model.RowIterator; 30 import org.displaytag.model.TableModel; 31 import org.displaytag.util.TagConstants; 32 33 import com.lowagie.text.BadElementException; 34 import com.lowagie.text.Cell; 35 import com.lowagie.text.Chunk; 36 import com.lowagie.text.Document; 37 import com.lowagie.text.Element; 38 import com.lowagie.text.Font; 39 import com.lowagie.text.FontFactory; 40 import com.lowagie.text.HeaderFooter; 41 import com.lowagie.text.PageSize; 42 import com.lowagie.text.Phrase; 43 import com.lowagie.text.Rectangle; 44 import com.lowagie.text.Table; 45 import com.lowagie.text.pdf.PdfWriter; 46 47 48 55 public class PdfView implements BinaryExportView 56 { 57 58 61 private TableModel model; 62 63 66 private boolean exportFull; 67 68 71 private boolean header; 72 73 76 private boolean decorated; 77 78 82 private Table tablePDF; 83 84 87 private Font smallFont; 88 89 92 public void setParameters(TableModel tableModel, boolean exportFullList, boolean includeHeader, 93 boolean decorateValues) 94 { 95 this.model = tableModel; 96 this.exportFull = exportFullList; 97 this.header = includeHeader; 98 this.decorated = decorateValues; 99 } 100 101 105 protected void initTable() throws BadElementException 106 { 107 tablePDF = new Table(this.model.getNumberOfColumns()); 108 tablePDF.setDefaultVerticalAlignment(Element.ALIGN_TOP); 109 tablePDF.setCellsFitPage(true); 110 tablePDF.setWidth(100); 111 112 tablePDF.setPadding(2); 113 tablePDF.setSpacing(0); 114 115 smallFont = FontFactory.getFont(FontFactory.HELVETICA, 7, Font.NORMAL, new Color (0, 0, 0)); 116 117 } 118 119 123 public String getMimeType() 124 { 125 return "application/pdf"; } 127 128 133 protected void generatePDFTable() throws JspException , BadElementException 134 { 135 if (this.header) 136 { 137 generateHeaders(); 138 } 139 tablePDF.endHeaders(); 140 generateRows(); 141 } 142 143 146 public void doExport(OutputStream out) throws JspException 147 { 148 try 149 { 150 initTable(); 152 153 Document document = new Document(PageSize.A4.rotate(), 60, 60, 40, 40); 155 document.addCreationDate(); 156 HeaderFooter footer = new HeaderFooter(new Phrase(TagConstants.EMPTY_STRING, smallFont), true); 157 footer.setBorder(Rectangle.NO_BORDER); 158 footer.setAlignment(Element.ALIGN_CENTER); 159 160 PdfWriter.getInstance(document, out); 161 162 generatePDFTable(); 164 document.open(); 165 document.setFooter(footer); 166 document.add(this.tablePDF); 167 document.close(); 168 169 } 170 catch (Exception e) 171 { 172 throw new PdfGenerationException(e); 173 } 174 } 175 176 180 protected void generateHeaders() throws BadElementException 181 { 182 Iterator iterator = this.model.getHeaderCellList().iterator(); 183 184 while (iterator.hasNext()) 185 { 186 HeaderCell headerCell = (HeaderCell) iterator.next(); 187 188 String columnHeader = headerCell.getTitle(); 189 190 if (columnHeader == null) 191 { 192 columnHeader = StringUtils.capitalize(headerCell.getBeanPropertyName()); 193 } 194 195 Cell hdrCell = getCell(columnHeader); 196 hdrCell.setGrayFill(0.9f); 197 hdrCell.setHeader(true); 198 tablePDF.addCell(hdrCell); 199 200 } 201 } 202 203 208 protected void generateRows() throws JspException , BadElementException 209 { 210 RowIterator rowIterator = this.model.getRowIterator(this.exportFull); 212 while (rowIterator.hasNext()) 214 { 215 Row row = rowIterator.next(); 216 217 ColumnIterator columnIterator = row.getColumnIterator(this.model.getHeaderCellList()); 219 220 while (columnIterator.hasNext()) 221 { 222 Column column = columnIterator.nextColumn(); 223 224 Object value = column.getValue(this.decorated); 226 227 Cell cell = getCell(ObjectUtils.toString(value)); 228 tablePDF.addCell(cell); 229 } 230 } 231 } 232 233 239 private Cell getCell(String value) throws BadElementException 240 { 241 Cell cell = new Cell(new Chunk(StringUtils.trimToEmpty(value), smallFont)); 242 cell.setVerticalAlignment(Element.ALIGN_TOP); 243 cell.setLeading(8); 244 return cell; 245 } 246 247 252 static class PdfGenerationException extends BaseNestableJspTagException 253 { 254 255 258 private static final long serialVersionUID = 899149338534L; 259 260 264 public PdfGenerationException(Throwable cause) 265 { 266 super(PdfView.class, Messages.getString("PdfView.errorexporting"), cause); } 268 269 272 public SeverityEnum getSeverity() 273 { 274 return SeverityEnum.ERROR; 275 } 276 } 277 } | Popular Tags |