无限 table 循环;行内容大于页面(例如因为您没有缩放图像)

Infinite table loop; row content is larger than the page (for instance because you didn't scale an image)

我使用 Itext 库将 html 中的 table 转换为 pdf 并出现 "Infinite table loop; row content is larger than the page (for instance because you didn't scale an image)" 错误。请在下面找到我的代码。

public static File createPDF(File outputDir, Context context) {
    Document document = null;
    String htmlStr = "<table><tr><td><div class="two-col-pair-one"><div align="center">1:  9_ITEM</div><div class="row-odd">
<div class="packet-font">
<div class="two-col">Cust. Name</div>
<div class="two-col">Gg</div>
</div>
<div style="clear:both"></div>
</div>
<div class="row-even">
<div class="packet-font">
<div class="two-col">Org. City</div>
<div class="two-col">D</div>
</div>
<div style="clear:both"></div>
</div>
<div class="row-odd">
<div class="packet-font">
<div class="two-col">Dest. City</div>
<div class="two-col">D</div>
</div>
<div style="clear:both"></div>
</div>
<div class="row-even">
<div class="packet-font">
<div class="two-col">Product Type</div>
<div class="two-col">Domestic Moving</div>
</div>
<div style="clear:both"></div>
</div>
<div class="row-odd">
<div class="packet-font">
<div class="two-col">Trans.Mode</div>
<div class="two-col">Road</div>
</div>
<div style="clear:both"></div>
</div>
<div class="row-even">
<div class="packet-font">
<div class="two-col">Description</div>
<div class="two-col">Fridge - Regular</div>
</div>
<div style="clear:both"></div>
</div>
</div>
<div class="two-col-pair-two"></div>
</td></tr></table>";
     File pdfFile = null;
    String CSS =    body
{
 font-family: Verdana;
 font-size: 13px;
 font-weight: bold;
}
@page {
   size: A4;
}
@page {
 margin: 0px;
}
@page :left {
 margin: 0px;
}
@page :right {
 margin: 0px;
}
@page :right {
 margin: 0px;
}
.page-break{
   page-break-after: always;
}
table{
   border-collapse: collapse;
}
table, td, th{
   border: 3px dotted black;
   width: auto
}
td{
   height: 5.75cm;
   width: 5.1cm;
}
tr{
   page-break-inside:avoid;
   page-break-after:auto;
}
.two-col{
 position:relative;
 float: left;
 width: 47%;
}
.two-col-pair-one{
 float: left;
 width: 100%;
 padding:5px;
}
.two-col-pair-one-qr{
 float: left;
 width: 100%;
 padding:5px;
}
.two-col-pair-two{
 float: left;
 width: 0%;
}
.two-col-pair-two-qr{
 float: left;
 width: 100%;
}
.row, .row-odd, .row-even, .row-default{
 width: 100%;
 display:block;
}
.container{
 display: inline-block;
 vertical-align: middle;
 width: 99%;
 max-height:100%
 margin-left: auto;
 margin-right: auto;
}
@media(max-width:800px){
  .thumbnails img {
  height: 50px;
  border: 1px solid #555;
  margin: 6px 6px 0 0;
  }
}
@media(max-width:480px){
  .thumbnails img {
  height: 30px;
  border: 1px solid #555;
  margin: 4px 4px 0 0;
  }
}
@media(orientation:landscape){
  .container{
  width:90%;
  }
  .thumbnails{
   position: fixed;
   top: 0;
   right:0;
   height: 100%;
  }
};
    try {
        pdfFile = new File(outputDir.getAbsolutePath() + File.separator + String.format(Locale.ENGLISH, "%s_%s.pdf", "Packet_labels", "_" + job.getId()));
        pdfFile.getParentFile().mkdirs();
        document = new Document(PageSize.A4, 0, 0, 0, 0);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
        document.open();
        PdfPTable table = getTable(CSS, htmlStr);
        document.add(table);
        document.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        Log.d("PDF Exception", "" + e);
        e.printStackTrace();
    }
    return pdfFile;
}

public static PdfPTable getTable(String CSS, String table) throws IOException {
    CSSResolver cssResolver = new StyleAttrCSSResolver();
    CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
    cssResolver.addCss(cssFile);

    // HTML
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
    htmlContext.setImageProvider(new Base64ImageProvider());

    // Pipelines
    ElementList elements = new ElementList();
    ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null);
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

    // XML Worker
    XMLWorker worker = new XMLWorker(css, true);
    XMLParser p = new XMLParser(worker);
    p.parse(new ByteArrayInputStream(table.getBytes()));

    return (PdfPTable)elements.get(0);
}

提前致谢。

手动设置table单元格高度时出现同样的错误。

理想情况下,单元格应移至下一页,但 itext 5 无法处理。

ITEXT 7解决了这个问题。