Spring MVC生成PDF文件
以下示例演示如何使用Spring Web MVC框架生成PDF格式的文件。首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序:
- 创建一个名称为 GeneratePdf 的动态WEB项目。
- 在
com.yiibai.springmvc包下创建三个Java类:UserPDFView和PDFController。 - 从maven存储库页面下载Apache iText库:Apache iText。 把它放在
CLASSPATH中。 - 最后一步是创建所有源和配置文件的内容并运行应用程序,详细如下所述。
完整的项目文件目录结构如下所示 -

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>
在上面的代码中,创建了一个PDFController和UserPDFView类。iText库是用来处理PDF文件格式,并将数据转换为PDF文档。
完成创建源和配置文件后,发布应用程序到Tomcat服务器。
现在启动Tomcat服务器,当访问URL => http://localhost:8080/GeneratePdf/pdf , 如果Spring Web应用程序没有问题,应该看到以下结果:

Spring MVC生成PDF文件的更多相关文章
- 在spring boot 中使用itext和itextrender生成pdf文件
转载请注明出处 https://www.cnblogs.com/majianming/p/9539376.html 项目中需要对订单生成pdf文件,在第一版本其实已经有了比较满意的pdf文档,但是还是 ...
- spring mvc: 生成RSS源
spring mvc: 生成RSS源 准备: 从相同的maven存储库页面下载 Rome 库及其依赖项rome-utils,jdom和slf4j.和所需的依赖关系 <!-- rss源依赖 --& ...
- Spring MVC上传文件
Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...
- linux下编译bib、tex生成pdf文件
实验: 在linux环境下,编译(英文)*.bib和*.tex文件,生成pdf文件. 环境: fedora 20(uname -a : Linux localhost.localdomain 3.19 ...
- ThinkPHP3.2.3扩展之生成PDF文件(MPDF)
目前是PHP生成PDF文件最好的插件了,今天介绍下在ThinkPHP3.2.3里如何使用. 先安照路径放好如图. 下面是使用方法 public function pdf(){ //引入类库 Vendo ...
- Spring MVC 上传文件
Spring MVC上传文件需要如下步骤: 1.前台页面,form属性 method设置为post,enctype="multipart/form-data" input的typ ...
- [轉載]史上最强php生成pdf文件,html转pdf文件方法
之前有个客户需要把一些html页面生成pdf文件,然后我就找一些用php把html页面围成pdf文件的类.方法是可谓是找了很多很多,什么html2pdf,pdflib,FPDF这些都试过了,但是都没有 ...
- asp.net生成PDF文件 (1)
asp.net生成PDF文件 (1) 这个是例子是网上淘来的,哈哈,很有用的! 首先要到网上下载itextsharp.dll,然后添加引用,主程序如下: 1 2 3 4 5 6 7 8 9 10 11 ...
- 怎么用PHP在HTML中生成PDF文件
原文:Generate PDF from html using PHP 译文:使用PHP在html中生成PDF 译者:dwqs 利用PHP编码生成PDF文件是一个非常耗时的工作.在早期,开发者使用PH ...
随机推荐
- 【报错】引入jar包import org.apache.commons.codec.digest.DigestUtils 报错,jar不存在
import org.apache.commons.codec.digest.DigestUtils报错.缺少jar maven引用jar包地址: <!-- https://mvnreposit ...
- linux下GPRS模块的应用程序
---------------------------------------------------------------------------------------------------- ...
- centos7 安装nginx与配置
第一步安装 使用Yum安装是推荐的方式,整体的流程非常的简单,也不容易出错,如果不需要什么特殊配置,建议使用Yum尽进行安装. 第一种安装方式,通过添加epel源 yum install epel-r ...
- EL表达式介绍(2)
1. EL关系运算符: 关系运算符 说明 范例 结果 == 或 eq 等于 ${5==5}或${5eq5} true != 或 ne 不等于 ${5!=5}或${5ne5} false < 或 ...
- maven项目用assembly打包可执行jar包
该方法只可打包非spring项目的可执行jar包,spring项目可参考:http://www.cnblogs.com/guazi/p/6789679.html 1.添加maven插件: <!- ...
- JAVA加解密 -- 对称加密算法与非对称加密算法
对称加密算法:双方必须约定好算法 DES 数据加密标准:由于不断地被破解 自98年起就已经逐渐放弃使用 AES 目前使用最多的加密方式,官方并未公布加密方式已被破解,替代DES 实现和DES非常接近 ...
- css处理超出文本截断问题的两种情况(多行或者单行)
1.非多行的简单处理方式: css代码: .words{ width:400px; overflow:hidden; /*超过部分不显示*/ text-overflow:ellipsis; /*超过部 ...
- How to learn a new technology
是什么?为什么会出现? 这一阶段主要是对该技术有一个整体了解,他所解决的是什么问题,他的整体结构等. 怎么做? 最简单的是找一个上手视频,因为视频是非常直观的展示了技术的使用.先学会用是最根本的,对于 ...
- Sql Server2005 Synonyms
1. 同义词(SYNONYM)是SQL Server 2005中新特性 它是一种对已有的或潜在的新对象给予的别名.可以在同一个数据库或者跨数据中中使用这个别名,这个别名替代了原有对象.可以建别名的对象 ...
- 分别在.NET Framework 与 .NET Core 框架下 编写Windows Service(windows服务程序)
前言,为什么会分别在两个框架下编写Windows Service,是因为最近在做区块链这块,使用的是NEO(小蚁区块链)的相关技术,NEO使用的是.net core 2.1,业务上需要写两个程序,一个 ...