Expand my Community achievements bar.

java.lang.ClassNotFoundException: javax.xml.transform.TransformerConfigurationException not found by org.xhtmlrenderer.flying.saucer.pdf.osgi

Avatar

Level 1

AEM ver: 6.5.15.0 (on premise)
Trying to generate pdf from HTML using Flying-Saucer library
I have added the following Dependencies in my POM file:

<dependency>
            <groupId>org.xhtmlrenderer</groupId>
            <artifactId>flying-saucer-core</artifactId>
            <version>9.3.1</version>
</dependency>
<dependency>
            <groupId>org.xhtmlrenderer</groupId>
            <artifactId>flying-saucer-pdf-osgi</artifactId>
            <version>9.3.1</version>
</dependency>
<dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.17.2</version>
</dependency>

I have installed the bundles for above dependencies in the system/console/bundles, as I was getting dependencies error in my core bundle, that issue resolved but then I get this issue:
The java code snippet to generate the html to pdf:

private static void htmlToPdf() throws IOException {
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ITextRenderer iTextRenderer = new ITextRenderer(); // Getting the error at this line
            iTextRenderer.setDocumentFromString("<html><head><style>@page { size: A4; } body { font-size: 12pt; }</style></head><body><p>Hello, World!</p></body></html>");
            iTextRenderer.layout();
            iTextRenderer.createPDF(os);
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Whenever the line ITextRenderer iTextRenderer = new ITextRenderer() exceutes, it will be thrown out giving the exception: javax.xml.transform.TransformerConfigurationException

I tried to add the Import-Package configuration javax.xml.transform for this in bnd plugin in the POM file, but still getting the same exception
The POM File looks like below:

...
<build>
<plugins>
<plugin>
<groupId>org.apache.sling</groupId>
<artifactId>sling-maven-plugin</artifactId>
</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[
Import-Package: javax.annotation;version=0.0.0,*, javax.xml.transform;version=0.0.0,*
]]></bnd>
</configuration>
</execution>
</executions>
</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>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.3.1</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-osgi</artifactId>
<version>9.3.1</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.17.2</version>
</dependency>
...
<dependencies>
...
The stack trace looks like below:

 

 org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught Throwable
java.lang.NoClassDefFoundError: javax/xml/transform/TransformerConfigurationException
	at com.adobe.aem.test.core.servlets.TestServlet.xhtmlToPdf(TestServlet.java:176) [test.core:0.0.1.SNAPSHOT]
	at com.adobe.aem.test.core.servlets.TestServlet.doGet(TestServlet.java:72) [test.core:0.0.1.SNAPSHOT]
	at org.apache.sling.api.servlets.SlingSafeMethodsServlet.mayService(SlingSafeMethodsServlet.java:266) [org.apache.sling.api:2.22.0]
	at org.apache.sling.api.servlets.SlingAllMethodsServlet.mayService(SlingAllMethodsServlet.java:137) [org.apache.sling.api:2.22.0]
	at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:342) [org.apache.sling.api:2.22.0]
	at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:374) [org.apache.sling.api:2.22.0]
	at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:579) [org.apache.sling.engine:2.7.10.B0004]
	at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:45) [org.apache.sling.engine:2.7.10.B0004]
	at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:88) [org.apache.sling.engine:2.7.10.B0004]
	at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilter(WCMDebugFilter.java:156) [com.day.cq.wcm.cq-wcm-core:5.12.216]
	at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0004]
	at com.day.cq.wcm.core.impl.WCMComponentFilter.filterRootInclude(WCMComponentFilter.java:375) [com.day.cq.wcm.cq-wcm-core:5.12.216]
	at com.day.cq.wcm.core.impl.WCMComponentFilter.doFilter(WCMComponentFilter.java:190) [com.day.cq.wcm.cq-wcm-core:5.12.216]
	at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0004]
	at com.day.cq.wcm.core.impl.page.PageLockFilter.doFilter(PageLockFilter.java:91) [com.day.cq.wcm.cq-wcm-core:5.12.216]
	at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0004]
	at com.day.cq.personalization.impl.TargetComponentFilter.doFilter(TargetComponentFilter.java:94) [com.day.cq.cq-personalization:5.12.48]
	at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0004]
	at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processComponent(SlingRequestProcessorImpl.java:283) [org.apache.sling.engine:2.7.10.B0004]
	at org.apache.sling.engine.impl.filter.RequestSlingFilterChain.render(RequestSlingFilterChain.java:49) [org.apache.sling.engine:2.7.10.B0004]
	at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:82) [org.apache.sling.engine:2.7.10.B0004]
	at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:84) [org.apache.sling.engine:2.7.10.B0004]
	at 
	...
org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:883) [org.apache.felix.http.jetty:4.2.2]
	at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1034) [org.apache.felix.http.jetty:4.2.2]
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.ClassNotFoundException: javax.xml.transform.TransformerConfigurationException not found by org.xhtmlrenderer.flying.saucer.pdf.osgi [627]
	at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1597)
	at org.apache.felix.framework.BundleWiringImpl.access$300(BundleWiringImpl.java:79)
	at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1982)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:527)
	... 141 common frames omitted

 

6 Replies

Avatar

Community Advisor

Are you also embedding the external dependencies to deploy them along with your bundle into AEM? If you don't know how to do it please check this thread: https://www.linkedin.com/pulse/how-add-third-party-bundle-you-aem-package-veena-vikraman/ 

 

Also, please check this post: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/unresolved-requirements-os... which mentioned some compatible versions for OSG. 

Hope this helps



Esteban Bustamante

Avatar

Administrator

@Rahul_Dubey Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni

Avatar

Community Advisor

