Apache FOP: The file format is not supported. No ImagePreloader found for - apache-fop

I'm trying to generate PDF using Apache FOP with version 2.7. But unfortunately i got the following error, when i try to insert a jpg image into PDF:
14:43:02.322 [main] ERROR org.apache.fop.apps.FOUserAgent - Image not available. URI: . Reason: org.apache.xmlgraphics.image.loader.ImageException: The file format is not supported. No ImagePreloader found for (No context info available)
org.apache.xmlgraphics.image.loader.ImageException: The file format is not supported. No ImagePreloader found for at org.apache.xmlgraphics.image.loader.ImageManager.preloadImage(ImageManager.java:181)
Here is my added FOP dependency POM.XML:
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>2.7</version>
</dependency>
Thanx a lot in advance!

Related

Why is tessdata throwing Exceptions?

All is happening on MAC in SpringBoot WAR file deployed in Tomcat:
I have tesseract 4.1.0 installed (via brew on Mac).
For Tessdata - OCR on Mac
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>4.4.0</version>
</dependency>
I also updated SpringBoot today to:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Now, when I try to run the OCR logic in tessdata (Service method in SpringBoot), I am first getting this error (on the mac):
java.lang.NoSuchMethodError: com.sun.jna.Native.load(Ljava/lang/String;Ljava/lang/Class;)Lcom/sun/jna/Library;
Then, if I try to OCR the Spanish png file again, I get this result:
"Could not initialize class net.sourceforge.tess4j.TessAPI"
The Tessdata code where the crash occurs seems to be:
public String doOCR(BufferedImage var1) throws TesseractException {
return this.doOCR((BufferedImage)var1, (Rectangle)null);
I've seen postings here about this problem, but all the ones I've seen are for Windows. I also tried to load the dependency for jna just in case, but that made no difference.
Has anyone faced and fixed this issue?
The problem appears to be with the tess4j 4.4.0. If I back it out to the last version, then things work again.
Would really appreciate some ideas.
It seems your application is loading an old JNA version. com.sun.jna.Native.load is new in JNA 5.x.x. So make sure you use the latest JNA version.
https://groups.google.com/forum/#!topic/jna-users/VKbCGUcotkg

Docx4j java.lang.UnsupportedOperationException: No ImageHandler available for image when converting from docx to pdf

I am using Docx4j to first create a .docx file (working as intended), then convert the docx file to pdf using Docx4j.toFO(). The file contains images, and is throwing this stack trace when executed.
org.docx4j.openpackaging.exceptions.Docx4JException: Exception exporting package
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:97) ~[docx4j-3.1.0.jar:na]
at org.docx4j.Docx4J.toFO(Docx4J.java:475) ~[docx4j-3.1.0.jar:na]
...
Caused by: java.lang.UnsupportedOperationException: No ImageHandler available for image: file:/usr/tomcat/apache-tomcat-7.0.62/temp/df7fc224-b424-4083-9b85-45032b5ff6e5image1.png (image/png) (org.apache.xmlgraphics.image.loader.impl.ImageRawStream)
at org.apache.fop.render.intermediate.AbstractIFPainter.drawImage(AbstractIFPainter.java:218) ~[fop-1.0.jar:na]
Relevant POM.xml entries:
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>3.2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>2.1</version>
</dependency>
Code:
FOSettings foSettings = Docx4J.createFOSettings();
String outputfilepath = System.getProperty("java.io.tmpdir") + "/form.pdf";
OutputStream os = new java.io.FileOutputStream(outputfilepath);
foSettings.setWmlPackage(wordMLPackage);
if (foSettings.getImageHandler() == null) {
logz.debug("Image handler is null");
foSettings.setImageHandler(foSettings.getImageDirPath() != null ?
new FOConversionImageHandler(foSettings.getImageDirPath(), true) :
new FOConversionImageHandler());
}
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
docx4j currently uses FOP 1.1. (We may make forthcoming docx4j 3.3.0's newly separate docx4j-export-fo project use a more recent FOP over coming days)
So FOP 1.1 ought to work. Otherwise, it sounds like you need to configure an image handler for PNG. Might relate to your xmlgraphics-commons jar?

PDFStamper fails on several PDF files (itext 5.5.1)

I'm trying to fill PDF forms with data and got PDFStamper (itext version 5.5.1) to work with several PDF files, but on some it always fails. Sample code:
PdfReader reader = new PdfReader(new FileInputStream("C:/Temp/source.pdf"));
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("C:/Temp/temp.pdf"));
stamper.close();
Two different error messages until now, first one:
Exception in thread "main" java.lang.AbstractMethodError: javax.xml.parsers.DocumentBuilderFactory.setFeature(Ljava/lang/String;Z)V
at com.itextpdf.xmp.impl.XMPMetaParser.createDocumentBuilderFactory(XMPMetaParser.java:423)
at com.itextpdf.xmp.impl.XMPMetaParser.<clinit>(XMPMetaParser.java:71)
at com.itextpdf.xmp.XMPMetaFactory.parseFromBuffer(XMPMetaFactory.java:167)
at com.itextpdf.xmp.XMPMetaFactory.parseFromBuffer(XMPMetaFactory.java:153)
at com.itextpdf.text.pdf.PdfStamperImp.close(PdfStamperImp.java:337)
at com.itextpdf.text.pdf.PdfStamper.close(PdfStamper.java:208)
Second:
java.lang.reflect.InvocationTargetException
[...]
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.itextpdf.xmp.impl.XMPMetaParser
at com.itextpdf.xmp.XMPMetaFactory.parseFromBuffer(XMPMetaFactory.java:167)
at com.itextpdf.xmp.XMPMetaFactory.parseFromBuffer(XMPMetaFactory.java:153)
at com.itextpdf.text.pdf.PdfStamperImp.close(PdfStamperImp.java:337)
at com.itextpdf.text.pdf.PdfStamper.close(PdfStamper.java:208)
The error messages vary, maybe because of different environments (full application vs. test class, but both using itext-5.5.1.jar).
One of the working PDF form was created using OpenOffice Writer, which produced a version 1.4 (Acrobat 5.x) document. The failing PDF form was created using Acrobat Distiller 7.0.5, pdf version 1.6 (Acrobat 7.x). I already tried to convert it down to version 1.4/5.x without luck.
Any ideas?
We had the same problem in our project, where we used FOP for generating PDF and iText for PDF signing.
FOP has a dependency on the xercesImpl:xerces jar. This jar is important for JRE <= 1.4 but it isn't needed in the JRE>1.4 and works without it (more information on JDK 1.6 and Xerces?).
Ps: Check that you do not have the xerces library on classpath, if so remove it.
I had the same problem (with xerces), I could fix it by adding an exclusion in my dependency:
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j-fop-ext-complete</artifactId>
<version>2.0</version>
<type>jar</type>
<exclusions>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</exclusion>
</exclusions>
</dependency>

