itext+Flying Saucer生成pdf
2015-07-21 13:49
405 查看
生成pdf有多种方式,这里附上itext+Flying Saucer方式生成pdf并下载,也会在另一篇文章中给出使用itext手动绘画pdf的工具类,不过不建议这么写,使用itext话样式不好控制,另外如果特别复杂的pdf,虽然也能搞出来,不过那家伙,真是谁用谁知道哇
①:准备html模板文件,写好样式
注意:html必须以
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 开头
②:struts2 下载pdf源码(需要中文字体支持,在C:\Windows\Fonts找到字体文件simsun.ttc拷贝到项目下)
struts的xml配置
注意:由于每次下载时都会在服务器上重新生成一个pdf文件,所以这个pdf需要另外的程序去清理,这里就不写了用定时器去解决就好了,生成的pdf你会发现文字都不会换行
这时需要替换jar包需要替换的jar包下载地址,如果pdf中有数字太长不能换行的,我是直接用ftl标签进行截取的(),如果哪位仁兄知道其他解决方法,望告诉一声,跪谢。
附上截取ftl用法
①:准备html模板文件,写好样式
注意:html必须以
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 开头
②:struts2 下载pdf源码(需要中文字体支持,在C:\Windows\Fonts找到字体文件simsun.ttc拷贝到项目下)
OutputStream out = null; try { HttpServletRequest request = ServletActionContext.getRequest(); String xmlPath = gBasicService.queryXmlPathByRegno(regno); Map dataMap = getTemplateMap("1",xmlPath); // classpath 路径 String outputFileClass = com.ccx.org.util.ResourceLoader.getPath(""); //pdf 存放路径 String dateStr = DateUtil.toString(new Date(), 10); String outputFile = new File(outputFileClass).getParentFile().getParent()+ File.separator+"tmp"+File.separator+dateStr+File.separator+ System.currentTimeMillis() + ".pdf" ; File ff = new File(new File(outputFileClass).getParentFile().getParent()+ File.separator+"tmp"+File.separator+dateStr); if(!(ff.exists() && ff.isDirectory())){ ff.mkdirs(); } // 生成pdf路径 outputFile = outputFile == null ? new File(outputFileClass).getParentFile().getParent()+ "/tmp/"+dateStr+"/"+System.currentTimeMillis() + ".pdf" : outputFile; String templatePath = ServletActionContext.getServletContext().getRealPath("/template/html/"); String templateName = "org_pdf.html"; HtmlGenerator htmlGenerator = new HtmlGenerator(); String htmlContent = htmlGenerator.generate(templatePath,templateName,dataMap); out = new FileOutputStream(outputFile); ITextRenderer renderer = new ITextRenderer(); ITextFontResolver fontResolver = renderer.getFontResolver(); fontResolver.addFont(outputFileClass+"/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); renderer.setDocumentFromString(htmlContent); renderer.layout(); renderer.createPDF(out); File file = new File(outputFile); fileIns = new FileInputStream(file); fileName = "企业信息_" + DateUtil.toStringNoInterval(new Date(), 8)+ ".pdf"; fileName = DownLoadUtil.transFileName(fileName, request); } catch (Exception e) { e.printStackTrace(); } finally{ if(null != out){ try { out.flush(); out.close(); } catch (IOException e) { log.error("GBasicAction expOrgInfoToPdf error", e); } } } return SUCCESS;
struts的xml配置
<action name="****" class="com.***.org.action.***" method="****"> <result name="success" type="stream"> <param name="contentType">application/pdf</param> <param name="contentDisposition">attachment;filename="${fileName}"</param> <param name="bufferSize">1024</param> <param name="inputName">fileIns</param> </result> </action>
注意:由于每次下载时都会在服务器上重新生成一个pdf文件,所以这个pdf需要另外的程序去清理,这里就不写了用定时器去解决就好了,生成的pdf你会发现文字都不会换行
这时需要替换jar包需要替换的jar包下载地址,如果pdf中有数字太长不能换行的,我是直接用ftl标签进行截取的(),如果哪位仁兄知道其他解决方法,望告诉一声,跪谢。
附上截取ftl用法
<#if item.regno ??> ${item.regno?substring(0, 7)}<br/>${item.regno?substring(7)} <#else> ${item.regno!''} </#if>
相关文章推荐
- Google Chrome中的高性能网络(二)
- Android Dialog.dismiss()与Activity.finish()顺序
- 关于Breeze.js+Angular.js+KendoUI+BootStrap+TypeScript+EF4.5的使用心得记录之一
- 【网络】 NAT
- GRE写作必备句型
- 15. Magento路由分发过程解析(四):请求重写
- SlidingMenu (一)在Eclipse中如何引入SlidingMenu和其Demo
- UITableviewCell滑动出现多级的控制按钮 - UITableViewRowAction
- 跨浏览器的事件处理程序
- Ubuntu14.04设置自定义桌面分辨率
- 获取imageview里的Bitmap
- HOG:用于人体检测的梯度方向直方图 Histograms of Oriented Gradients for Human Detection
- 修改eclipse启动时eclipse使用的jre
- 在C#中使用WIA获取扫描仪数据
- 【SSH异常系列】关于SSH中连接数溢出问题的解决方法
- 脑法之一 --- DEBUG与搜索算法
- 单例模式中的Double check lock
- 连续值分组
- NSInvocation简单使用
- 傅盛