javax.xml.transform - importing this explicitly looks good.

javax.xml.transform Is this package required to Export explicitly with bundle?

Avatar

Level 6

Hi @Rahul_Dubey 

I think you should go with Apache PDFBox library instead of Flying Saucer. I also faced similar issues with flying saucer. With Flying Saucer, you resolve one dependency and it throws another one at you.

For Apache PDFBox, add below dependencies in your core pom.xml

 

<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
		<dependency>
		    <groupId>org.apache.pdfbox</groupId>
		    <artifactId>pdfbox</artifactId>
		    <version>2.0.30</version>
		    <scope>provided</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.openhtmltopdf/openhtmltopdf-pdfbox -->
		<dependency>
		    <groupId>com.openhtmltopdf</groupId>
		    <artifactId>openhtmltopdf-pdfbox</artifactId>
		    <version>1.0.10</version>
		    <scope>provided</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.openhtmltopdf/openhtmltopdf-core -->
		<dependency>
		    <groupId>com.openhtmltopdf</groupId>
		    <artifactId>openhtmltopdf-core</artifactId>
		    <version>1.0.10</version>
		    <scope>provided</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox -->
		<dependency>
		    <groupId>org.apache.pdfbox</groupId>
		    <artifactId>fontbox</artifactId>
		    <version>2.0.30</version>
		    <scope>provided</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/xmpbox -->
		<dependency>
		    <groupId>org.apache.pdfbox</groupId>
		    <artifactId>xmpbox</artifactId>
		    <version>2.0.30</version>
		    <scope>provided</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/de.rototor.pdfbox/graphics2d -->
		<dependency>
		    <groupId>de.rototor.pdfbox</groupId>
		    <artifactId>graphics2d</artifactId>
		    <version>3.0.1</version>
		    <scope>provided</scope>
		</dependency>

 

in core pom.xml, export this package with Export-Package option from maven-bundle-plugin or bnd-maven-plugin which ever you are using.

 

<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[
Import-Package: javax.annotation;version=0.0.0,!org.osgi.framework,!sun.misc,*
Export-Package: de.rototor.pdfbox.graphics2d.*
-includeresource: jsoup*.jar;lib:=true
                                ]]></bnd>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

 

In all module's pom.xml embed these bundles in order to install them.

 

<plugin>
    <groupId>org.apache.jackrabbit</groupId>
    <artifactId>filevault-package-maven-plugin</artifactId>
    <extensions>true</extensions>
    <configuration>
		<group>org.myproject.site</group>
        <packageType>container</packageType>
        <!-- skip sub package validation for now as some vendor packages like CIF apps will not pass -->
        <skipSubPackageValidation>true</skipSubPackageValidation>
        <embeddeds>
			<embedded>
				<groupId>com.openhtmltopdf</groupId>
		    	<artifactId>openhtmltopdf-pdfbox</artifactId>
		    	<target>/apps/myproject-packages/application/install</target>
				</embedded>
			<embedded>
				<groupId>org.apache.pdfbox</groupId>
		    	<artifactId>pdfbox</artifactId>
		    	<target>/apps/myproject-packages/application/install</target>
			</embedded>
			<embedded>
				<groupId>com.openhtmltopdf</groupId>
		    	<artifactId>openhtmltopdf-core</artifactId>
		    	<target>/apps/myproject-packages/application/install</target>
			</embedded>
			<embedded>
				<groupId>org.apache.pdfbox</groupId>
		    	<artifactId>fontbox</artifactId>
		    	<target>/apps/myproject-packages/application/install</target>
			</embedded>
			<embedded>
				<groupId>org.apache.pdfbox</groupId>
		    	<artifactId>xmpbox</artifactId>
		    	<target>/apps/myproject-packages/application/install</target>
			</embedded>
			<embedded>
				<groupId>de.rototor.pdfbox</groupId>
		    	<artifactId>graphics2d</artifactId>
		    	<target>/apps/myproject-packages/application/install</target>
			</embedded>
        </embeddeds>
    </configuration>
</plugin>
<dependencies>
		<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
		<dependency>
		    <groupId>org.apache.pdfbox</groupId>
		    <artifactId>pdfbox</artifactId>
		    <version>2.0.30</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.openhtmltopdf/openhtmltopdf-pdfbox -->
		<dependency>
		    <groupId>com.openhtmltopdf</groupId>
		    <artifactId>openhtmltopdf-pdfbox</artifactId>
		    <version>1.0.10</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.openhtmltopdf/openhtmltopdf-core -->
		<dependency>
		    <groupId>com.openhtmltopdf</groupId>
		    <artifactId>openhtmltopdf-core</artifactId>
		    <version>1.0.10</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox -->
		<dependency>
		    <groupId>org.apache.pdfbox</groupId>
		    <artifactId>fontbox</artifactId>
		    <version>2.0.30</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/xmpbox -->
		<dependency>
		    <groupId>org.apache.pdfbox</groupId>
		    <artifactId>xmpbox</artifactId>
		    <version>2.0.30</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/de.rototor.pdfbox/graphics2d -->
		<dependency>
		    <groupId>de.rototor.pdfbox</groupId>
		    <artifactId>graphics2d</artifactId>
		    <version>3.0.1</version>
		</dependency>
</dependencies>

 

 

Avatar

Level 1

Thanks for your response, it helps.
But I am not able to render the images in my pdf:

Steps I have taken to solve it:

- Give the full path with base uri

- added dependencies: jai-imageio-core, jai-imageio-jpeg2000

But nothing works. Could you please help me what is the issue

Avatar

Administrator

@Rahul_Dubey If you have found out solution yourself, please share it with the community.



Kautuk Sahni