0

Hi I'm trying to convert a HTML String to pdf using itext 7 (latest version 7.1.15) available in maven repo.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;

public class PdfHtmlResponsiveDesign 

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

        String htmlSource = "<h1>aaa</h1>";
        File pdfDest = new File("C:\\Users\\acme\\Desktop\\tester\\itext7.pdf");
        // pdfHTML specific code
        ConverterProperties converterProperties = new ConverterProperties();
        converterProperties.setBaseUri("");
        HtmlConverter.convertToPdf(htmlSource,
                                   new FileOutputStream(pdfDest), converterProperties);
    }
}

But this simple code is throwing errors. This was working with itext 5.x. Any pointers to find the issue would be extremely helpful.

Exception in thread "main" java.lang.NoClassDefFoundError: com/itextpdf/kernel/counter/event/IMetaInfo
    at com.pegado.qpguard.demo.PdfHtmlResponsiveDesign.main(PdfHtmlResponsiveDesign.java:19)
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)
    ... 1 more

Note:

Itext 7 Version 7.1.15

html2pdf Version 3.0.4

Additional Info

If I change the version of itext to 7.1.14 the same code gives me another error

Exception in thread "main" java.lang.NoClassDefFoundError: com/itextpdf/layout/renderer/FlexContainerRenderer
    at com.itextpdf.html2pdf.attach.util.WaitingInlineElementsHelper.flushHangingLeaves(WaitingInlineElementsHelper.java:196)
    at com.itextpdf.html2pdf.attach.impl.tags.DivTagWorker.processEnd(DivTagWorker.java:97)
    at com.itextpdf.html2pdf.attach.impl.tags.HTagWorker.processEnd(HTagWorker.java:67)
    at com.itextpdf.html2pdf.attach.impl.DefaultHtmlProcessor.visit(DefaultHtmlProcessor.java:345)
    at com.itextpdf.html2pdf.attach.impl.DefaultHtmlProcessor.visit(DefaultHtmlProcessor.java:338)
    at com.itextpdf.html2pdf.attach.impl.DefaultHtmlProcessor.visit(DefaultHtmlProcessor.java:338)
    at com.itextpdf.html2pdf.attach.impl.DefaultHtmlProcessor.processDocument(DefaultHtmlProcessor.java:253)
    at com.itextpdf.html2pdf.attach.Attacher.attach(Attacher.java:78)
    at com.itextpdf.html2pdf.HtmlConverter.convertToDocument(HtmlConverter.java:325)
    at com.itextpdf.html2pdf.HtmlConverter.convertToPdf(HtmlConverter.java:139)
    at com.itextpdf.html2pdf.HtmlConverter.convertToPdf(HtmlConverter.java:127)
    at com.itextpdf.html2pdf.HtmlConverter.convertToPdf(HtmlConverter.java:104)
    at com.itextpdf.html2pdf.HtmlConverter.convertToPdf(HtmlConverter.java:92)
    at com.pegado.qpguard.demo.PdfHtmlResponsiveDesign.main(PdfHtmlResponsiveDesign.java:19)
Caused by: java.lang.ClassNotFoundException: com.itextpdf.layout.renderer.FlexContainerRenderer
    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)
    ... 14 more

Thank you

kallada
  • 1,829
  • 4
  • 33
  • 64

2 Answers2

2

If iText 5 user needed only one jar to do all the magic, iText 7, on the contrary, splits different functionality into separate modules. One should have:

  • a dependency on html2pdf (separate project) to harness html-to-pdf conversion functionality;
  • a dependency on layout (separate module of iTextCore project) to have means to layout element to PDF;
  • a dependency on kernel (separate module of iTextCore project) for some core stuff (in your case, IMetaInfo, which is internally used while considering the fact, whether any license is loaded).

So there were no issue with iTextcore 7.1.15 + pdfHTML 3.0.4, you just needed to add one more dependency. As for iTextcore 7.1.14 + pdfHTML 3.0.4: they are indeed incompatible, more precisely, flex elements are already supported in 3.0.4 on pdfHTML level, but not yet at layout level. The compatibility matrix could be found here: https://kb.itextpdf.com/home/it7kb/compatibility-matrix

Uladzimir Asipchuk
  • 2,368
  • 1
  • 9
  • 19
1

This problem is definitely not related to the iText itself. I wasn't able to reproduce this problem with Itext 7.1.15 and html2pdf 3.0.4. java.lang.NoClassDefFoundError exception is typically thrown when there are problems with classpath. see Why am I getting a NoClassDefFoundError in Java? . I can suggest to clean your .m2 folder or try to reload projects in intellij.

Eugen
  • 165
  • 1
  • 1
  • 10