java.lang.NoClassDefFoundError: com/itextpdf/kernel/counter/event/IMetaInfo

java.lang.NoClassDefFoundError: com/itextpdf/kernel/counter/event/IMetaInfo

我是 Java 的新手,我正在研究从 html 生成 pdf。因此,我使用的是 iText7,我可以通过 PdfWriter 和 Document 生成一个普通的 pdf 文件,但我不能使用 html2pdf.

这是我的 Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>springexample</groupId>
  <artifactId>PDFGenerator</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>bean</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext7-core</artifactId>
        <version>7.0.4</version>
        <type>pom</type>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>kernel</artifactId>
        <version>7.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>io</artifactId>
        <version>7.0.4</version>
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>forms</artifactId>
        <version>7.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdfa</artifactId>
        <version>7.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdftest</artifactId>
        <version>7.0.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.itextpdf/html2pdf -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>html2pdf</artifactId>
        <version>2.1.4</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.26</version>
    </dependency>
</project>

这是我的代码 java:

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;

import com.itextpdf.html2pdf.HtmlConverter;

public class HtmlToPdf {

/** The HTML-string that we are going to convert to PDF. */
public static final String HTML = "<h1>Test</h1><p>Hello World</p>";
/** The target folder for the result. */
public static final String TARGET = "target/";
/** The path to the resulting PDF file. */
public static final String DEST = String.format("%stest-01.pdf", TARGET);

        public static void main(String[] args) throws IOException { 

            HtmlConverter.convertToPdf(HTML, new FileOutputStream(DEST));

            System.out.println("Done");
        }
    }

错误在线:

HtmlConverter.convertToPdf(HTML, new FileOutputStream(DEST));

Exception in thread "main" java.lang.NoClassDefFoundError: com/itextpdf/kernel/counter/event/IMetaInfo
    at springexample.bean.HtmlToPdf.main(HtmlToPdf.java:18)
Caused by: java.lang.ClassNotFoundException: com.itextpdf.kernel.counter.event.IMetaInfo
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)

我阅读了与

相关的其他问题

java.lang.NoClassDefFoundError

他们说我需要添加 io 和 slf4j 依赖项,但是错误仍然存​​在。 提前致谢。

com/itextpdf/kernel/counter/event/IMetaInfo 在版本 7.0.4 中不存在 尝试升级到 itext 7.1.7