最近做项目,有一个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%" >
  <td colspan="7" height=72   style='height:54.0pt;width:100%;text-align:center;'>ssss</td>
   <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>
	   <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>
	   <td colspan="6" style="text-align:left;height:40px; ">单位(盖章):</td>
	   </table>
</table>
<table  width="100%" id="myTable" >
<tbody>
  <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>
  <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>
</tbody>
</table>
由于第一个table是标题备份我在style里直接写样式的时候,不起作用,所以,在这里控制列宽的时候最好是单独一个table
进行控制。不要在一个table里写其他的table,不然不会起作用。
最后还是一个参考资料:
http://wenku.baidu.com/link?url=GZ2vpvRHCyoLvy3OiMkQASvtFrxzFcvpu2Uk-pVsazFsYwx38b9YNNzUvbY5apqs6xV_YQV2E3y52tnjyigPhaCEm858aEr_Mq7lFXA5ADO
2、附字体
3、动态html拼接转pdf
public static void htmlCodeComeString(String linkcss,String htmlCode, String outputFile,String title) throws Exception {  
        OutputStream os = new FileOutputStream(outputFile);  
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocumentFromString(getConversionHtmlCode(linkcss,htmlCode,title));
        ITextFontResolver fontResolver = renderer.getFontResolver();
        URL fontPath = ItextUtil.class.getResource("simsun.ttc");
        fontResolver.addFont(fontPath.toString(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        // 解决图片的相对路径问题  
        // renderer.getSharedContext().setBaseURL("file:/F:/teste/html/");  
        renderer.layout();  
        renderer.createPDF(os);
        System.out.println("======转换成功!");
        os.close();
        os.flush();
    public static void main(String[] args)
        ItextUtil itextUtil = new ItextUtil();
        String html = "";
        html += "";
        html += "企业信息";
        html += "   ";
        html += "   ";
        html += "       登记日期";
        html += "       2006-04-28";
        html += "   ";
        html += "   ";
        html += "       纳税人编号";
        html += "       HSJIHKS002";
        html += "   ";
        html += "   ";
        html += "       有效标志";
        html += "       Y";
        html += "   ";
        html += "   ";
        html += "       社会信用代码";
        html += "       916101317H";
        html += "   ";
        html += "   ";
        html += "       评估机关代码";
        html += "       盛世";
        html += "   ";
        html += "   ";
        html += "       工商注销日期";
        html += "       2006-04-28";
        html += "   ";
        html += "   ";
        html += "";
        String outputFile = "D:\\pdf\\aa.pdf";
            itextUtil.htmlCodeComeString("",html,outputFile,"");
        catch (Exception e)
            // TODO Auto-generated catch block
            e.printStackTrace();
        System.out.println("生成结束!!!");
				
这样的BUG比较小众,项目里面用到导出PDF的功能也不多,接触的人也不多。所以网上的资料也不多,整理出来以便给后来者一个参考,希望能帮到你! (关注一下博主,获取更多文章推送) BUG描述 我主要是将包含有< pre>标签和< code>标签的HTML文章转换成PDF,而code标签里面放置的是java代码,代码如下: public Object authentication(...){ // "/**"表示匹配所有
itext实现HTML转换为PDF 1、首先,建立一个maven工程(maven工程的建立这里不在演示)    1.1 在maven工程的pom.xml中加入itext所需的jar包 com.lowagie itext 2.0.8 ```java import com.itextpdf.io.image.ImageData; import com.itextpdf.io.image.ImageDataFactory; import com.itextpdf.kernel.geom.PageSize; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Image; import java.io.FileOutputStream; import java.io.IOException; public class ImageToPdfConverter { public static void main(String[] args) { byte[] imageBytes = getImageBytes(); // 替换为获取图片字节数组的方法 String pdfPath = "path/to/save/pdf.pdf"; try { convertImageBytesToPdf(imageBytes, pdfPath); System.out.println("Image converted to PDF successfully!"); } catch (IOException e) { e.printStackTrace(); public static void convertImageBytesToPdf(byte[] imageBytes, String pdfPath) throws IOException { FileOutputStream fos = new FileOutputStream(pdfPath); PdfWriter writer = new PdfWriter(fos); PdfDocument pdfDocument = new PdfDocument(writer); Document document = new Document(pdfDocument, new PageSize(600, 800)); // 自定义页面大小 ImageData imageData = ImageDataFactory.create(imageBytes); Image image = new Image(imageData); document.add(image); document.close(); fos.close(); public static byte[] getImageBytes() { // 返回图片的字节数组 return null; 在上面的代码中,你需要将 `getImageBytes()` 方法替换为获取图片字节数组的方法。然后,将字节数组传递给 `convertImageBytesToPdf()` 方法来创建一个包含图片的 PDF 文件。 在 `convertImageBytesToPdf()` 方法中,我们使用 `new PageSize(600, 800)` 来自定义 PDF 文件的页面大小。在这个示例中,我们设置页面大小为宽度 600 像素,高度 800 像素。你可以根据需要自定义页面大小。 希望这对你有所帮助!如果还有其他问题,请随时提问。
2. 如何设置table的列宽 不要在td的width或者style属性上白费力气了,要想设置table的列宽,正确的做法是指定table的2个非html标准属性columns和widths: <table border="0" width="100%" columns="3" widths="50;10;40"> 解决了我的问题 关于Eclipse-JUNO安装Maven失败的总结 版本对应,着实捉急 jsp 应用kindeditor4.1.7 时的一些配置问题 qq_39194387: 图片能上传成功,但在文本框内不显示,这是什么原因, 关于通过iText通过HTML转成PDF导出的操作遇到一些问题的记录 lei_1994: 博主 遇到过XMLWorkerHelper.getInstance().parseXHtml(pdfwriter, document, new StringReader(html)); 解析非常慢的情况吗??? 十页用了快五分钟的时间 关于通过iText通过HTML转成PDF导出的操作遇到一些问题的记录 赤炎大帅: 看了xmlworker的源码,发现它对很多css属性都不支持或者解析的不好,它是通过状态机模式解析一个个Tag并找到Tag的css,然后使用pipeline模式分段处理的。