PDF and Excel

Infra offers ways to return output other than HTML, including PDF and Excel spreadsheets. This section describes how to use those features.

Introduction to Document Views

An HTML page is not always the best way for the user to view the model output, and Infra makes it simple to generate a PDF document or an Excel spreadsheet dynamically from the model data. The document is the view and is streamed from the server with the correct content type, to (hopefully) enable the client PC to run their spreadsheet or PDF viewer application in response.

In order to use Excel views, you need to add the Apache POI library to your classpath. For PDF generation, you need to add (preferably) the OpenPDF library.

You should use the latest versions of the underlying document-generation libraries, if possible. In particular, we strongly recommend OpenPDF (for example, OpenPDF 1.2.12) instead of the outdated original iText 2.1.7, since OpenPDF is actively maintained and fixes an important vulnerability for untrusted PDF content.

PDF Views

A simple PDF view for a word list could extend cn.taketoday.web.view.document.AbstractPdfView and implement the buildPdfDocument() method, as the following example shows:

  • Java

public class PdfWordList extends AbstractPdfView {

  protected void buildPdfDocument(Map<String, Object> model, Document doc, PdfWriter writer,
      HttpServletRequest request, HttpServletResponse response) throws Exception {

    List<String> words = (List<String>) model.get("wordList");
    for (String word : words) {
      doc.add(new Paragraph(word));
    }
  }
}

A controller can return such a view either from an external view definition (referencing it by name) or as a View instance from the handler method.

Excel Views

Since TODAY Framework 4.2, cn.taketoday.web.mock.view.document.AbstractXlsView is provided as a base class for Excel views. It is based on Apache POI, with specialized subclasses (AbstractXlsxView and AbstractXlsxStreamingView) that supersede the outdated AbstractExcelView class.

The programming model is similar to AbstractPdfView, with buildExcelDocument() as the central template method and controllers being able to return such a view from an external definition (by name) or as a View instance from the handler method.