Spring MVC生成PDF檔案


以下範例演示如何使用Spring Web MVC框架生成PDF格式的檔案。首先使用Eclipse IDE,並按照以下步驟使用Spring Web Framework開發基於動態表單的Web應用程式:

  1. 建立一個名稱為 GeneratePdf 的動態WEB專案。
  2. com.yiibai.springmvc 包下建立三個Java類:UserPDFViewPDFController
  3. maven儲存庫頁面下載Apache iText庫:Apache iText。 把它放在CLASSPATH中。
  4. 最後一步是建立所有源和組態檔案的內容並執行應用程式,詳細如下所述。

完整的專案檔案目錄結構如下所示 -

PDFController.java 的程式碼如下所示 -

package com.yiibai.springmvc;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class PDFController extends AbstractController {

   @Override
   protected ModelAndView handleRequestInternal(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      //user data
      Map<String,String> userData = new HashMap<String,String>();
      userData.put("100", "Xiao.Lu");
      userData.put("102", "User 102");
      userData.put("301", "User 301");
      userData.put("400", "User 400");
      return new ModelAndView("UserSummary","userData",userData);
   }
}

UserPDFView.java 的程式碼如下所示 -

package com.yiibai.springmvc;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.lowagie.text.Document;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;

public class UserPDFView extends AbstractPdfView {

   protected void buildPdfDocument(Map<String, Object> model, Document document,
      PdfWriter pdfWriter, HttpServletRequest request, HttpServletResponse response)
      throws Exception {
      Map<String,String> userData = (Map<String,String>) model.get("userData");

      Table table = new Table(2);
      table.addCell("No.");
      table.addCell("User Name");

      for (Map.Entry<String, String> entry : userData.entrySet()) {
         table.addCell(entry.getKey());
         table.addCell(entry.getValue());
      }
      document.add(table);
   }
}

GeneratePdf-servlet.xml 組態如下所示 -

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"   
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
   <bean
      class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

   <bean class="com.yiibai.springmvc.PDFController" />

   <bean class="org.springframework.web.servlet.view.XmlViewResolver">
      <property name="location">
         <value>/WEB-INF/views.xml</value>
      </property>
   </bean>
</beans>

views.xml 檔案中的組態如下所示 -

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <bean id="UserSummary" class="com.yiibai.springmvc.UserPDFView"></bean>
</beans>

在上面的程式碼中,建立了一個PDFControllerUserPDFView類。iText庫是用來處理PDF檔案格式,並將資料轉換為PDF文件。

完成建立源和組態檔案後,發布應用程式到Tomcat伺服器。

現在啟動Tomcat伺服器,當存取URL => http://localhost:8080/GeneratePdf/pdf , 如果Spring Web應用程式沒有問題,應該看到以下結果: