itext+Flying Saucer生成pdf

生成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拷贝到项目下

		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>







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值