0

I am converting pdf from html which also includes css using ITextRenderer,

    ClassLoader classLoader = DigiFaxInvoice.class.getClassLoader();
          URL url = classLoader.getResource("html file path");
            String urlString = new File(url.getFile()).toURI().toURL().toString();
            String HTML_TO_PDF = "pdf file path";
            OutputStream os = new FileOutputStream(HTML_TO_PDF);       
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocument(urlString);      
            renderer.layout();
            renderer.createPDF(os);        
            os.close();

I got following error,

Exception in thread "main" java.lang.ClassCastException: org.xhtmlrenderer.render.BlockBox cannot be cast to org.xhtmlrenderer.newtable.TableBox
            at org.xhtmlrenderer.layout.BoxCollector.collect(BoxCollector.java:127)
            at org.xhtmlrenderer.layout.BoxCollector.collect(BoxCollector.java:139)
            at org.xhtmlrenderer.layout.BoxCollector.collect(BoxCollector.java:139)
            at org.xhtmlrenderer.layout.BoxCollector.collect(BoxCollector.java:46)
            at org.xhtmlrenderer.layout.Layer.paint(Layer.java:314)
            at org.xhtmlrenderer.layout.Layer.paintLayers(Layer.java:165)
            at org.xhtmlrenderer.layout.Layer.paint(Layer.java:337)
            at org.xhtmlrenderer.pdf.ITextRenderer.paintPage(ITextRenderer.java:384)
            at org.xhtmlrenderer.pdf.ITextRenderer.writePDF(ITextRenderer.java:348)
            at org.xhtmlrenderer.pdf.ITextRenderer.createPDF(ITextRenderer.java:315)
            at org.xhtmlrenderer.pdf.ITextRenderer.createPDF(ITextRenderer.java:246)
            at DigiFaxInvoice.main(DigiFaxInvoice.java:47)

Can you please explain why I am getting this error and how to fix it?

Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
  • Possible duplicate of [Getting this "org.xhtmlrenderer.render.BlockBox cannot be cast to org.xhtmlrenderer.newtable.TableBox" while trying to render a pdf](https://stackoverflow.com/questions/13137401/getting-this-org-xhtmlrenderer-render-blockbox-cannot-be-cast-to-org-xhtmlrende) – Georg Leber Jan 17 '18 at 07:53
  • 1
    You are tagging this question as an iText® question, but it isn't. You code looks like Flying Saucer code. Only questions that use code that resembles the code in the question and answer [Converting HTML to PDF](https://stackoverflow.com/questions/47895935/converting-html-to-pdf-using-itext) should be tagged as iText questions. Flying Saucer questions should be tagged as Flying Saucer questions. (Flying Saucer is a third party library that is not endorsed by iText Group.) – Bruno Lowagie Jan 17 '18 at 08:10
  • Please check : https://stackoverflow.com/questions/30213697/itext-flying-saucer-xhtml-to-pdf-classcastexception/56310645#56310645 – asmmahmud May 26 '19 at 04:29

0 Answers0