关于通过iText通过HTML转成PDF导出的操作遇到一些问题的记录

最近做项目,有一个pdf导出的操作,是通过html转成pdf做的导出,遇到的一些问题分享出来,希望大家以后做导出pdf的时候注意:

第一:html导出为pdf是通过XMLWorkerHelper.getInstance().parseXHtml(pdfwriter, document, new StringReader(html));这个方法导出的,但是XMLWorkerHelper并不支持所有的css属性,具体支持的属性的参看:

http://demo.itextsupport.com/xmlworker/itextdoc/CSS-conformance-list.htm

第二:用html作为模版导出的时候,会出现一行分两页的情况(就是在一页底部显示不全一行会继续显示到下一页),出现这种情况,最好的办法还是通过java 代码画出表格进行导出。我试验了freemarker做导出效果还是不理想。

第三:关于控制导出表格列的宽度的设置的说明:

先说参考资料:

http://www.it610.com/article/98479.htm

说的是这样:

2. 如何设置table的列宽
不要在td的width或者style属性上白费力气了,要想设置table的列宽,正确的做法是指定table的2个非html标准属性columns和widths:

<table border="0" width="100%" columns="3" widths="50;10;40">
下面再看一个资料:
http://unmi.cc/itext-htmlworker-html2pdf-table-cellwidth/
这个说明iText5.0.4还是不支持控制列的宽度的,只能通过修改源码来实现
再看一个资料
http://unmi.cc/itext5-0-6-htmlworker-html-pdf-support-cell-width/
到了iText5.0.6就支持控制列宽了,直接通过width:xx%;来控制。
但是这里还有一定局限性。
看下面一段代码:
<pre name="code" class="html"><style >
     body{font-family:宋体;}
   table{border-collapse:collapse;border-spacing:0;border:0;border-style:solid;}
   tbody td{border-style:solid;border-right-width:.5pt solid #000;border-bottom-width:.5pt solid #000;text-align: center;font-size:7pt;}
   .leftTd{border-left-width-width:.5pt solid #000;}
   .topTd{border-top-width-width:.5pt solid #000;}
   #myTable .widthCss1{width:5%;}
   #myTable .widthCss2{width:25%;}
   #myTable .widthCss3{width:15%;}
   #myTable .widthCss4{width:15%;}
   #myTable .widthCss5{width:15%;}
   #myTable .widthCss6{width:25%;}
</style>
<table  width="100%" >
 <tr >
  <td colspan="7" height=72   style='height:54.0pt;width:100%;text-align:center;'>ssss</td>
 </tr>
 <tr>
   <td colspan="7" height=58 style='height:43.5pt;width:692pt'>
	   <table style="width:100%;font-size:14px;font-weight: bold;">
	   <tr><td colspan="6"> </td></tr>
	   <tr>
	   <td style="text-align:left;width:15%;">xxx:</td> <td style="text-align:left;width:15%;"></td>
<td style="text-align:left;width:15%;">xxx:</td><td style="text-align:left;width:15%;">
</td><td style="text-align:left;width:18%;">xxx签字:</td><td style="text-align:left;width:20%;"></td>
	   </tr>
	   <tr>
	   <td colspan="6" style="text-align:left;height:40px; ">单位(盖章):</td>
	   </tr>
	   </table>
  </td>
 </tr>
</table>
<table  width="100%" id="myTable" >
<tbody>
 <tr>
  <td style='border-left-width-width:.5pt solid #000;border-top-width-width:.5pt solid #000;height:21.0pt;width:5%;'>序号</td>
  <td  style='border-left-width:none;border-top-width-width:.5pt solid #000;width:10%;' >xx</td>
  <td  style='border-left-width:none;border-top-width-width:.5pt solid #000;width:15%;' >xx</td>
  <td  style='border-left-width:none;border-top-width-width:.5pt solid #000;width:15%;' >xx</td>
  <td style='border-left-width:none;border-top-width-width:.5pt solid #000;width:15%;' >xx</td>
  <td style='border-left-width:none;border-top-width-width:.5pt solid #000;width:15%;' >xx</td>
  <td style='border-left-width:none;border-top-width-width:.5pt solid #000;width:25%;' >xx</td>
 </tr>
 <tr>
  <td height=28   style='border-top-width:none;height:21.0pt;' class="leftTd widthCss1">1</td>
  <td colspan=2   style='border-left-width:none;border-top-width-width:none;' class="widthCss2">一、课桌椅(单人套)</td>
  <td  style='border-left-width:none;border-top-width-width:none;' class="widthCss3">xx</td>
  <td  style='border-left-width:none;border-top-width-width:none;' class="widthCss4">xx</td>
  <td  style='border-left-width:none;border-top-width-width:none;' class="widthCss5">xx</td>
  <td  style='border-left-width:none;border-top-width-width:none;' class="widthCss6">xx</td>
 </tr>
</tbody>
</table>
由于第一个table是标题备份我在style里直接写样式的时候,不起作用,所以,在这里控制列宽的时候最好是单独一个table
 
进行控制。不要在一个table里写其他的table,不然不会起作用。
最后还是一个参考资料:
http://wenku.baidu.com/link?url=GZ2vpvRHCyoLvy3OiMkQASvtFrxzFcvpu2Uk-pVsazFsYwx38b9YNNzUvbY5apqs6xV_YQV2E3y52tnjyigPhaCEm858aEr_Mq7lFXA5ADO


 
如果写的有什么不对的地方欢迎大家指正.
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值