freemarker.runtime - Template processing error: "No mapping defined for http://www.springframework.org/security/tags"

Spring-security:3.0.2
Freemarker:2.3.19
I want to use springSecurity tags in freemarker, but it throws this errors:
[16 14:10:46,349 ERROR] [btpool0-0] freemarker.runtime - Template processing error: "No mapping defined for http://www.springframework.org/security/tags"
No mapping defined for http://www.springframework.org/security/tags
The problematic instruction:
==> assignment: security=JspTaglibs["http://www.springframework.org/security/tags"] [on line 1, column 1 in macro/header.ftl]
in user-directive page.bodytemplate [on line 6, column 1 in items.ftl]
ftl:
<#assign security=JspTaglibs["http://www.springframework.org/security/tags"] />
pom.xml
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
if the problems happen with tomcat7-maven-plugin and not with jetty-maven-plugin you most likely suffer from the not fully initialized servlet context of tomcat7.
Instead of "tomcat:run", do "tomcat:run-war"
the latter makes sure that your web app is (almost) fully being build and bootstrapped, like having jars in /WEB-INF/libs. freemarker's scanning logic for tlds will work like a charm.
It seems like a tag library which you want to download contains the tag org.springframework.security.taglibs.authz.AclTag.
The last version of Spring Security taglibs (which had this class) was the 2.0.7 release, hence the problem.
You can change you spring to 2.0 version. Or you can download security.tld from https://src.springframework.org/svn/spring-security/branches/spring-2.5-integration-branch/taglibs/src/main/resources/META-INF/security.tld and use it on freemarker template like this: <#assign security=JspTaglibs["/WEB-INF/tlds/security.tld"] />
By the way I really don't understand why http://www.springframework.org/security/tags doesn't work... I have tried to use it like you, and I have faced the same issue.
I encountered this error when switched to run-jetty-run maven module.
Module jetty:run didn't had this error.
This helped me:
Download spring-security.tld file and put it in webapp/WEB-INF/tld folder
Add this code to web.xml:
<jsp-config>
<taglib>
<taglib-uri>http://www.springframework.org/security/tags</taglib-uri>
<taglib-location>/WEB-INF/tld/spring-security.tld</taglib-location>
</taglib>
</jsp-config>
The reason why this doesn't work has to do with the Servlet container (and maybe the spec). If you're associating taglibs by the namespace it will only do that automatically if the JAR is in the WEB-INF/lib folder.
If you move the spring-security-taglibs.jar into the WEB-INF/lib folder, it will work.

Apache CXF RS Extensions issue in 2.4.0

I'm using Apache CXF version 2.4.0. I'm trying to create a Restful service.
The below example works in 2.3.4 but does not work in 2.4.0. What should I do different in beans config file?
When I include the below xml line in my bean config file.
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
I get the following stacktrace error:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Failed to
import bean definitions from URL
location
[classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml]
Offending resource: ServletContext
resource [/WEB-INF/beans.xml]; nested
exception is
org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from
class path resource
[META-INF/cxf/cxf-extension-jaxrs-binding.xml];
nested exception is
java.io.FileNotFoundException: class
path resource
[META-INF/cxf/cxf-extension-jaxrs-binding.xml]
cannot be opened because it does not
exist at
org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
My POM dependency is below. This will work for 2.3.4 but not for 2.4.0. Any suggestions? Is that xml extension line deprecated or included in another jar?
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle-jaxrs</artifactId>
<version>2.3.4</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</exclusion>
</exclusions>
</dependency>
It's no longer needed. With 2.4.x, most of the META-INF/cxf files are no longer needed and can be removed. Really, the cxf.xml one is the only one needed.
Here is the CXF information about embedding cxf inside spring in version 2.4 and above.
http://cxf.apache.org/docs/embedding-cxf-inside-spring.html