KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > document > AbstractPdfView


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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
17 package org.springframework.web.servlet.view.document;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.servlet.ServletOutputStream JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import com.lowagie.text.Document;
28 import com.lowagie.text.DocumentException;
29 import com.lowagie.text.PageSize;
30 import com.lowagie.text.pdf.PdfWriter;
31
32 import org.springframework.web.servlet.view.AbstractView;
33  
34 /**
35  * Abstract superclass for PDF views, using Bruno Lowagie's
36  * <a HREF="http://www.lowagie.com/iText">iText</a> package.
37  * Application-specific view classes will extend this class.
38  * The view will be held in the subclass itself, not in a template.
39  *
40  * <p>Note: Internet Explorer requires a ".pdf" extension, as
41  * it doesn't always respect the declared content type.
42  *
43  * @author Rod Johnson
44  * @author Jean-Pierre Pawlak
45  * @author Juergen Hoeller
46  */

47 public abstract class AbstractPdfView extends AbstractView {
48     
49     private static final int OUTPUT_BYTE_ARRAY_INITIAL_SIZE = 4096;
50
51
52     /**
53      * This constructor sets the appropriate content type "application/pdf".
54      * Note that IE won't take much notice of this, but there's not a lot we
55      * can do about this. Generated documents should have a ".pdf" extension.
56      */

57     public AbstractPdfView() {
58         setContentType("application/pdf");
59     }
60
61
62     protected final void renderMergedOutputModel(
63             Map JavaDoc model, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
64
65         // The following simple method doesn't work in IE, which
66
// needs to know the content length.
67

68         // PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
69
// document.open();
70
// buildPdfDocument(model, document, writer, request, response);
71
// document.close();
72

73         // See http://www.lowagie.com/iText/faq.html#msie
74
// for an explanation of why we can't use the obvious form above.
75

76         // IE workaround: write into byte array first.
77
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(OUTPUT_BYTE_ARRAY_INITIAL_SIZE);
78         Document document = newDocument();
79         PdfWriter writer = newWriter(document, baos);
80
81         // Apply preferences and build metadata.
82
prepareWriter(model, writer, request);
83         buildPdfMetadata(model, document, request);
84
85         // Build PDF document.
86
document.open();
87         buildPdfDocument(model, document, writer, request, response);
88         document.close();
89
90         // Write content type and also length (determined via byte array).
91
response.setContentType(getContentType());
92         response.setContentLength(baos.size());
93
94         // Flush byte array to servlet output stream.
95
ServletOutputStream JavaDoc out = response.getOutputStream();
96         baos.writeTo(out);
97         out.flush();
98     }
99
100     /**
101      * Create a new document to hold the PDF contents.
102      * <p>By default returns an A4 document, but the subclass can specify any
103      * Document, possibly parameterized via bean properties defined on the View.
104      * @return the newly created iText Document instance
105      * @see com.lowagie.text.Document#Document(com.lowagie.text.Rectangle)
106      */

107     protected Document newDocument() {
108         return new Document(PageSize.A4);
109     }
110
111     /**
112      * Create a new PdfWriter for the given iText Document.
113      * @param document the iText Document to create a writer for
114      * @param os the OutputStream to write to
115      * @return the PdfWriter instance to use
116      * @throws DocumentException if thrown during writer creation
117      */

118     protected PdfWriter newWriter(Document document, OutputStream JavaDoc os) throws DocumentException {
119         return PdfWriter.getInstance(document, os);
120     }
121
122     /**
123      * Prepare the given PdfWriter. Called before building the PDF document,
124      * that is, before the call to <code>Document.open()</code>.
125      * <p>Useful for registering a page event listener, for example.
126      * The default implementation sets the viewer preferences as returned
127      * by this class's <code>getViewerPreferences()</code> method.
128      * @param model the model, in case meta information must be populated from it
129      * @param writer the PdfWriter to prepare
130      * @param request in case we need locale etc. Shouldn't look at attributes.
131      * @throws DocumentException if thrown during writer preparation
132      * @see com.lowagie.text.Document#open()
133      * @see com.lowagie.text.pdf.PdfWriter#setPageEvent
134      * @see com.lowagie.text.pdf.PdfWriter#setViewerPreferences
135      * @see #getViewerPreferences()
136      */

137     protected void prepareWriter(Map JavaDoc model, PdfWriter writer, HttpServletRequest JavaDoc request)
138             throws DocumentException {
139
140         writer.setViewerPreferences(getViewerPreferences());
141     }
142
143     /**
144      * Return the viewer preferences for the PDF file.
145      * <p>By default returns <code>AllowPrinting</code> and
146      * <code>PageLayoutSinglePage</code>, but can be subclassed.
147      * The subclass can either have fixed preferences or retrieve
148      * them from bean properties defined on the View.
149      * @return an int containing the bits information against PdfWriter definitions
150      * @see com.lowagie.text.pdf.PdfWriter#AllowPrinting
151      * @see com.lowagie.text.pdf.PdfWriter#PageLayoutSinglePage
152      */

153     protected int getViewerPreferences() {
154         return PdfWriter.AllowPrinting | PdfWriter.PageLayoutSinglePage;
155     }
156
157     /**
158      * Populate the iText Document's meta fields (author, title, etc.).
159      * <br>Default is an empty implementation. Subclasses may override this method
160      * to add meta fields such as title, subject, author, creator, keywords, etc.
161      * This method is called after assigning a PdfWriter to the Document and
162      * before calling <code>document.open()</code>.
163      * @param model the model, in case meta information must be populated from it
164      * @param document the iText document being populated
165      * @param request in case we need locale etc. Shouldn't look at attributes.
166      * @see com.lowagie.text.Document#addTitle
167      * @see com.lowagie.text.Document#addSubject
168      * @see com.lowagie.text.Document#addKeywords
169      * @see com.lowagie.text.Document#addAuthor
170      * @see com.lowagie.text.Document#addCreator
171      * @see com.lowagie.text.Document#addProducer
172      * @see com.lowagie.text.Document#addCreationDate
173      * @see com.lowagie.text.Document#addHeader
174     */

175     protected void buildPdfMetadata(Map JavaDoc model, Document document, HttpServletRequest JavaDoc request) {
176     }
177
178     /**
179      * Subclasses must implement this method to build an iText PDF document,
180      * given the model. Called between <code>Document.open()</code> and
181      * <code>Document.close()</code> calls.
182      * <p>Note that the passed-in HTTP response is just supposed to be used
183      * for setting cookies or other HTTP headers. The built PDF document itself
184      * will automatically get written to the response after this method returns.
185      * @param model the model Map
186      * @param document the iText Document to add elements to
187      * @param writer the PdfWriter to use
188      * @param request in case we need locale etc. Shouldn't look at attributes.
189      * @param response in case we need to set cookies. Shouldn't write to it.
190      * @throws Exception any exception that occured during document building
191      * @see com.lowagie.text.Document#open()
192      * @see com.lowagie.text.Document#close()
193      */

194     protected abstract void buildPdfDocument(
195             Map JavaDoc model, Document document, PdfWriter writer,
196             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
197             throws Exception JavaDoc;
198
199 }
200
Popular Tags