Class AbstractPdfView

  • All Implemented Interfaces:
    Aware, BeanNameAware, ApplicationContextAware, ServletContextAware, View

    public abstract class AbstractPdfView
    extends AbstractView
    Abstract superclass for PDF views. Application-specific view classes will extend this class. The view will be held in the subclass itself, not in a template.

    This view implementation uses Bruno Lowagie's iText API. Known to work with the original iText 2.1.7 as well as its fork OpenPDF. We strongly recommend OpenPDF since it is actively maintained and fixes an important vulnerability for untrusted PDF content.

    Note: Internet Explorer requires a ".pdf" extension, as it doesn't always respect the declared content type.

    Author:
    Rod Johnson, Juergen Hoeller, Jean-Pierre Pawlak
    See Also:
    AbstractPdfStamperView
    • Constructor Detail

      • AbstractPdfView

        public AbstractPdfView()
        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.
    • Method Detail

      • renderMergedOutputModel

        protected final void renderMergedOutputModel​(Map<String,​Object> model,
                                                     HttpServletRequest request,
                                                     HttpServletResponse response)
                                              throws Exception
        Description copied from class: AbstractView
        Subclasses must implement this method to actually render the view.

        The first step will be preparing the request: In the JSP case, this would mean setting model objects as request attributes. The second step will be the actual rendering of the view, for example including the JSP via a RequestDispatcher.

        Specified by:
        renderMergedOutputModel in class AbstractView
        Parameters:
        model - combined output Map (never null), with dynamic values taking precedence over static attributes
        request - current HTTP request
        response - current HTTP response
        Throws:
        Exception - if rendering failed
      • newDocument

        protected com.lowagie.text.Document newDocument()
        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.

        Returns:
        the newly created iText Document instance
        See Also:
        Document(com.lowagie.text.Rectangle)
      • newWriter

        protected com.lowagie.text.pdf.PdfWriter newWriter​(com.lowagie.text.Document document,
                                                           OutputStream os)
                                                    throws com.lowagie.text.DocumentException
        Create a new PdfWriter for the given iText Document.
        Parameters:
        document - the iText Document to create a writer for
        os - the OutputStream to write to
        Returns:
        the PdfWriter instance to use
        Throws:
        com.lowagie.text.DocumentException - if thrown during writer creation
      • prepareWriter

        protected void prepareWriter​(Map<String,​Object> model,
                                     com.lowagie.text.pdf.PdfWriter writer,
                                     HttpServletRequest request)
                              throws com.lowagie.text.DocumentException
        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.

        Parameters:
        model - the model, in case meta information must be populated from it
        writer - the PdfWriter to prepare
        request - in case we need locale etc. Shouldn't look at attributes.
        Throws:
        com.lowagie.text.DocumentException - if thrown during writer preparation
        See Also:
        Document.open(), PdfWriter.setPageEvent(com.lowagie.text.pdf.PdfPageEvent), PdfWriter.setViewerPreferences(int), getViewerPreferences()
      • getViewerPreferences

        protected int getViewerPreferences()
        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.

        Returns:
        an int containing the bits information against PdfWriter definitions
        See Also:
        PdfWriter.AllowPrinting, PdfWriter.PageLayoutSinglePage
      • buildPdfMetadata

        protected void buildPdfMetadata​(Map<String,​Object> model,
                                        com.lowagie.text.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().
        Parameters:
        model - the model, in case meta information must be populated from it
        document - the iText document being populated
        request - in case we need locale etc. Shouldn't look at attributes.
        See Also:
        Document.addTitle(java.lang.String), Document.addSubject(java.lang.String), Document.addKeywords(java.lang.String), Document.addAuthor(java.lang.String), Document.addCreator(java.lang.String), Document.addProducer(), Document.addCreationDate(), Document.addHeader(java.lang.String, java.lang.String)
      • buildPdfDocument

        protected abstract void buildPdfDocument​(Map<String,​Object> model,
                                                 com.lowagie.text.Document document,
                                                 com.lowagie.text.pdf.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.

        Parameters:
        model - the model Map
        document - the iText Document to add elements to
        writer - the PdfWriter to use
        request - in case we need locale etc. Shouldn't look at attributes.
        response - in case we need to set cookies. Shouldn't write to it.
        Throws:
        Exception - any exception that occurred during document building
        See Also:
        Document.open(), Document.close()