我正在尝试使用itextpdf将html转换为pdf
但是即使我使用最新版本的itextpdf也没有nosuchmethoderror

相依性

    <dependency>
        <groupId>com.itextpdf.tool</groupId>
        <artifactId>xmlworker</artifactId>
        <version>5.5.3</version>
    </dependency>


进口货

   import com.itextpdf.text.Document;
   import com.itextpdf.text.DocumentException;
   import com.itextpdf.text.pdf.PdfWriter;
   import com.itextpdf.tool.xml.XMLWorkerHelper;

   ....


资源

    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/home/test.pdf"));
    document.open();
    String file = "/home/test.html";
    XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(file));  /* **line 191** */
    document.close();


例外

Exception in thread "main" java.lang.NoSuchMethodError: com.itextpdf.text.pdf.PdfPTable.addCell(Lcom/itextpdf/text/pdf/PdfPCell;)Lcom/itextpdf/text/pdf/PdfPCell;
    at com.itextpdf.tool.xml.html.table.Table.end(Table.java:433)
    at com.itextpdf.tool.xml.html.AbstractTagProcessor.endElement(AbstractTagProcessor.java:192)
    at com.itextpdf.tool.xml.pipeline.html.HtmlPipeline.close(HtmlPipeline.java:207)
    at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:142)
    at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:396)
    at com.itextpdf.tool.xml.parser.state.ClosingTagState.process(ClosingTagState.java:71)
    at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:236)
    at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:214)
    at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:175)
    at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:220)
    at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:182)
    at com.jvr.util.CommonUtil.pdf(CommonUtil.java:191)

最佳答案

我知道了

使用的com.itextpdf版本不正确

正确的版本是

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.4.2</version>
    </dependency>

09-25 19:49