我使用的是 IText PDF(7.1.x) 在 AEM 6.5. 我已经将IText7作为AEM主项目中的一个子项目添加到AEM中。代码构建成功,但我得到的是以下信息
java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.itextpdf.io.font.FontRegisterProvider.<clinit>(FontRegisterProvider.java:68)
at com.itextpdf.io.font.FontProgramFactory.<clinit>(FontProgramFactory.java:67)
at com.itextpdf.kernel.font.PdfFontFactory.createFont(PdfFontFactory.java:234)
at com.itextpdf.kernel.font.PdfFontFactory.createFont(PdfFontFactory.java:220)
at com.itextpdf.kernel.font.PdfFontFactory.createFont(PdfFontFactory.java:162)
at com.itextpdf.kernel.font.PdfFontFactory.createFont(PdfFontFactory.java:93)
at com.itextpdf.layout.RootElement.getDefaultProperty(RootElement.java:178)
at com.itextpdf.layout.Document.getDefaultProperty(Document.java:342)
............
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory not found by com.pdfgen
请在下面找到该子项目的POM文件。
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>com.pdfgen.itexttest</groupId>
<artifactId>pdfgen_itexttest</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>pdfgen_itexttest.itext</artifactId>
<packaging>jar</packaging>
<name>Pdfgen IText POC</name>
<description>IText bundle for POC/description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeArtifactIds>kernel,layout,bcprov-jdk15on,slf4j-api,slf4j-simple</includeArtifactIds>
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<executions>
<execution>
<id>bnd-process</id>
<goals>
<goal>bnd-process</goal>
</goals>
<configuration>
<bnd><![CDATA[
Bundle-Category: ITextHtml2pdf
Bundle-SymbolicName: com.pdfgen
Import-Package: javax.annotation;version=0.0.0,
!sun.*,
org.slf4j,
org.bouncycastle.asn1,
*
Include-Resource: target/dependency/slf4j-api.jar,
target/dependency/slf4j-simple.jar
Bundle-ClassPath: .,slf4j-api
Export-Package: com.itextpdf.*
]]></bnd>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.caconfig.bnd-plugin</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-baseline-maven-plugin</artifactId>
<configuration>
<failOnMissing>false</failOnMissing>
</configuration>
<executions>
<execution>
<id>baseline</id>
<goals>
<goal>baseline</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
</dependency>
</dependencies>
</project>
我尝试了一段PDF生成代码,它在AEM之外完美地工作,但在AEM中我得到了上述错误。
slf4j-api包在AEM上是可用的--没有必要导入它。事实上,你自己导入它可能是你的问题的原因。请在不导入它的情况下再试一次--并确保你的依赖定义中的slf4j版本与你的AEM安装相匹配。
除此之外,我建议你改用maven插件,比如efilevault-package-maven-plugin来嵌入外部依赖。