Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 998
Joyce Guo Avatar asked Feb 05 '23 16:02

Joyce Guo


1 Answers

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.

like image 113
Bruno Lowagie Avatar answered Feb 16 '23 17:02

Bruno Lowagie