|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
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 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| public class PdfView implements BinaryExportView |
|
56 |
| { |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| private TableModel model; |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| private boolean exportFull; |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| private boolean header; |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| private boolean decorated; |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| private Table tablePDF; |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| private Font smallFont; |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
4
| public void setParameters(TableModel tableModel, boolean exportFullList, boolean includeHeader,
|
|
93 |
| boolean decorateValues) |
|
94 |
| { |
|
95 |
4
| this.model = tableModel;
|
|
96 |
4
| this.exportFull = exportFullList;
|
|
97 |
4
| this.header = includeHeader;
|
|
98 |
4
| this.decorated = decorateValues;
|
|
99 |
| } |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
4
| protected void initTable() throws BadElementException
|
|
106 |
| { |
|
107 |
4
| tablePDF = new Table(this.model.getNumberOfColumns());
|
|
108 |
4
| tablePDF.setDefaultVerticalAlignment(Element.ALIGN_TOP);
|
|
109 |
4
| tablePDF.setCellsFitPage(true);
|
|
110 |
4
| tablePDF.setWidth(100);
|
|
111 |
| |
|
112 |
4
| tablePDF.setPadding(2);
|
|
113 |
4
| tablePDF.setSpacing(0);
|
|
114 |
| |
|
115 |
4
| smallFont = FontFactory.getFont(FontFactory.HELVETICA, 7, Font.NORMAL, new Color(0, 0, 0));
|
|
116 |
| |
|
117 |
| } |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
4
| public String getMimeType()
|
|
124 |
| { |
|
125 |
4
| return "application/pdf";
|
|
126 |
| } |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
4
| protected void generatePDFTable() throws JspException, BadElementException
|
|
134 |
| { |
|
135 |
4
| if (this.header)
|
|
136 |
| { |
|
137 |
4
| generateHeaders();
|
|
138 |
| } |
|
139 |
4
| tablePDF.endHeaders();
|
|
140 |
4
| generateRows();
|
|
141 |
| } |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
4
| public void doExport(OutputStream out) throws JspException
|
|
147 |
| { |
|
148 |
4
| try
|
|
149 |
| { |
|
150 |
| |
|
151 |
4
| initTable();
|
|
152 |
| |
|
153 |
| |
|
154 |
4
| Document document = new Document(PageSize.A4.rotate(), 60, 60, 40, 40);
|
|
155 |
4
| document.addCreationDate();
|
|
156 |
4
| HeaderFooter footer = new HeaderFooter(new Phrase(TagConstants.EMPTY_STRING, smallFont), true);
|
|
157 |
4
| footer.setBorder(Rectangle.NO_BORDER);
|
|
158 |
4
| footer.setAlignment(Element.ALIGN_CENTER);
|
|
159 |
| |
|
160 |
4
| PdfWriter.getInstance(document, out);
|
|
161 |
| |
|
162 |
| |
|
163 |
4
| generatePDFTable();
|
|
164 |
4
| document.open();
|
|
165 |
4
| document.setFooter(footer);
|
|
166 |
4
| document.add(this.tablePDF);
|
|
167 |
4
| document.close();
|
|
168 |
| |
|
169 |
| } |
|
170 |
| catch (Exception e) |
|
171 |
| { |
|
172 |
0
| throw new PdfGenerationException(e);
|
|
173 |
| } |
|
174 |
| } |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
4
| protected void generateHeaders() throws BadElementException
|
|
181 |
| { |
|
182 |
4
| Iterator iterator = this.model.getHeaderCellList().iterator();
|
|
183 |
| |
|
184 |
4
| while (iterator.hasNext())
|
|
185 |
| { |
|
186 |
4
| HeaderCell headerCell = (HeaderCell) iterator.next();
|
|
187 |
| |
|
188 |
4
| String columnHeader = headerCell.getTitle();
|
|
189 |
| |
|
190 |
4
| if (columnHeader == null)
|
|
191 |
| { |
|
192 |
0
| columnHeader = StringUtils.capitalize(headerCell.getBeanPropertyName());
|
|
193 |
| } |
|
194 |
| |
|
195 |
4
| Cell hdrCell = getCell(columnHeader);
|
|
196 |
4
| hdrCell.setGrayFill(0.9f);
|
|
197 |
4
| hdrCell.setHeader(true);
|
|
198 |
4
| tablePDF.addCell(hdrCell);
|
|
199 |
| |
|
200 |
| } |
|
201 |
| } |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
4
| protected void generateRows() throws JspException, BadElementException
|
|
209 |
| { |
|
210 |
| |
|
211 |
4
| RowIterator rowIterator = this.model.getRowIterator(this.exportFull);
|
|
212 |
| |
|
213 |
4
| while (rowIterator.hasNext())
|
|
214 |
| { |
|
215 |
12
| Row row = rowIterator.next();
|
|
216 |
| |
|
217 |
| |
|
218 |
12
| ColumnIterator columnIterator = row.getColumnIterator(this.model.getHeaderCellList());
|
|
219 |
| |
|
220 |
12
| while (columnIterator.hasNext())
|
|
221 |
| { |
|
222 |
12
| Column column = columnIterator.nextColumn();
|
|
223 |
| |
|
224 |
| |
|
225 |
12
| Object value = column.getValue(this.decorated);
|
|
226 |
| |
|
227 |
12
| Cell cell = getCell(ObjectUtils.toString(value));
|
|
228 |
12
| tablePDF.addCell(cell);
|
|
229 |
| } |
|
230 |
| } |
|
231 |
| } |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
16
| private Cell getCell(String value) throws BadElementException
|
|
240 |
| { |
|
241 |
16
| Cell cell = new Cell(new Chunk(StringUtils.trimToEmpty(value), smallFont));
|
|
242 |
16
| cell.setVerticalAlignment(Element.ALIGN_TOP);
|
|
243 |
16
| cell.setLeading(8);
|
|
244 |
16
| return cell;
|
|
245 |
| } |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| static class PdfGenerationException extends BaseNestableJspTagException |
|
253 |
| { |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| private static final long serialVersionUID = 899149338534L; |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
0
| public PdfGenerationException(Throwable cause)
|
|
265 |
| { |
|
266 |
0
| super(PdfView.class, Messages.getString("PdfView.errorexporting"), cause);
|
|
267 |
| } |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
0
| public SeverityEnum getSeverity()
|
|
273 |
| { |
|
274 |
0
| return SeverityEnum.ERROR;
|
|
275 |
| } |
|
276 |
| } |
|
277 |
| } |