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

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?

Related

java.lang.NoClassDefFoundError: javax/xml/transform/TransformerConfigurationException

I am using dummy html string and trying to create the pdf from that...
once it tries to create ITextRenderer object, I am getting the "java.lang.NoClassDefFoundError: javax/xml/transform/TransformerConfigurationException"
Document doc = Jsoup.parse("<html><head><title>Pdf Generation..!</title></head><body><p>Pdf generated using flying saucer pdf openpdf!!!!</p></body></html>","UTF-8");
doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
try (OutputStream os = new FileOutputStream("output.pdf")){
ITextRenderer renderer = new ITextRenderer();
SharedContext cntxt = renderer.getSharedContext();
cntxt.setPrint(true);
cntxt.setInteractive(false);
renderer.setDocumentFromString(doc.html(), "");
renderer.layout();
renderer.createPDF(os);
logger.info("PDF Generation using OpenPDF Done Successfully!!!");
}
catch(Exception ex){
ex.printStackTrace();
}
This is a maven archetype project and dependencies used for this are,
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-openpdf</artifactId>
<version>9.1.20</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.1.20</version>
</dependency>
I have looked through some of the shared suggestions, but none of it could resolve this...
Check the Import-Package directive in the BND Maven plugin. Either import javax.xml.transform explicitly or import everything (*)
javax.xml.transform.* is a provided API which is present in many bundle definitions. The correct Import-Package should include something like this:
javax.xml.transform,version=2.1.0 from org.apache.felix.framework (0)

Error while creating building a hibernate app without Maven

I am learning to hibernate so I made a small app for crud operation using mySQL as database. However, I am getting some errors and I cannot find the solution anywhere. SDK 17.0.2, I am not using maven , Also all hibernate final jar files have been added
my class:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.luv2code.hibernate.demo.entity.Student;
public class CreateStudentDemo {
public static void main(String[] args) {
// Create session factory
SessionFactory factory= new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Student.class)
.buildSessionFactory();
//Create session
Session session = factory.getCurrentSession();
try {
//Create a Student object
System.out.println("Creating new student");
Student tempStudent = new Student("Pau;", "Wall", "paul#luv2code.com");
//Start a transaction
session.beginTransaction();
//save the student object
session.save(tempStudent);
//commit transaction
session.getTransaction().commit();
}
finally {
factory.close();
}
}
}
runtime error :
java.security.PrivilegedActionException: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
Caused by: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.reflect.Method.invoke(Object, Object[])" because "com.sun.xml.bind.v2.runtime.reflect.opt.Injector.defineClass" is null
java removed java.xml.bind from JAVA 9 and higher editions.
You need to add these jar files manually to your library
basically you can to that in this way.
If you use Intellij this is the easyest way:
jaxb-impl-2.3.0.jar
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
jaxb-core-2.3.0.jar
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
jaxb-api-2.3.1.jar
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
javax.activation-api-1.2.0.jar
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
If you use intellij here is a step my step to get these files through IDE
go to project structure:
Libraries, and click + sign
Chick to from Maven:
Paste these dependencies to the search.
It will automatically find exact jar files to download, Kist Paste it and click to the enter that is it.
Also later you can just add these files to the classpath just in case:
java removed java.xml.bind from JAVA 9 and higher editions.
Here is the eclipse solution:
In Eclipse IDE:
Download these jar files and paste them inside lib folder:
Go the project properties: The last line
Done these steps:
Happy Coding! :)

Replace JasperReport iText 2.1.7 with latest iText 7.0.1

