I want to create a PDF in a servlet Java8/Tomcat9.0 using iText 8.0.5 I got the sample class here. Compilation is ok but when I load the page I get an error 500. The jar files are present on my tomcat : E:\Tomcat 9.0\webapps\Thanact\WEB-INF\lib
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.borders.SolidBorder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
public class EtiquettesServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// Test si la classe PdfWriter est bien chargée
Class.forName("com.itextpdf.kernel.pdf.PdfWriter");
System.out.println("Classe PdfWriter trouvée !");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("Classe PdfWriter non trouvée !");
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Classe PdfWriter non trouvée !");
return;
}
// Le reste du code pour générer le PDF
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=etiquettes.pdf");
try {
PdfWriter writer = new PdfWriter(response.getOutputStream());
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
document.add(new Paragraph("Hello World!"));
document.close();
} catch (Exception e) {
e.printStackTrace();
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Erreur lors de la génération du PDF : " + e.getMessage());
}
}
}
}
The web page shows :
cause mère
java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
com.itextpdf.kernel.pdf.PdfOutputStream.<clinit>(PdfOutputStream.java:48)
But in the tomcat stdout log I see my iText classes. How can I fix this ?
/E:/Tomcat%209.0/webapps/Thanact/WEB-INF/classes/
/E:/Tomcat%209.0/webapps/Thanact/WEB-INF/lib/commons-8.0.5.jar
/E:/Tomcat%209.0/webapps/Thanact/WEB-INF/lib/gson-2.2.2.jar
/E:/Tomcat%209.0/webapps/Thanact/WEB-INF/lib/io-8.0.5.jar
/E:/Tomcat%209.0/webapps/Thanact/WEB-INF/lib/javax.servlet-api-3.0.1.jar
/E:/Tomcat%209.0/webapps/Thanact/WEB-INF/lib/jstl_1.2.5.jar
/E:/Tomcat%209.0/webapps/Thanact/WEB-INF/lib/kernel-8.0.5.jar
/E:/Tomcat%209.0/webapps/Thanact/WEB-INF/lib/layout-8.0.5.jar
/E:/Tomcat%209.0/webapps/Thanact/WEB-INF/lib/ojdbc6.jar
/E:/Tomcat%209.0/webapps/Thanact/WEB-INF/lib/sqljdbc.jar
/E:/Tomcat%209.0/webapps/Thanact/WEB-INF/lib/taglibs-standard-impl-1.2.5.jar
/E:/Tomcat%209.0/webapps/Thanact/WEB-INF/lib/taglibs-standard-spec-1.2.5.jar
You need to download the following files and put them under WEB-INF/lib
:
Your error message:
java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
com.itextpdf.kernel.pdf.PdfOutputStream.<clinit>(PdfOutputStream.java:48)
It means that itextpdf cannot find the class related to slf4j. It means that itextpdf depends on slf4j.
iText 8.0.5 Java can be found on Github iText Core/Community 8.0.5. (https://github.com/itext/itext-java/releases)
find Asserts
,
iText-Core-8.0.5.zip
iText-Core-8.0.5.zip
,slf4j
and logback
root-8.0.5.pom
<logback.version>1.2.13</logback.version>
<slf4j.version>1.7.36</slf4j.version>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>test</scope>
</dependency>
Then you can use google maven logback 1.2.13
and maven slf4j 1.7.36
to find the mvnrepository.com
related links in the results, and then download the jar.
In addition, iText 8.5 also requires the following jars:
Prerequisites:
Install all iText jar on local maven repo:
cd iText-Core-8.0.5
Create file: maven-install-jar.bat
@echo off
mvn install:install-file -Dfile=%1.jar -DpomFile=%1.pom
Create file: maven-install-jar
#!/bin/bash
mvn install:install-file -Dfile=$1.jar -DpomFile=$1.pom
chmod u+x maven-install-jar
export PATH=`pwd`:$PATH
(In the terminal, the current location is in the iText-Core-8.0.5
directory.)
Same command for Windows and Linux
mvn install:install-file -Dfile=root-8.0.5.pom -DgroupId=com.itextpdf -DartifactId=root -Dversion=8.0.5 -Dpackaging=pom
mvn install:install-file -Dfile=itext7-core-8.0.5.pom -DgroupId=com.itextpdf -DartifactId=itext7-core -Dversion=8.0.5 -Dpackaging=pom
mvn install:install-file -Dfile=itext-core-8.0.5.pom -DgroupId=com.itextpdf -DartifactId=itext-core -Dversion=8.0.5 -Dpackaging=pom
maven-install-jar barcodes-8.0.5
maven-install-jar bouncy-castle-adapter-8.0.5
maven-install-jar bouncy-castle-connector-8.0.5
maven-install-jar bouncy-castle-fips-adapter-8.0.5
maven-install-jar commons-8.0.5
maven-install-jar font-asian-8.0.5
maven-install-jar forms-8.0.5
maven-install-jar hyph-8.0.5
maven-install-jar io-8.0.5
maven-install-jar kernel-8.0.5
maven-install-jar layout-8.0.5
maven-install-jar pdfa-8.0.5
maven-install-jar pdftest-8.0.5
maven-install-jar pdfua-8.0.5
maven-install-jar sign-8.0.5
maven-install-jar styled-xml-parser-8.0.5
maven-install-jar svg-8.0.5
Now your iText Maven Project:
pom.xml
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>2.0.13</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>barcodes</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/barcodes-8.0.5.jar</systemPath>
-->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>bouncy-castle-adapter</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/bouncy-castle-adapter-8.0.5.jar</systemPath>
-->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>bouncy-castle-connector</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/bouncy-castle-connector-8.0.5.jar</systemPath>
-->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>bouncy-castle-fips-adapter</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/bouncy-castle-fips-adapter-8.0.5.jar</systemPath>
-->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>commons</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/commons-8.0.5.jar</systemPath>
-->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/font-asian-8.0.5.jar</systemPath>
-->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>forms</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/forms-8.0.5.jar</systemPath>
-->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>hyph</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/hyph-8.0.5.jar</systemPath>
-->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/io-8.0.5.jar</systemPath>
-->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/kernel-8.0.5.jar</systemPath>
-->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/layout-8.0.5.jar</systemPath>
-->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>pdfa</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/pdfa-8.0.5.jar</systemPath>
-->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>pdfua</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/pdfua-8.0.5.jar</systemPath>
-->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>sign</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/sign-8.0.5.jar</systemPath>
-->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>styled-xml-parser</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/styled-xml-parser-8.0.5.jar</systemPath>
-->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>svg</artifactId>
<version>8.0.5</version>
<!--
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib-jar-itext/svg-8.0.5.jar</systemPath>
-->
</dependency>