如何解决ExceptionConverter com.itextpdf.text.html.simpleparser.TableWrapper无法强制转换为com.itextpdf.text.TextElementArray

时间:2015-04-21 07:02:40

标签: html itext

我使用下面代码的目的是将HTML文件转换为PDF

 try {
      com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4);
      PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("D:\\CreateHTMLToPDF.pdf"));
      document.open();
      document.addAuthor("Real Gagnon");
      document.addCreator("Real's HowTo");
      document.addSubject("Thanks for your support");
      document.addCreationDate();
      document.addTitle("Please read this");

      HTMLWorker htmlWorker = new HTMLWorker(document);
      String str = "<html>"+
                "<head></head>"+
              "<table><body>"+
                "<table border='1' style='width:100%'>"+
                    "<tr>"+
                    "<th width='100%' height='30px' style='background-color:#00FF00'><h1>Result</h1></th>"+
                    "</tr>"+ 
                "</table>"+
                "<table border='1' style='width:100%'>"+  
                    "<tr>"+
                    "<th width='100%' height='50px'></th>"+
                    "</tr>"+ 
                "</table>"+
                "<table border='1' style='width:100%'>"+   
                    "<tr>"+
                    "<th width='30%' height='440px'></th>"+
                    "<th width='30%' height='440px'></th>"+
                    "<th width='30%' height='220px'></th>"+
                    "</tr>"+  
                "</table>"+
                "<table border='1' style='width:100%'>"+   
                    "+<tr>"+
                    "<th width='100%' height='200px'></th>"+
                    "</tr>"+ 
                "</table>"+
            "</body></table>"+
      "</html>";
      htmlWorker.parse(new StringReader(str));
      document.close();
      System.out.println("Done");
      } catch(DocumentException e) {
          e.printStackTrace();
      } catch (FileNotFoundException e) {
      e.printStackTrace();
      } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      }
  }

当我使用htmlWorker对象时,它会抛出异常,

java.lang.ClassCastException: com.itextpdf.text.html.simpleparser.TableWrapper cannot be cast to com.itextpdf.text.TextElementArray
    at com.itextpdf.text.html.simpleparser.HTMLWorker.processTable(HTMLWorker.java:599)
    at com.itextpdf.text.html.simpleparser.HTMLTagProcessors$11.endElement(HTMLTagProcessors.java:363)
    at com.itextpdf.text.html.simpleparser.HTMLWorker.endElement(HTMLWorker.java:238)
    at com.itextpdf.text.xml.simpleparser.SimpleXMLParser.processTag(SimpleXMLParser.java:590)
    at com.itextpdf.text.xml.simpleparser.SimpleXMLParser.go(SimpleXMLParser.java:341)
    at com.itextpdf.text.xml.simpleparser.SimpleXMLParser.parse(SimpleXMLParser.java:608)
    at com.itextpdf.text.html.simpleparser.HTMLWorker.parse(HTMLWorker.java:154)
    at com.project.Html2Pdf.main(HtmlToPDF.java:71)

1 个答案:

答案 0 :(得分:2)

你的HTML坏了:

<html>
  <head>
  </head>
  <table>
    <body>
      ...
    </body>
  </table>
</html>

你基本上将body包裹在table中,但身体必须是html的直接孩子