We are using JasperReport 6.1.0 which has a dependency to com.lowagie:itext:jar:2.1.7.js2. It looks like iText 2.1.7 has IP issue, and iText is asking all users to use the latest version which requires a commercial license. So we would like to buy iText license. Now iText latest version is 7.0.1. I tried did below steps to replace JasperReport's iText 2.1.7 with latest iText 7.0.1:
1. Exclude default itext 2.1.7 dependency in pom.xml
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.1.0</version>
<exclusions>
<exclusion>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
</exclusion>
...
2. Add new iText jars in pom.xml
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>barcodes</artifactId>
<version>7.0.1</version>
<!-- barcodes depends on kernel -->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>7.0.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>forms</artifactId>
<version>7.0.1</version>
<!-- forms depends on kernel and layout -->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>hyph</artifactId>
<version>7.0.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.0.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.0.1</version>
<!-- kernel depends on io -->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.0.1</version>
<!-- layout depends on kernel -->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>pdfa</artifactId>
<version>7.0.1</version>
<!-- pdfa depends on kernel -->
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>sign</artifactId>
<version>7.0.1</version>
<!-- sign depends on kernel, layout and forms -->
</dependency>
3. Run mvn and test report function, I got below error:
2016-11-17 14:43:36,520 ERROR [c.i.c.d.DeferredOperationManager] [ Thread-49] Exception on Deferred Operation. Operation UUID: 2a647922-d6d0-450d-9b2d-4d97638ba03f. UI Error key:d9a16093-be20-4278-9f8b-93120c0a2231 - Error: java.lang.NoClassDefFoundError: com/lowagie/text/SplitCharacter
It looks like JasperReport is trying to find old iText classes which namespace is "com.lowagie...".
I tried to unzip the new iText 7.0.1 jar, the classes are in package "com.itextpdf...".
How can I make the JasperReport call the new iText jar?
You can not replace iText 2.1.7 with iText 7 because the differences between the two versions are too big. We do know of some people who moved to using iText 5 with JasperReports. That requires a number of changes to JasperReports such as changing the package names from com.lowagie to com.itextpdf (*) and changing references to com.lowagie.text.Color to com.itextpdf.text.BaseColor.
At iText, we noticed that we were hitting the ceiling with iText 5. For instance: we store text using char which means that each character is stored using only 2 bytes. That wasn't sufficient if we wanted to support Hindi. We had to rewrite the complete font layer if we wanted to add support for Indic languages to iText. "Replacing the font layer" in iText 5 would have been really difficult since the font layer is the foundation on which all the rest of the code is built. Hence our decision to rewrite the complete API.
You can watch a video that goes into more detail regarding this decision here: Devoxx 2016: "Oops I broke my API"
However: the biggest problem with replacing iText 2.1.7 with iText 7, is that JasperReports depends on PdfGraphics2D and we haven't ported that part to iText 7 (yet). We might even decide not to port that part ever, because it is impossible to create PDF/UA if you choose to use PdfGraphics2D (and PDF/UA is getting more and more important).
(*) In 2009, I decided to remove my name from the package names. When I first released iText, I only owned the lowagie.com domain, and I used com.lowagie packages for all the Java code I wrote. I didn't expect iText to become such a success. When everyone started to use iText, everyone started to ask me questions personally. I didn't have a life anymore. Hence I created a company, we professionalized iText and replacing com.lowagie with the more neutral com.itextpdf was one of those professionalizations.

unable to perform multipart form upload using rest-assured

I am trying to upload file using multipart form upload using the following rest-assured method.
given().filter(new RequestLoggingFilter(captor)).when().multiPart("metadata", new File("S:\\testdata.prop")).multiPart("file",aFileStream).post("/uploadFile").then().statusCode(200);
This throws an error that
com.fasterxml.jackson.databind.Module: Provider
com.fasterxml.jackson.datatype.joda.JodaModule could not be
instantiated.
jackson library is added to classpath.the testdata.Prop file is a properties file
You need to add jackson-databind to the classpath. If you're using Maven you can add it like this:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.2</version>
</dependency>
Also if you're using JodaTime you may need to add this dependency:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.6.2</version>
</dependency>
REST Assured automatically tries to register all jackson modules in classpath.

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>