0

Trying to use iText for simple PDF generation, and encountering NotImplementedException on table elements.

TableRowElement.type() just throw new NotImplementedException()... Why is it called?

com.itextpdf.tool.xml.exceptions.NotImplementedException
at com.itextpdf.tool.xml.html.table.TableRowElement.type(TableRowElement.java:148)
at com.itextpdf.text.pdf.ColumnText.addElement(ColumnText.java:471)
at com.itextpdf.text.pdf.PdfPCell.addElement(PdfPCell.java:284)
at com.itextpdf.tool.xml.html.table.TableData.end(TableData.java:151)
at com.itextpdf.tool.xml.html.AbstractTagProcessor.endElement(AbstractTagProcessor.java:189)
at com.itextpdf.tool.xml.pipeline.html.HtmlPipeline.close(HtmlPipeline.java:206)
at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:141)
at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:395)
at com.itextpdf.tool.xml.parser.state.ClosingTagState.process(ClosingTagState.java:70)
at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:235)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:213)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:175)
at com.slim.pims.upload.ui.OwnerMainPDF.doPost(OwnerMainPDF.java:90)
at com.slim.pims.upload.ui.OwnerMainPDF.doGet(OwnerMainPDF.java:53)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

Source:

//now, this is fun!
public boolean process(final ElementListener listener) {
    throw new NotImplementedException();
}

public int type() {
    throw new NotImplementedException();
}

public boolean isContent() {
    throw new NotImplementedException();
}

public boolean isNestable() {
    throw new NotImplementedException();
}

public List<Chunk> getChunks() {
    throw new NotImplementedException();
}

Edit to include html source.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <title></title>     
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    </head>

    <body>


    <table style="width: 100%; ">
        <tr>
        <td>



        <tr>
        <td align="left" width="40%" valign="top">
            <table id='tableRegionTotals' style='width: 100%;' border='0' cellpadding='0' cellspacing='1' bgcolor='#cccccc'><tr height='20'><td class='a5' align='center' width=40%;><stong>Schedule Status</stong></td><td class='a5' align='center' width=30%;><stong>No.</stong></td><td class='a5' align='center' width=30%;><stong>Total (for New Projects)</stong></td></tr><tr height='20'><td class='a0' align='left'><table><tr><td width=10%;>&nbsp;</td><td class='a0' width=90%;><div id='divOnTrack' style='width:10px;background-color:#336633;' >&nbsp;&nbsp;&nbsp;&nbsp;<a onclick='' name='nontrack'>On&nbsp;&nbsp;Track&nbsp;</a></div></td></tr></table></td><td class='a0' align='right'>1&nbsp;&nbsp;</td><td class='a0' align='right'>0.00&nbsp;&nbsp;</td></tr><tr height='20'><td class='a0' align='left'><table><tr><td width=10%;>&nbsp;</td><td class='a0' width=90%;><div id='divNeedsAttention' style='width:10px;background-color:#cccc33;' >&nbsp;&nbsp;&nbsp;&nbsp;<a onclick='' name='nontrack'>Needs&nbsp;&nbsp;Attention&nbsp;</a></div></td></tr></table></td><td class='a0' align='right'>1&nbsp;&nbsp;</td><td class='a0' align='right'>0.00&nbsp;&nbsp;</td></tr><tr height='20'><td class='a0' align='left'><table><tr><td width=10%;>&nbsp;</td><td class='a0' width=90%;><div id='divOffTrack' style='width:10px;background-color:#cc0000;' >&nbsp;&nbsp;&nbsp;&nbsp;<a onclick='' name='nontrack'>Off&nbsp;&nbsp;Track&nbsp;</a></div></td></tr></table></td><td class='a0' align='right'>20&nbsp;&nbsp;</td><td class='a0' align='right'>0&nbsp;&nbsp;</td></tr></table>
        </td>
        <td width="1%">&nbsp;</td>
        <td align="right" width="55%" valign="top">


        </td>
        </tr>

        </td>
        </tr>   
    </table>

    <table>
        <tr>
        <td>



        </td>
        </tr>

    </table>

        </body>
    </html>
5
  • 1
    When you create a table in iText, you don't need rows. You create a PdfPTable and add PdfPCell instances. The concept of a row only exists internally. To make this clear, the methods of the Element interface you mention throw an NotImplementedException. AFAIK, nobody has ever presented a use case that needed the type() method for a row. Commented Nov 5, 2013 at 7:37
  • I got this error by calling parseXHtml(PdfWriter writer, Document doc, Reader in); also parseXHtml(PdfWriter writer, Document doc, InputStream in, InputStream inCssFile). So type() is called internally by other iText api. Commented Nov 5, 2013 at 7:50
  • Aha, that is interesting information. I'll pass it on to the developers of XML Worker as soon as you let me know which version you're using. I'll also need an HTML file that can be used to reproduce the error (the error was never thrown in our JUnit tests). Commented Nov 5, 2013 at 11:07
  • version 5.4.4, question edited to include the html. Commented Nov 7, 2013 at 7:26
  • 1
    Thanks, that HTML looks very strange though. You have a <tr> tag inside a <td> tag. That's not correct, is it? Commented Nov 7, 2013 at 8:24

1 Answer 1

1

This is a copy paste from the iText support engineers (I made a paid support ticket for this):

The HTML is indeed not correct. It is full of errors.

The first table starts with <tr><td><tr>. It's illegal to nest a <tr> inxide a <td>. This causes XML Worker to throw the NonImplementedException. When <tr><td> at the start and </td></tr> at the end of this table are removed, XML Worker is able to process the HTML.

The resulting PDF doesn't look good because of other issues with the HTML:

  • Attribute values must be quoted. Example: width=90%; should be replaced with width="90%".
  • In the first row of the table, the text is surrounded by <stong>, which is not a valid (X)HTML tag and ignored by XML Worker. Replace with <strong>.
  • The divs that are used to display the green, yellow and red rectangles should be replaced by spans.
  • Some tweaks are needed in the width attributes to accommodate paper size (a PDF viewer isn't a browser).
  • width and height aren't valid attributes for <td> and <tr> (but we do process them).
  • bgcolor isn't a valid attribute for <table>, which is why the table background is white in the PDF.

In other words: your question was less a problem with iText and XML Worker than it was a problem with XHTML.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Bruno, much appreciated. The html is from an ex-project just for testing iText, the page originally has 3 tables, and none of them works with iText individually, probably because of the reasons above. What's your opinion on iText, especially the XHTML to PDF?
The support team tested an altered HTML file and produced a PDF that was acceptable. So my opinion on the matter is: fix the HTML. There are a lot of misconceptions regarding XML Worker. It's not an "URL2PDF" tool. Instead it's an alternative to XFA, XSL:FO,... for those who don't need to accuracy of XFA, nor want the complexity of XSL:FO. See youtube.com/watch?v=qJ34RBb4GYc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.