|
Spring Framework example source code file (AbstractPdfView.java)
The Spring Framework AbstractPdfView.java source code
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.view.document;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfWriter;
import org.springframework.web.servlet.view.AbstractView;
/**
* Abstract superclass for PDF views, using Bruno Lowagie's
* <a href="http://www.lowagie.com/iText">iText package.
* Application-specific view classes will extend this class.
* The view will be held in the subclass itself, not in a template.
*
* <p>Note: Internet Explorer requires a ".pdf" extension, as
* it doesn't always respect the declared content type.
*
* @author Rod Johnson
* @author Jean-Pierre Pawlak
* @author Juergen Hoeller
*/
public abstract class AbstractPdfView extends AbstractView {
private static final int OUTPUT_BYTE_ARRAY_INITIAL_SIZE = 4096;
/**
* This constructor sets the appropriate content type "application/pdf".
* Note that IE won't take much notice of this, but there's not a lot we
* can do about this. Generated documents should have a ".pdf" extension.
*/
public AbstractPdfView() {
setContentType("application/pdf");
}
protected boolean generatesDownloadContent() {
return true;
}
protected final void renderMergedOutputModel(
Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
// The following simple method doesn't work in IE, which
// needs to know the content length.
// PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
// document.open();
// buildPdfDocument(model, document, writer, request, response);
// document.close();
// See http://www.lowagie.com/iText/faq.html#msie
// for an explanation of why we can't use the obvious form above.
// IE workaround: write into byte array first.
ByteArrayOutputStream baos = new ByteArrayOutputStream(OUTPUT_BYTE_ARRAY_INITIAL_SIZE);
Document document = newDocument();
PdfWriter writer = newWriter(document, baos);
// Apply preferences and build metadata.
prepareWriter(model, writer, request);
buildPdfMetadata(model, document, request);
// Build PDF document.
document.open();
buildPdfDocument(model, document, writer, request, response);
document.close();
// Write content type and also length (determined via byte array).
response.setContentType(getContentType());
response.setContentLength(baos.size());
// Flush byte array to servlet output stream.
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
}
/**
* Create a new document to hold the PDF contents.
* <p>By default returns an A4 document, but the subclass can specify any
* Document, possibly parameterized via bean properties defined on the View.
* @return the newly created iText Document instance
* @see com.lowagie.text.Document#Document(com.lowagie.text.Rectangle)
*/
protected Document newDocument() {
return new Document(PageSize.A4);
}
/**
* Create a new PdfWriter for the given iText Document.
* @param document the iText Document to create a writer for
* @param os the OutputStream to write to
* @return the PdfWriter instance to use
* @throws DocumentException if thrown during writer creation
*/
protected PdfWriter newWriter(Document document, OutputStream os) throws DocumentException {
return PdfWriter.getInstance(document, os);
}
/**
* Prepare the given PdfWriter. Called before building the PDF document,
* that is, before the call to <code>Document.open().
* <p>Useful for registering a page event listener, for example.
* The default implementation sets the viewer preferences as returned
* by this class's <code>getViewerPreferences() method.
* @param model the model, in case meta information must be populated from it
* @param writer the PdfWriter to prepare
* @param request in case we need locale etc. Shouldn't look at attributes.
* @throws DocumentException if thrown during writer preparation
* @see com.lowagie.text.Document#open()
* @see com.lowagie.text.pdf.PdfWriter#setPageEvent
* @see com.lowagie.text.pdf.PdfWriter#setViewerPreferences
* @see #getViewerPreferences()
*/
protected void prepareWriter(Map model, PdfWriter writer, HttpServletRequest request)
throws DocumentException {
writer.setViewerPreferences(getViewerPreferences());
}
/**
* Return the viewer preferences for the PDF file.
* <p>By default returns
Other Spring Framework examples (source code examples)Here is a short list of links related to this Spring Framework AbstractPdfView.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.