關於通過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

如果寫的有什麼不對的地方歡迎大家指正.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章