生成 pdf 的方式有多种,直接用 iText 编码控制产生,fop 转换,不过我觉得很方便的一种方法就是用 iText 的 HTMLWorker 类直接把 HTML 转换成相应的 PDF 文档,HTMLWorker 可支持图片和表格,一定数量的样式控制。这样可以让我们关注到 HTML 页面设计上,至于转换出来的效果就要看母版 HTML 的设计功底了。要知道这个 HTML 中的样式要让 HTMLWorker 可理解的,并非说能支持 CSS2 还是 CSS3。HTMLWorker 能支持的样式还是非常有限,并且还未文档化的,有些多试试,能看看源代码是自然可以让胸中更有数了。
项目中有一个比较迫切的问题是把 HTML 中的表格转换成 PDF 的 PdfPTable 时,无论是在
上用 width="xxx" 或是 style="width:xxx" 时都看不到宽度的设置效果,iText 总是让生成的 PdfPTable 平均分配单元格宽度的。这对于单元格宽度有过于悬殊的需求是无法妥协的,所以要一种办法来支持自定义单元格宽度,让 width="xxx" 或是 style="width:xxx" 有一种控制能力就行。又得改源代码了,网上找到了是支持 iText 2.1.7 的改法,本篇是针对 iText 5.0.4 的做法,使支持 width="10%" 百分比来控制单元格宽度。需要改动的文件有三个,全是在包 com.itextpdf.text.html.simpleparser 中的,HTMLWorker.java、IncTable.java 和 IncCell.java。我把改动的地方用 diff 生成的 patch 文件列出来吧,可以照着改,或是直接 patch。后面修改后的代码还提供下载,把类放在自己项目中的 src 目录中即可,想拉进原来的 iText.jar 包亦可。
HTMLWorker.java.patch:
50d49
< import java.util.Collections;
586d584
< ArrayList<Integer> cellWidths = new ArrayList<Integer>();
591,598c589
< //cells.add(((IncCell) obj).getCell());
< IncCell cell = (IncCell)obj;
< if(cell.getWidth() != null){
< cellWidths.add(cell.getWidth());
< }
<
< cells.add(cell.getCell());
<
---
> cells.add(((IncCell) obj).getCell());
606,613d596
< if(cellWidths.size()>0){
< Collections.reverse(cellWidths);
< int[] widths = new int[cellWidths.size()];
< for(int i=0;i<widths.length;i++){
< widths[i] = cellWidths.get(i).intValue();
< table.setColWidths(widths);
< }
< }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
50d49
<importjava.util.Collections;
586d584
< ArrayList<Integer>cellWidths=newArrayList<Integer>();
591,598c589
< //cells.add(((IncCell) obj).getCell());
< IncCellcell=(IncCell)obj;
< if(cell.getWidth()!=null){
< cellWidths.add(cell.getWidth());
< }
<
< cells.add(cell.getCell());
<
---
> cells.add(((IncCell)obj).getCell());
606,613d596
< if(cellWidths.size()>0){
< Collections.reverse(cellWidths);
< int[]widths=newint[cellWidths.size()];
< for(inti=0;i<widths.length;i++){
< widths[i]=cellWidths.get(i).intValue();
< table.setColWidths(widths);
< }
< }
IncTable.java.patch
49d48
< import java.util.List;
51d49
< import com.itextpdf.text.DocumentException;
66,67d63
< private int[] colWidths;
<
98,105d93
< public int[] getColWidths(){
< return colWidths;
< }
<
< public void setColWidths(int[] colWidths){
< this.colWidths = colWidths;
< }
<
130,136d117
< try{
< if(colWidths != null){
< table.setWidths(colWidths);
< }
< }catch(DocumentException e){
< e.printStackTrace();
< }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
49d48
<importjava.util.List;
51d49
<importcom.itextpdf.text.DocumentException;
66,67d63
< privateint[]colWidths;
<
98,105d93
< publicint[]getColWidths(){
< returncolWidths;
< }
<
< publicvoidsetColWidths(int[]colWidths){
< this.colWidths=colWidths;
< }
<
130,136d117
< try{
< if(colWidths!=null){
< table.setWidths(colWidths);
< }
< }catch(DocumentExceptione){
< e.printStackTrace();
< }
IncCell.java.patch
63d62
< private Integer width;
106,109d104
< value = props.getProperty("width");
< if(value != null && value.endsWith("%")){
< width = new Integer(value.replace("%",""));
< }
148,155d142
<
< public Integer getWidth(){
< return width;
< }
<
< public void setWidth(Integer width){
< this.width = width;
< }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
63d62
< privateIntegerwidth;
106,109d104
< value=props.getProperty("width");
< if(value!=null&&value.endsWith("%")){
< width=newInteger(value.replace("%",""));
< }
148,155d142
<
< publicIntegergetWidth(){
< returnwidth;
< }
<
< publicvoidsetWidth(Integerwidth){
< this.width=width;
< }
注意下,这是针对 iText 5.0.4 的版本,其他的版本可能有差异,打完补丁后给
设置这样的百分比宽度就可以在 PDF 中看到效果的。注意,你可以只给第一个 下的每一个 设置宽度,用百分比的。而且必须是第一行中每个 都要 width 属性。如果在 中某个 设置了 width 属性,其他的 也必须要,不然会出现空指针错误。