在springmvc中使用itext生成pdf文件
2017-06-09 10:23
525 查看
感谢此文SpringMVC使用PDF模板生成PDF文件
本文内容多多参考了胡桃的文章
1. 使用Microsoft Office Word画好模板
2. 使用Adobe Acrobat将Word文件转换为带表单字段的PDF模板文件(使用准备表单工具)
3. 使用itext操作PDF模板,填充数据,生成PDF文件
SpringMVC的视图中已提供了对PDF模板文件的支持:org.springframework.web.servlet.view.document.AbstractPdfStamperView。那么只需要配置好此视图就可以了。
时序图
1)pom文件
要说明一点,AbstractPdfStamperView使用的是com.lowagie.itext,比较陈旧的版本,由于要使用中文,需要itextasian包,但是在maven仓库中是下载不到com.lowagie.itext,只有com.itextpdf包。所以要使用maven私服将其加入,否则会报错:Font 'STSongStd-Light' with 'UniGB-UCS2-H' is not recognized
2)TestController
由于springmvc使用了auto-annotation,最好将返回值写成"pdfStamperView",否则可能会报错
app\view\PdfStamperView.class]:Invocation of init method failed; nested exception is java.lang.IllegalArgumentException:Property 'url' is required
3)springmvc xml配置文件
查看AbstractPdfStamperView的代码,可以看到其内部已经使用了PdfReader读取了xml配置文件中的pdf模板。
通过以上步骤就实现了通过SpringMVC加载PDF模板来导出PDF文件的需求
总结:
注意两点:
1. itextasian包的使用
2. 由于springmvc使用了auto-annotation,最好将返回值写成"pdfStamperView",否则可能会报错
本文内容多多参考了胡桃的文章
1. 使用Microsoft Office Word画好模板
2. 使用Adobe Acrobat将Word文件转换为带表单字段的PDF模板文件(使用准备表单工具)
3. 使用itext操作PDF模板,填充数据,生成PDF文件
SpringMVC的视图中已提供了对PDF模板文件的支持:org.springframework.web.servlet.view.document.AbstractPdfStamperView。那么只需要配置好此视图就可以了。
时序图
1)pom文件
<dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>com.lowagie</groupId> <artifactId>itextasian</artifactId> <version>1.5.2</version> <scope>system</scope> <systemPath>F:/jar_package/itextasian-1.5.2.jar</systemPath> </dependency>
要说明一点,AbstractPdfStamperView使用的是com.lowagie.itext,比较陈旧的版本,由于要使用中文,需要itextasian包,但是在maven仓库中是下载不到com.lowagie.itext,只有com.itextpdf包。所以要使用maven私服将其加入,否则会报错:Font 'STSongStd-Light' with 'UniGB-UCS2-H' is not recognized
2)TestController
@RequestMapping("/contract") @Controller public class TestController { @RequestMapping("/export/pdf") public String exportPdf(Model model){ model.addAttribute(DATA,data()); model.addAttribute(FILENAME,"contract"); return "pdfStamperView"; } //准备数据 private Map<String,String> data(){ Map<String,String> data = new HashMap<>(); data.put("borrower","小明"); return data; } }
由于springmvc使用了auto-annotation,最好将返回值写成"pdfStamperView",否则可能会报错
app\view\PdfStamperView.class]:Invocation of init method failed; nested exception is java.lang.IllegalArgumentException:Property 'url' is required
3)springmvc xml配置文件
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> <property name="order" value="1"></property> </bean> <bean id="pdfStamperView" class="com.hfi.itextDemoSpringmvc.view.PdfStamperView"> <property name="url" value="/WEB-INF/template/template.pdf"></property> </bean> <!--<!– jsp视图视图解析器 –>--> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean>4)PdfStamperView
public class PdfStamperView extends AbstractPdfStamperView { public static final String DATA = "data"; public static final String FILENAME = "mergePdfFileName"; @SuppressWarnings("unchecked") @Override protected void mergePdfDocument(Map<String, Object> model, PdfStamper stamper, HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("Content-Disposition", "attachment;filename=" + new String(model.get(FILENAME).toString().getBytes(), "ISO8859-1")); AcroFields fields = stamper.getAcroFields(); //在这个地方可以使用itext api设置填充单元格文本的格式 //填充pdf模板 fillData(fields, (Map<String, String>) model.get(DATA)); stamper.setFormFlattening(true); stamper.close(); } private void fillData(AcroFields fields, Map<String, String> data) throws IOException, DocumentException { for (String key : data.keySet()) { String value = data.get(key); fields.setField(key, value); } } }
查看AbstractPdfStamperView的代码,可以看到其内部已经使用了PdfReader读取了xml配置文件中的pdf模板。
通过以上步骤就实现了通过SpringMVC加载PDF模板来导出PDF文件的需求
总结:
注意两点:
1. itextasian包的使用
2. 由于springmvc使用了auto-annotation,最好将返回值写成"pdfStamperView",否则可能会报错
相关文章推荐
- SpringMVC使用PDF模板生成PDF文件
- itext使用模板生成pdf文件
- 使用iText生成pdf文件
- itext使用模板生成pdf文件
- SpringMVC使用PDF模板生成PDF文件
- SpringMVC使用PDF模板生成PDF文件
- springmvc使用itext生存pdf文件
- Java使用Freemarker和iText生成带有图片的PDF文件
- iTEXT生成PDF文件使用方法 实例 示例 Servlet输出PDF文档方法
- 使用itext组件生成PDF文件
- Android使用iText生成pdf文件并读取pdf内容
- java 使用itextpdf.jar和itext-asian.jar根据pdf模板填写数据后生成一个新的pdf文件
- Itext 生成 pdf文件使用方法:
- 滴水穿石--Java 生成PDF文件--iText使用之插入图片和中文乱码
- 使用iText7----生成纯文本、图片、表格内容的pdf文件
- Java使用Freemarker和iText生成PDF文件
- java使用IText生成PDF格式文件
- 从零开始学习使用iText PDF(1):生成第一个PDF文件
- itext使用html标签生成pdf文件
- 关于Java Web 使用 iText 将数据库中的 表 的数据 生成 PDF 格式文件(升級版本)