3

I'm new to Java, I'm working on generating pdf from html. Therefore, I'm using the iText7, I can generate a normal pdf file through PdfWriter and Document but I can't do it using html2pdf.

Here is my 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>

and here is my code 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");
        }
    }

The error is on the line:

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)

I read other issues related to

java.lang.NoClassDefFoundError

they said that I need to add the io and slf4j dependencies, however the error remains. Thanks in advance.

Nadya Nux
  • 519
  • 1
  • 5
  • 17
Nick
  • 419
  • 9
  • 19

3 Answers3

5

com/itextpdf/kernel/counter/event/IMetaInfo is not present in version 7.0.4 Try upgrading to itext 7.1.7

Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
e.g78
  • 667
  • 4
  • 8
0

With html2pdf, this error java.lang.NoClassDefFoundError: com/itextpdf/kernel/counter/event/IMetaInfo is resolved by upgrading your IText 7 Kernel to 7.2.5 as shown below

 <dependency>
     <groupId>com.itextpdf</groupId>
     <artifactId>kernel</artifactId>
     <version>7.2.5</version>
 </dependency>
Nandom Gusen
  • 51
  • 1
  • 5
-1

Yesterday I had simillar problem. It appears that some dependencies did not load properly even that no errors were thrown. I had to restart IDE (Eclipse in my case) and that sorted out the problem.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
lukdymek
  • 15
  • 5
  • If you consider your post to be a comment please delete it; compare https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead But you might be able to turn it into a partial answer, if you can provide an insight to at least solve some part of the problem. In that case please [edit] according to [answer]. – Yunnosch Dec 29 '22 at 11:09
  • 2
    You know about the commenting privilege which you do not have, so well that you can even put it into words. You are aware of the rule https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead . In that situation please do not decide to misuse a different mechanism (an answer) for something it is not meant for and which you are not allowed yet to do. – Yunnosch Dec 29 '22 at 11:10
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 29 '22 at 14:31