Abstract superclass for PDF views, using Bruno Lowagie's
package.
Application-specific view classes will extend this class.
The view will be held in the subclass itself, not in a template.
Note: Internet Explorer requires a ".pdf" extension, as
it doesn't always respect the declared content type.
| Method from org.springframework.web.servlet.view.document.AbstractPdfView Detail: |
abstract protected void buildPdfDocument(Map model,
Document document,
PdfWriter writer,
HttpServletRequest request,
HttpServletResponse response) throws Exception
Subclasses must implement this method to build an iText PDF document,
given the model. Called between Document.open() and
Document.close() calls.
Note that the passed-in HTTP response is just supposed to be used
for setting cookies or other HTTP headers. The built PDF document itself
will automatically get written to the response after this method returns. |
protected void buildPdfMetadata(Map model,
Document document,
HttpServletRequest request) {
}
Populate the iText Document's meta fields (author, title, etc.).
Default is an empty implementation. Subclasses may override this method
to add meta fields such as title, subject, author, creator, keywords, etc.
This method is called after assigning a PdfWriter to the Document and
before calling document.open(). |
protected boolean generatesDownloadContent() {
return true;
}
|
protected int getViewerPreferences() {
return PdfWriter.AllowPrinting | PdfWriter.PageLayoutSinglePage;
}
Return the viewer preferences for the PDF file.
By default returns AllowPrinting and
PageLayoutSinglePage, but can be subclassed.
The subclass can either have fixed preferences or retrieve
them from bean properties defined on the View. |
protected Document newDocument() {
return new Document(PageSize.A4);
}
Create a new document to hold the PDF contents.
By default returns an A4 document, but the subclass can specify any
Document, possibly parameterized via bean properties defined on the View. |
protected PdfWriter newWriter(Document document,
OutputStream os) throws DocumentException {
return PdfWriter.getInstance(document, os);
}
Create a new PdfWriter for the given iText Document. |
protected void prepareWriter(Map model,
PdfWriter writer,
HttpServletRequest request) throws DocumentException {
writer.setViewerPreferences(getViewerPreferences());
}
Prepare the given PdfWriter. Called before building the PDF document,
that is, before the call to Document.open().
Useful for registering a page event listener, for example.
The default implementation sets the viewer preferences as returned
by this class's getViewerPreferences() method. |
protected final void renderMergedOutputModel(Map model,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
// IE workaround: write into byte array first.
ByteArrayOutputStream baos = createTemporaryOutputStream();
// Apply preferences and build metadata.
Document document = newDocument();
PdfWriter writer = newWriter(document, baos);
prepareWriter(model, writer, request);
buildPdfMetadata(model, document, request);
// Build PDF document.
document.open();
buildPdfDocument(model, document, writer, request, response);
document.close();
// Flush to HTTP response.
writeToResponse(response, baos);
}
|