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

如何解决异常转换器com.itextpdf.text.html.simpleparser.TableWrapper无法强制转换为com.itextpdf.text.TextElementArray,html,itext,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

我使用下面代码的目的是将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();
      }
  }
您的html已损坏:

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

...

基本上,您可以将
正文
包装在
表格
中,但正文必须是
html

的直接子项,但不能像html页面显示那样显示pdf,我的要求相同的pdf页面创建看起来相同如何可以创建多个表格以及如何在一个PDFA html呈现程序中添加拒绝该html,它是无效的。应该拒绝在不同的呈现器上呈现相同无效HTML的要求。您体内的
元素看起来正常。移除无效的外部。好的,我移除了。PDF正在创建。但显示方式与Html页面显示方式不同
<html>
  <head>
  </head>
  <table>
    <body>
      ...
    </body>
  </table>
</html>