您的位置:首页 > 其它

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

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: