PDF and Excel

Spring 提供了除 HTML 之外的其他方式,包括 PDF 和 Excel 电子表格。本节介绍了如何使用这些功能。

Spring 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

对于用户来说,HTML 页面并非总是查看模型输出的最佳方法,而 Spring 可以轻松地基于模型数据动态生成 PDF 文档或 Excel 电子表格。文档是视图,并带有正确的 ContentType 从服务器中传输,以便(希望)使客户端电脑响应可以运行自己的电子表格或 PDF 查看器应用程序。

An HTML page is not always the best way for the user to view the model output, and Spring 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.

为了使用 Excel 视图,您需要将 Apache POI 库添加到您的类路径。对于 PDF 生成,您需要添加(最好是)OpenPDF 库。

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.

您应该使用最新版本的底层文档生成库,如果可能的话。特别地,我们强烈建议使用 OpenPDF(例如 OpenPDF 1.2.12),而不是过时的原始 iText 2.1.7,因为 OpenPDF 积极维护并且修复了对不受信任的 PDF 内容的重要漏洞。

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

单词列表中的简单 PDF 视图可以扩展`org.springframework.web.servlet.view.document.AbstractPdfView`并实现`buildPdfDocument()`方法,如下例所示:

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

  • Java

  • Kotlin

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));
		}
	}
}
class PdfWordList : AbstractPdfView() {

	override fun buildPdfDocument(model: Map<String, Any>, doc: Document, writer: PdfWriter,
			request: HttpServletRequest, response: HttpServletResponse) {

		val words = model["wordList"] as List<String>
		for (word in words) {
			doc.add(Paragraph(word))
		}
	}
}

控制器既可以从外部视图定义返回此视图(以名称引用),也可以从处理程序方法作为 View 实例返回。

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

由于 Spring 框架 4.2,org.springframework.web.servlet.view.document.AbstractXlsView 被用作 Excel 视图的基类。它基于 Apache POI,具有特殊子类(AbstractXlsxViewAbstractXlsxStreamingView),它们取代了过时的 AbstractExcelView 类。

Since Spring Framework 4.2, org.springframework.web.servlet.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.

编程模型与 AbstractPdfView 类似,其中 buildExcelDocument() 作为中心模板方法,并且控制器能够从外部定义(按名称)或从处理程序方法返回这样的视图。

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.