1

I'm trying to create PDF file from HTML using Java. I've added two jar files.

  1. itextpdf-5.1.0
  2. xmlworker-5.5.6

My code:

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
    String emailBody = "<html><body> This is my Project </body></html>";

    OutputStream file = new FileOutputStream(new File("E:\\Test.pdf"));
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, file);
    document.open();
    InputStream is = new ByteArrayInputStream(emailBody.getBytes());
    XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
    document.close();
    file.close();

I got an error from this line:

XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);

It throws me this exception. I'm looking for a solution.

java.lang.NoSuchMethodError: com.itextpdf.text.log.LoggerFactory.getLogger(Ljava/lang/Class;)Lcom/itextpdf/text/log/Logger;
    at com.itextpdf.tool.xml.net.FileRetrieveImpl.<clinit>(FileRetrieveImpl.java:68)
    at com.itextpdf.tool.xml.css.StyleAttrCSSResolver.<init>(StyleAttrCSSResolver.java:116)
    at com.itextpdf.tool.xml.css.StyleAttrCSSResolver.<init>(StyleAttrCSSResolver.java:105)
    at com.itextpdf.tool.xml.css.StyleAttrCSSResolver.<init>(StyleAttrCSSResolver.java:93)
    at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:212)
    at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:184)
    at com.cds.controllers.subservlets.Admin.PDFDownload.manageDetails(PDFDownload.java:102)
    at com.cds.models.SubServletModel.handleRequest(SubServletModel.java:66)
    at com.cds.controllers.HDTController.doPost(HDTController.java:390)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Thanks.

Kerem
  • 840
  • 7
  • 22
Dhaval Goti
  • 447
  • 2
  • 10
  • 25

2 Answers2

1

Most probably they are not compatiable to each other. Try with the following recent versions xmlworker-5.5.13.jar, itextpdf-5.5.13.jar

Deb
  • 2,922
  • 1
  • 16
  • 32
  • 1
    That is most definitely the case: iText 5.1.0 was released on 6 May 2011; the current version of `LoggerFactory.getLogger` (mentioned in the OP's error) was written on 12 May 2011, so after the release of iText 5.1.0. Which means that XMLWorker 5.5.6 and iText 5.1.0 are not compatible. Your recommendation, to use version 5.5.13 for both, is indeed the right one. – Amedee Van Gasse May 02 '18 at 12:37
  • I down-voted this answer because iText 5.5.13 is a *maintenance* release of an old iText version that is being phased out. You describe them as "recent", which is true, but they were created only for people who aren't ready to migrate to iText 7 yet. No new functionality will be added to iText 5 and further development has stopped. New functionality will only be added to iText 7. Please read [Converting HTML to PDF](https://stackoverflow.com/questions/47895935) for more info. – Bruno Lowagie May 04 '18 at 09:46
-1

You really have conflicts with these two libraries. You have to exclude transitive dependency of Loggeer from one of them.

What build tool do you use? Like maven, gradle etc.

Please see https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html or https://docs.gradle.org/current/userguide/managing_transitive_dependencies.html

Yaroslav
  • 446
  • 4
  • 15