代码display:exportPDFHelper
//调用velocity engine来融合数据和vm模板 public String mergeMessage(String vmPath, Map map) { String text = VelocityEngineUtils.mergeTemplateIntoString(exportPDFVelocityEngine, vmPath, map); return text; } public void exportPDF(HttpServletResponse response,String fileName, Map<String,Object> map,String template) { try{ OutputStream os = response.getOutputStream();// 取得输出流 response.reset();// 清空输出流 System.out.println(fileName); response.setHeader("Content-disposition", "attachment; filename="+URLEncoder.encode(fileName,"UTF-8")+".pdf");// 设定输出文件头 response.setContentType("application/pdf");// 定义输出类型 ITextRenderer renderer=new ITextRenderer(); ITextFontResolver fontResolver=renderer.getFontResolver(); //设置中文字体路径 中文支持这块我也没整太明白 我看网上说的是支持中文 就要在这里添加中文字体且网页上//也要加上css font引用添加的这个字体 但是我没在网页上引用这个添加的字体也显示中文了 fontResolver.addFont("C:/Windows/Fonts/simsunb.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); fontResolver.addFont("C:/Windows/Fonts/arial.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); String content =mergeMessage(template,map); //传入最终的包含css的html代码给flying sauser 去解析啦 renderer.setDocumentFromString(content); //设置图片的基础路径 renderer.getSharedContext().setBaseURL("file:/D:/apache-tomcat-7.0.68-windows-x64/apache-tomcat-7.0.68/webapps/report/styles/"); //支持BASE64图片 renderer.getSharedContext().setReplacedElementFactory(new B64ImgReplacedElementFactory()); renderer.layout(); renderer.createPDF(os); os.close(); }catch (Exception ex) { ex.printStackTrace(); result.setSuccess(false); result.setResultInfo(ex.getMessage()); } } <beans default-autowire="byName"> <bean id="exportPDFHelper" class="com.takungae.report.common.utils.ExportPDFHelper"> <property name="exportPDFVelocityEngine" ref="exportPDFVelocityEngine"></property> </bean> <bean id="exportPDFVelocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> <property name="resourceLoaderPath" value="/WEB-INF/pdf-template" /> <property name="velocityProperties"> <props> <prop key="input.encoding">GB18030</prop> <prop key="output.encoding">UTF-8</prop> </props> </property> </bean> </beans> 导出为pdf示例图感谢以下几篇文章的指引 (http://blog.csdn.net/hybaym/article/details/52999702) (http://blog.sina.com.cn/s/blog_816730ea010hxxb.html) (http://blog.csdn.net/woshizhangliang999/article/details/45197877) (http://blog.csdn.net/jasonchris/article/details/7705631) (http://adminjun.iteye.com/blog/2274861)