flying saucer 使用中的一些问题 (java导出pdf)

[url=http://code.google.com/p/flying-saucer/]flying saucer[/url](源代码托管在github [url=https://github.com/flyingsaucerproject/flyingsaucer]https://github.com/flyingsaucerproject/flyingsaucer[/url])是java导出pdf的一种解决方案,最早是从downpour老大的文章里看到它: [url=http://www.iteye.com/topic/509417]http://www.iteye.com/topic/509417[/url] ,感觉比之前的iText好用许多,它可以解析css,即我将页面先设置好,然后传递给它,它既可以给我生成一个pdf出来,跟页面一样,当时感觉很酷,于是就研究了一下,现在项目中也用到了,效果还不错。

优点很明显,之前也提到了,可以解析css,这样很方便,大大的减少了工作量。pdf加水印也变得很简单——只需为body设置一个background-image即可。
说说使用中需要注意的一些问题吧:
[list=1]

[*][color=red][b]中文换行问题[/b][/color]

老外做的东西,没有考虑到中文问题。默认提供的包里,中文不会换行,[url=http://www.iteye.com/topic/509417?page=6#1279762]有人修改了源代码[/url],解决了这个问题,重新编译好的包在附件里,可以下载。需要注意的是,在官网提供的jar包里,有两个包,一个是core-renderer.jar,另一个是core-renderer-minimal.jar。引用时,只需引用前者就行。有人曾经说用这个重新编译后的包替换了原来的包之后,不起作用,原因就在此。

另外,想要中文换行,如果是table,那么table 的style必须加上这句话
style="table-layout:fixed; word-break:break-strict;"


[*][color=red][b]css路径问题[/b][/color]

在一个java project里,使用相对css路径是可以的,效果也都不错。但在java web project里,使用css相对路径是不可以的(最起码这里困扰了我很久,差点就放弃flying saucer了)。例如,我有一个模板叫addOne.jsp,里面引用到了某个css,就应该这样写(windows)


<link href="file:///D|/project/WebContent/commons/css/module-pdf.css" rel="stylesheet" type="text/css" />


只有这样写了之后,它才能找到这个css,很诡异。每次换了机器之后都要改路径,很麻烦。

[*][color=red][b]中文字体问题[/b][/color]

downpour老大在它那篇文章里提到了怎样处理中文字体的,他可能高估了许多人的水平。其实说起来,很简单,就两点:一是在java代码里引用字体,二是在页面上引用字体。
引用字体:


// 解决中文支持问题  
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/Fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);


这里引用了arialuni.ttf字体,它位于C盘windows/fonts文件夹下,将它引用后,还需要在页面上使用这个字体


<body style="font-family:'Arial Unicode MS'">


这里的Arial Unicode MS 即刚才 的arialuni.ttf字体的名字,换了其它字体后要注意修改这里的名称。这样才可以在pdf中显示中文。
[color=red]许多人有这样一个问题——按照以上两个步骤做了之后,页面中还是没有中文,这时,请检查你引用的css文件,其中一定设置了其它字体,只需将它去掉即可[/color]
[/list]
[color=red][b]缺点:[/b][/color]
[color=red]我在使用中发现,flying saucer不支持富文本[/color],如果用到了[url=http://www.kindsoft.net/demo.php]KindEditor[/url]此类富文本编辑器,
还要将其中的内容转化成pdf,那对flying saucer来说就是个灾难。会报一堆错误,目前我还没有找到解决方案。还好这次项目中不是必须使用富文本编辑器,对于有此类需求的同学来说,请慎重选择flying saucer。另外,flying saucer严格遵守html规则,一个小小的错误,都会导致它报错。诸如

<td colspan="2""2">

此类的html代码在jsp中是不会有问题的,可是flying saucer却会报错,曾经这个问题导致我花了一小时时间来寻找问题所在。不过很难说这到底是缺点还是优点

最后贴一个较完整的例子:
我使用spring mvc,在controller里


       
@RequestMapping("/pdf/{projectId}")
public ModelAndView generatePdf(HttpServletRequest request,
HttpServletResponse response, @PathVariable
String projectId) {
Project project = this.projectService.getProjectById(projectId);
ModelAndView mav = new ModelAndView();
if (project == null) {
mav.setViewName("forward:/error/page-not-found");
return mav;
}
//中文需转义
String pdfName = "pdfName";

response.setHeader("Content-disposition", "attachment;filename="+pdfName;
response.setContentType("application/pdf");
OutputStream os = response.getOutputStream();
ITextRenderer renderer = new ITextRenderer();
//指定模板地址
renderer.setDocument("http://localhost/project/preview/"+projectId);

ITextFontResolver fontResolver = renderer.getFontResolver();
if (StringUtils.isOSWindow())
fontResolver.addFont("C:/Windows/Fonts/ARIALUNI.TTF",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
else
fontResolver.addFont("/usr/share/fonts/TTF/ARIALUNI.TTF",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.layout();
renderer.createPDF(os);
os.close();

return null;
}

@RequestMapping("/preview/{projectId}")
public ModelAndView pdf(@PathVariable
String projectId) {
Project project = this.projectService.getProjectById(projectId);
ModelAndView mav = new ModelAndView();
if (project == null) {
mav.setViewName("forward:/error/page-not-found");
return mav;
}
mav.setViewName("pdf");
mav.addObject("project",project);
return mav;
}

jsp页面如下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>title</title>
<link href="file:///D|/project/WebContent/commons/css/print-pdf.css" rel="stylesheet" type="text/css" />

</head>
<body style="font-family:'Arial Unicode MS'">
<table border="1" cellspacing="0" cellpadding="0" class="table" style="table-layout:fixed; word-break:break-strict;">
<tr>
<td rowspan="9" width="4%" class="tc">项目单位基本信息</td>
<td colspan="2" style="width:160px">(1)项目单位名称 </td>
<td colspan="2"><%=StringUtils.getValueString(user.getDeptName()) %></td>
</tr>
</table>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值