PDFStamper fails on several PDF files (itext 5.5.1) - itext

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>

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)

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.

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?

DynamicJasper and Asian characters

My PDF generated by DynamicJasper now looking exactly as it's supposed to, I am facing only one more problem: Asian characters are not displayed at all in the resulting PDF. Any other characters work fine. I can verify in the debugger that the Strings are properly placed in the JRDataSource, and Jasper does in fact generate lines for them in the report, but the text itself is completely missing.
Are there some additional encoding settings to be considered when using DynamicJasper with Asian text elements?
Thanks for any advice and best regards
Okay, so this is what fixed the issue, step by step:
1.) Add the Arial Unicode MS font extension JAR to your classpath (or any other equivalent unicode font). An "official" one can be found here, I ended up using this one, however. If you're using maven, you can use the following mvn command line and POM entry to add the file to your classpath:
mvn install:install-file -Dfile=DynamicJasper-arial-unicode-fonts-1.0.jar -DgroupId=ar.com.fdvs -DartifactId=DynamicJasper-arial-unicode-fonts -Dversion=1.0 -Dpackaging=jar
<dependency>
<groupId>ar.com.fdvs</groupId>
<artifactId>DynamicJasper-arial-unicode-fonts</artifactId>
<version>1.0</version>
</dependency>
2.) Add, if not already present, spring-core and spring-beans to the project:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
3.) Enable "Identity-H" Jasper PDF encoding:
static {
JRProperties.setProperty("net.sf.jasperreports.default.pdf.encoding", "Identity-H");
}
4.) Configure Arial Unicode MS as your DynamicJasper font (e.g. for rows):
// ...
final DynamicReportBuilder dynamicReportBuilder = new DynamicReportBuilder();
final Style style = new Style();
style.setFont(new Font(Font.MEDIUM, "Arial Unicode MS", false));
dynamicReportBuilder.setDefaultStyles(null, null, null, style);
// ...
That was some annoying crap :-/ ...
you may have to configure jasper report exporter to embed special fonts on the pdf file. also check ck the pdf character encoding

iTextPDF Library - Can't find the class "com.itextpdf.text.pdf.PdfAWriter"

Can't find the class "com.itextpdf.text.pdf.PdfAWriter" in latest version of itextpad v5.4.0. I have even tried downloading the extra jars but to no avail.
Any help will be appreciated.
I'm the original developer of iText; I made the 5.4.0 release, but I've never heard of itextpad. If you download the release from SourceForge, you'll find PdfAWriter in the extra jar named itext-pdfa-5.4.0.jar.
As said by Bruno, PDF/A Libraries are in a different jar file: itext-pdfa.
Here the Maven dependency:
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-pdfa -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-pdfa</artifactId>
<version>5.5.13</version>
</dependency>