<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ahmed galal</title>
    <description>The latest articles on DEV Community by ahmed galal (@deathwaiting).</description>
    <link>https://dev.to/deathwaiting</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F669979%2Faa489761-8f5d-48b2-a572-db1e257fd833.png</url>
      <title>DEV Community: ahmed galal</title>
      <link>https://dev.to/deathwaiting</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deathwaiting"/>
    <language>en</language>
    <item>
      <title>JasperReports with Arabic</title>
      <dc:creator>ahmed galal</dc:creator>
      <pubDate>Sat, 28 Sep 2024 17:21:49 +0000</pubDate>
      <link>https://dev.to/deathwaiting/jasperreports-with-arabic-12pk</link>
      <guid>https://dev.to/deathwaiting/jasperreports-with-arabic-12pk</guid>
      <description>&lt;p&gt;JasperReports is a great reporting library that can produce high quality pixel-perfect reports. Unfortunately, creating reports in Arabic language can be bit tricky if you need to produce a high quality report, especially in PDF format.&lt;br&gt;
This article is actually a remake of a previous article I made on my blog long ago, but here I add more updates, in addition to discussing how to produce Arabic reports using &lt;a href="https://github.com/deathwaiting/jasper-reports-api" rel="noopener noreferrer"&gt;Jasper report API&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  So, What is the problem?
&lt;/h2&gt;

&lt;p&gt;When trying to create PDF reports in Arabic, the most common problems I met were :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Empty Pages:
You create your report on JasperSoft Studio, you select a nice font that supports Arabic like &lt;code&gt;Arial&lt;/code&gt;, it runs greate in the preview. Then you try producing a PDF file and get a blank page.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh4j3mzedouo6bteodi50.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh4j3mzedouo6bteodi50.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;No Hindi numerals:&lt;br&gt;&lt;br&gt;
The numerals used in English language - like 1,2,3, .. - that we all use and love are called "Arabic numerals". However Arabic and some other eastern languages use other numerals called "Hindi numerals" : (١, ٢, ٣ , ...) - It is a bit confusing, I Know 😅 ! -. While many organizations do tolerate using Arabic numerals in their Arabic reports, others simply don't, and they may consider it a hard requirement for audit reasons. And generally, using numerals of a different language makes the report looks more sloppy and less professional.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"It works on my machine" scenario&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You solve the above problems on your machine, and the report work fine, just for them to appear again either during runtime, or on other developers machines. Whether you are generating the reports in your application, Jasper Server or Jasper Reports API, the runtime environment will need some configuration.&lt;/p&gt;

&lt;p&gt;So, let's fix these one-by-one!&lt;/p&gt;
&lt;h2&gt;
  
  
  Problem 1: Empty Pages PDF Reports
&lt;/h2&gt;

&lt;p&gt;This is not a problem specific to Arabic itself, but using a non-latin language makes it more common. The reason is simple:  &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                                  The font you used is not available at runtime!  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This may seem confusing at first, especially when you run the report on JasperSoft Studio itself. I mean it was running just fine with the java preview, right ?!&lt;br&gt;&lt;br&gt;
Well, for PDF generation, the font must be available in the java runtime classpath. If you are not familiar with java, a classpath is basically collection of locations for additional resources and libraries that your program need while running. Typically it can be a directory or a java archive (Jar).  &lt;/p&gt;

&lt;p&gt;The confusion actually comes from the fonts list in Jasper studio. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9uz63sd3zyuhnhq0q069.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9uz63sd3zyuhnhq0q069.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If look at it, you will find a separator line between some fonts at the top. Fonts above the line are in the classpath of JasperSoft studio - which is a java application itself -, therefore, they can be used for PDF generation. Other fonts are the fonts available on your system, these will work in the java preview, or when exporting to formats like docx or xlsx on the same machine.&lt;br&gt;&lt;br&gt;
In addition to the PDF problem, relying on system fonts can break the report generation on other systems if they don't have the same fonts. For example if a developer works on a windows machine, and used the font &lt;code&gt;Arial&lt;/code&gt; which comes pre-installed with windows, the report won't run as expected on a linux machine, because they don't have &lt;code&gt;Arial&lt;/code&gt; installed.&lt;/p&gt;

&lt;p&gt;The simple solution is to use a font already existing in the classpath. JasperReports actually provides a library with some default fonts called &lt;code&gt;jasperreports fonts&lt;/code&gt;, these are the ones you find by default above the separator line in the font list. While some of these fonts supports Arabic like &lt;code&gt;Dejavu Sans&lt;/code&gt;, none of them looks great. Official documents in Arabic usually use &lt;code&gt;Arial&lt;/code&gt;, or maybe &lt;code&gt;Times New Roman&lt;/code&gt;, and users will usually consider &lt;code&gt;Dejavu Sans&lt;/code&gt; visually inferior to them. But if your users are Ok with it, just use the default fonts, it will save even more effort on configuring the runtime because the default fonts are usually included in your report server. If your application generates the report then you will just need to adds the fonts dependency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;net.sf.jasperreports&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;jasperreports-fonts&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;${version.jasperreports}&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, but what if we need a more professionally looking report ? You will need to add your fonts to the classpath.  &lt;/p&gt;

&lt;p&gt;Luckily, this is a simple process, JasperSoft Studio already provides a tool for quickly doing this :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, you will need to get your font files, usually TTF.&lt;/li&gt;
&lt;li&gt;From the main menu open &lt;code&gt;Window&lt;/code&gt; -&amp;gt; &lt;code&gt;Preferences&lt;/code&gt; -&amp;gt; &lt;code&gt;JasperSoft Studio&lt;/code&gt; -&amp;gt; &lt;code&gt;Fonts&lt;/code&gt; -&amp;gt; &lt;code&gt;Add&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;This will open a dialog for adding the font where you provide the font file for each mode : &lt;code&gt;Normal&lt;/code&gt;, &lt;code&gt;Bold&lt;/code&gt;, &lt;code&gt;Italic&lt;/code&gt;. You should at least provide a file for &lt;code&gt;Normal&lt;/code&gt;, the rest are extra.&lt;/li&gt;
&lt;li&gt;A critical part here will be checking &lt;code&gt;Embed this font in PDF document&lt;/code&gt; and setting PDF encoding to &lt;code&gt;Identity-H&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Press &lt;code&gt;finish&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frup60u6879nmd1hrm0fk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frup60u6879nmd1hrm0fk.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After finishing, your font should now appear above the separator line in the font list, indicating it is now "installed" by JasperSoft Studio. Now, if you used this font for your text elements, they should now appear in the PDF file!  &lt;/p&gt;

&lt;p&gt;The good news is: You now have a PDf report that is not empty, bad news is : It probably won't work elsewhere!&lt;br&gt;&lt;br&gt;
This is because you installed the font to the IDE only, but you need to make your font accessible to other runtimes as well. We will discuss this later with problem 3.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem 2: No Hindi numerals
&lt;/h2&gt;

&lt;p&gt;Congradulations! You now have a readable PDF report!&lt;br&gt;&lt;br&gt;
But your prefectionist eye quickly catches something unsettling : All numbers and dates are in English :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fem3i3vz7s6gqmewwga38.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fem3i3vz7s6gqmewwga38.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As discussed before, many organizations will have no problem with that, but others will have higher standards, assuming you are from the later, let's try to fix this. The fastest fix will be to change the report default locale to &lt;code&gt;Arabic&lt;/code&gt;, and it is fairly simple to do it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select your report in the overview tab, or just click on an empty space outside the design&lt;/li&gt;
&lt;li&gt;In &lt;code&gt;Properties&lt;/code&gt; pane select &lt;code&gt;Advanced&lt;/code&gt; tab -&amp;gt; &lt;code&gt;Misc&lt;/code&gt; -&amp;gt; The button on right of &lt;code&gt;Edit Properties&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;This will open the report properties window. Search for &lt;code&gt;net.sf.jasperreports.default.locale&lt;/code&gt; and select one of the Arabic locale's.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you can't change the locale for some reason, the other option will be using a &lt;code&gt;FormatFactory&lt;/code&gt;. &lt;br&gt;
&lt;code&gt;FormatFactory&lt;/code&gt; is an interface in jasper reports that defines methods for doing a custom formatting on numeral fields and dates. This should be used if the default pattern-based formatting is not enough. In our case we use &lt;a href="https://github.com/deathwaiting/japerreports-arabic" rel="noopener noreferrer"&gt;jasperreports-arabic&lt;/a&gt; library, which provides simple utils for supporting Arabic in jasper reports. The library should be available on maven central and the github repository, after downloading the jar file you should :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Copy it to your JasperSoft Studio project, then right-click -&amp;gt; &lt;code&gt;Build Path&lt;/code&gt; -&amp;gt; &lt;code&gt;Add to Build Path&lt;/code&gt;. This will add the library to the project classpath and make it available for reports.&lt;/li&gt;
&lt;li&gt;Select your report in the overview tab, or just click on an empty space outside the design&lt;/li&gt;
&lt;li&gt;In &lt;code&gt;Properties&lt;/code&gt; your will find &lt;code&gt;Format Factory&lt;/code&gt; field, press the the button next to it.&lt;/li&gt;
&lt;li&gt;This will open a dialog for selecting classes, search with keyword &lt;code&gt;Hindi&lt;/code&gt;, the class &lt;code&gt;HindiNumeralsFormatFactory&lt;/code&gt; should appear in the results. Select it and press OK.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Regardless of the method, if you run your report now, you will notice that numbers and dates are shown in arabic with hindi numerals.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzjhqg5jj5asowt4faf8x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzjhqg5jj5asowt4faf8x.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But wait a second! It seems that there are still english numbers in the title of this report! &lt;br&gt;
Well, that's because changing the locale and &lt;code&gt;FormatFactory&lt;/code&gt; works only on TextFields whose expression result is either numeric (int, long, double , ..) or &lt;code&gt;java.util.Date&lt;/code&gt;. The title here is actually a string which has a number inside it.&lt;br&gt;&lt;br&gt;
If the string is static, you may actually just replace the Arabic numerals with hindi ones manually, but more often the string is dynamic and is being fetched from some database. So, what can we do?  &lt;/p&gt;

&lt;p&gt;To fix that problems, we need to process the string first to replace all Arabic numerals with Hindi numerals. Luckily &lt;code&gt;jasperreports-arabic&lt;/code&gt; already provides a utility method for that : &lt;code&gt;io.github.deathwaiting.jasperreports.arabic.HindiNumeralsUtils.toHindiNumerals()&lt;/code&gt;. Apply the method to the string in the expression editor, then try running the report.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1cpacogifbb8dmen1rxy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1cpacogifbb8dmen1rxy.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After using the method in the Expression editor and running, you can see that the title now is rendered with Hindi Numerals.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4btydb0c4mavurc5rmgg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4btydb0c4mavurc5rmgg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will find that this method is commonly used through out the report, so, to avoid using the fully qualified name of the class, you can import the class to the report instead :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;code&gt;Properties&lt;/code&gt; your will find &lt;code&gt;Imports&lt;/code&gt; field, press the the button next to it.&lt;/li&gt;
&lt;li&gt;In the dialog, Add the class &lt;code&gt;io.github.deathwaiting.jasperreports.arabic.HindiNumeralsUtils&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Now you can use &lt;code&gt;HindiNumeralsUtils.toHindiNumerals()&lt;/code&gt; directly.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Problem 3: "It works on my machine!"
&lt;/h2&gt;

&lt;p&gt;"It works on my machine!" is a famous quote, said by many many programmers, young and old, regardless of the technology or the era. We don't want to say it :).  &lt;/p&gt;

&lt;p&gt;In the last sections, we solved the problems of missing fonts that led to blank PDF's, and we solved the problem of Hindi numerals mainly by using a third party library. In both cases we basically added a new dependency to our report, and for the report to work, those dependencies should be available and in the classpath of other environments that will run the report. So, the focus of this section will be about how to provide the used dependencies in :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your reports project. So JasperSoft Studio can run the report on other machines.&lt;/li&gt;
&lt;li&gt;Your report server. We will talk about &lt;a href="https://github.com/deathwaiting/jasper-reports-api" rel="noopener noreferrer"&gt;Jasper report API&lt;/a&gt; and Jasper Server.&lt;/li&gt;
&lt;li&gt;Your java application. In case the reports are generated by the application directly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, lets start by the first step.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exporting fonts
&lt;/h3&gt;

&lt;p&gt;If you are not familiar with java, jar stands for &lt;code&gt;Java Archive&lt;/code&gt;, which are basically just zip archives containing java classes and other resources. Jar files are the main way to package libraries and applications in the java ecosystem, and even the java virtual machine can run applications packaged as jar directly.&lt;br&gt;
To be able to run the fonts you "installed" on JasperStudio on other environments, you will need to export them as jar files, then import those jar files in other environments. That's the main idea of this section.  &lt;/p&gt;

&lt;p&gt;To export the fonts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From the main menu open &lt;code&gt;Window&lt;/code&gt; -&amp;gt; &lt;code&gt;Preferences&lt;/code&gt; -&amp;gt; &lt;code&gt;JasperSoft Studio&lt;/code&gt; -&amp;gt; &lt;code&gt;Fonts&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Select the font you want to export from the list, then press &lt;code&gt;Export&lt;/code&gt;, and select the location of the jar&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it 🙂. We will then use those font jars in other environments. But each environment will need different method.&lt;/p&gt;

&lt;h3&gt;
  
  
  Preparing your reports project
&lt;/h3&gt;

&lt;p&gt;First, let me tell you some well-known "Secret": Tibco didn't actually build JasperSoft Studio from the ground up!&lt;br&gt;&lt;br&gt;
JasperSoft Studio is actually a plugin on the well known java IDE &lt;code&gt;eclipse&lt;/code&gt;. This makes sense, as jasperreports is basically a java library. But more importantly, because JasperSoft Studio is essentially a java IDE, you get other features out of the box: like project dependencies management, git repositories support, navigation for jar files, class search , etc.  &lt;/p&gt;

&lt;p&gt;What this has to do with the topic? Well, basically, the reports projects you create on JasperSoft Studio are nothing but java projects, and those can have jar files saved into them, and can be configured to add those jars to the classpath, your font jars are not an exception.  &lt;/p&gt;

&lt;p&gt;In more simple terms, you copy your font jars into the report project, right-click -&amp;gt; &lt;code&gt;Build Path&lt;/code&gt; -&amp;gt; &lt;code&gt;Add to Build Path&lt;/code&gt;. And by just doing this, these fonts will be added to the project itself, not just JaserSoft Studio. If the same project is opened on another machine, you should find these fonts automatically added to the top of the font list! Your team members don't need to install them from TTF files again. This also means if your team works on a shared report repository, you can share certain fonts to be used by the team to standardize the looks of your reports.&lt;/p&gt;

&lt;p&gt;The same goes for third-party libraries like &lt;code&gt;jasperreports-arabic&lt;/code&gt;. Just copy the jar and add it to the build bath and share it on the repository, and it will be available to the team.&lt;/p&gt;

&lt;p&gt;This is actually a crucial step to unify the development environment for the whole team.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preparing Jasper Reports API
&lt;/h2&gt;

&lt;p&gt;If you are using &lt;a href="https://github.com/deathwaiting/jasper-reports-api" rel="noopener noreferrer"&gt;Jasper report API&lt;/a&gt; as your report server, then the good news is you don't need to do much as long as your reports repository, along with the jars, is provided as the report root directory for the server.&lt;br&gt;&lt;br&gt;
Jasper Report API will add all the jar files inside the report root directory and its sub-directories to the classpath. This means all your font jars and third-party libraries will be loaded on startup and be available for the reports. But be aware of adding multiple versions of the same library/font, as only one version will be loaded, better to set a central directory for all jars to avoid duplication.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preparing Jasper Server
&lt;/h2&gt;

&lt;p&gt;Jasper Server on the other hand has to be configured manually to add each jar file and make it available to the reports. But the good news is, JasperSoft Studio has a very good integration with Jasper Server. If you connect to the server using the studio, you can copy paste the jar files as resources for each report.&lt;br&gt;&lt;br&gt;
The good point her, is that each report can probably have its own version of the jar file if needed - AFAIK, didn't try it before -. Bad news is, you will get a lot of duplicated jar files.  &lt;/p&gt;

&lt;p&gt;A good solution will be to make a central directory for the common jar files, and add a &lt;code&gt;link&lt;/code&gt; to the jar in each report, this will give access to the reports to use the same jar.  In JasperSoft Studio this can be done by:&lt;br&gt;
in Repository explorer pane -&amp;gt; copy the jar file -&amp;gt; right click on the report unit -&amp;gt; &lt;code&gt;paste as link&lt;/code&gt;.&lt;br&gt;
You will notice that an icon for the jar file appeared under the report unit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Preparing your application
&lt;/h2&gt;

&lt;p&gt;I won't go in details here, because there are many build tools in the java ecosystem and each project have its own thing. But if you are a java developer, you will know what to do with your jar files :). In the end you just need to add them to your project somehow. &lt;/p&gt;

&lt;p&gt;That's will be the end of this tutorial, I hope it helps you out with your next project to make outstanding reports 🙂 !&lt;br&gt;
While this may look dull for us developers, but a good looking report can impress your end users and give a first impression of a high quality system for your potential clients, and of course : first impressions last .... hopefully 🙂.&lt;/p&gt;

&lt;p&gt;Lastly, please feel free to add your thoughts and suggestions on the topic, Peace!&lt;/p&gt;

</description>
      <category>jasperreports</category>
      <category>backend</category>
      <category>java</category>
    </item>
    <item>
      <title>JasperReports in microservices</title>
      <dc:creator>ahmed galal</dc:creator>
      <pubDate>Tue, 24 Sep 2024 21:01:12 +0000</pubDate>
      <link>https://dev.to/deathwaiting/jasperreports-in-microservices-5a6i</link>
      <guid>https://dev.to/deathwaiting/jasperreports-in-microservices-5a6i</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Microservices has almost became the defacto architecture in many current projects, and while you may agree or disagree with the need for microservices, the reality is it is a very common architecture now, especially in modern enterprise applications.&lt;br&gt;
And in many such applications we are usually faced with the need for providing reports to the user, these can be mainly web-based reports like the ones shown in dashboards, or they can be pixel-perfect reports, i.e reports that are expected to be printed by users; bank statements, invoices, salary slips, are all examples of such reports. While using paper-based reports seems obsolete, in many enterprises and government workflows, it is a hard requirement.&lt;/p&gt;

&lt;p&gt;The web-based reports are already done via the frontend technology of the application, which will generate some html for showing the data in the end. The web-based reports aims to be interactive, with reactive design for different screen sizes, and it is mainly optimized to run on browsers.&lt;br&gt;
But in the case of printable reports, we usually aim to provide them as PDF files, and they are designed to be printed on certain paper size like A4. The design objectives are different, and so are the optimizations and required features, for example there is usually the need for page breaks, page header and footers with sections like page numbers, page background images, etc.&lt;br&gt;
This means the tools and design priorities and optimizations are different, and usually conflicting. While we can try to generate the report as a simple html page and let the user print it, it is not that simple, and the results are usually sub-optimal, and because browser print functionality handle basic cases, any more complex requirement can lead to a road block.&lt;/p&gt;

&lt;p&gt;So, the best option is usually to generate pixel-perfect reports with tools dedicated for that, and there are many tools already available; Birt, Pentaho, Jasper Reports, Crystal reports are all example of such tools. Some tools provides SDK's and libraries that you can use in your application directly, others rely on a dedicated report server for generating the reports. Like everything in technology, each option comes with its pros and cons, but when it comes to printable reports, I believe Jasper reports are on the top of the list.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why Jasper reports?
&lt;/h2&gt;

&lt;p&gt;Jasper reports is one of the oldest and most-mature report generation libraries in the java eco-system, having several great features such as :&lt;/p&gt;
&lt;h3&gt;
  
  
  Opensource
&lt;/h3&gt;

&lt;p&gt;It seems at first that money shouldn't be a problem for enterprises, yet, for teams working in enterprises, opensource solutions are usually on the top of the list, while the money can be available for paid solutions, another main factor is ... bearucracy 🙂.&lt;br&gt;
To use a properitary solution, you need to go through a bearucratic limbo in a big organizations, depending on the organization , the process may take months of back-and-forth meetings and procedures, and for technical teamleaders to start such a quest, there have to be some good reasons.&lt;br&gt;
On the otherhand opensource solutions can be used on day 1, as long as the license allows it, this means quick and cheap adoption.&lt;br&gt;
Jasper reports is mainly a java library , which is opensource, therefore, teams can start using it at once, or at least try it in some pilot project. The same goes for the community version of other jasper reports tools like their report designer : Jasper studio, and their report server.&lt;/p&gt;
&lt;h3&gt;
  
  
  Actively developed
&lt;/h3&gt;

&lt;p&gt;A usual problem with opensource projects is developers abandoning the project. With lack of motivated developers or resources, development can halt after sometime. This can be a problem in enterprise applications because they usually have long lifetime, so, you need tools with long lifetime as well. It is not rare to have a system working for a decade in a big organization, because the business it serves actually doesn't change that much through out the decade.&lt;br&gt;
Jasper reports ecosystem is developed by Tibco, a well-know company, and for more than a decade now, the project has bee active and kicking. Tibco profit model here is to provide opensource version of their tools, while providing additional premium features for a price, which seems to work fine for them till now.&lt;/p&gt;
&lt;h2&gt;
  
  
  Mature and Feature rich
&lt;/h2&gt;

&lt;p&gt;Jasper reports is almost 2 decades old, so, we can consider it a mature library 🙂. It already provides a variaty of features covering many use cases. Bugs are rare - for me at least -, and it is well optimized.&lt;br&gt;
Reports can have a variaty of datasources, from database queries, to xml and json files. The library supports generating charts, sub-reports, can use custom fonts, and can generate reports as PDF, docx, xlsx, and other formats.&lt;br&gt;
The tooling is also mature. Their WYSIWYG report designer is more than a decade old and is based on eclipse IDE, it is feature rich and simple to use, and it can even have additional features using eclipse plugins like managing git repositories for reports.&lt;/p&gt;
&lt;h2&gt;
  
  
  Challenges using jasper reports in microservices
&lt;/h2&gt;

&lt;p&gt;So, where is the problem? If we need to use Jasper reports in our microservice application we will just use the library in our services or add the report server to the cluster, right?&lt;/p&gt;

&lt;p&gt;Well, it is not always that simple:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Being a java library means it is accessible mainly by applications written in java or other JVM languages. If you have the services built using .Net for example, this option is out.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It is a heavy weight library, adding a lot of dependencies and like 50 MB  - IIRC - to the application jar file. Adding a heavy library like that to generate a report or two in a single service is usually an overkill, especially if one of the dependencies may mess with something else.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Generating reports on the microservice itself can be problematic if the reports are complex. If your service has many users who generate reports, then the report generation can eat from the overall performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In many cases, the solution is to simply use Jasper report server to handle the report generation for all services, it is battle-proven solution, and will work with any technology via its REST API. The problem is, Jasper report server is not microservices friendly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Jasper report server is an entire report management system, it comes with its own web report designer, its own dashboard for managing reports, users, access management, database connections, etc. It was meant to run as a standalone central monotlithic solution. It needs its own database to run all this, and needs manual configuration to add the reports to the server.&lt;br&gt;
It was made in the era of the monolith, not microservices. So, if the system needs a lighweight scalable report server just to generate arbitrary reports and nothing else, Jasper report server was not built for that.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tibco has realized that as well, and therefore they introduced &lt;a href="https://community.jaspersoft.com/knowledgebase/getting-started/jasperreports-io-getting-started/" rel="noopener noreferrer"&gt;JasperReports IO&lt;/a&gt;, a jasper report server that mainly provides a REST API for generating reports.&lt;br&gt;
Unfortunately it is not opensource 🙂.&lt;/p&gt;

&lt;p&gt;So, we are now in square one, if we have microservices and need a lightweight opensource report server, what should we use?&lt;/p&gt;

&lt;p&gt;I met the same problem with a recent project. We had a simple dotnet service and we needed a good reporting opensource solution. While dotnet had reporting solutions, most were not opensource, and I was familiar with jasper reports, as I already worked with it for several years to produce high quality pixel-perfect reports in Arabic.&lt;br&gt;
The application was deployed as a docker image, so, it made sense to deploy a report server to handle the reports generation in the same cluster. &lt;br&gt;
But even for such a simple scenario, JasperReport server was an overkill. I just needed a lightweight REST API that takes the report as url, generate it and return it back.&lt;/p&gt;

&lt;p&gt;As JasperReport IO was licensed, and its features seemed to be an overkill as well, my developer brain decided it is time to just do it myself 🙂. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3jilm2oae82ktup7ad4h.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3jilm2oae82ktup7ad4h.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Introducing &lt;a href="https://github.com/deathwaiting/jasper-reports-api" rel="noopener noreferrer"&gt;Jasper report API&lt;/a&gt; ! &lt;/p&gt;

&lt;p&gt;While this may seem like a shameless promotion ..... well, it is 😅, but there isn't really much to promote here anyway.&lt;/p&gt;
&lt;h2&gt;
  
  
  Using Jasper reports API
&lt;/h2&gt;

&lt;p&gt;This is a service that provides a simple API for generating arbitrary reports. Reports design files - jasper's JRXML files - are placed in a directory accessible to the service, and to generate a PDF report we call it like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://localhost:8080/report/emp-report.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we need a docx version of it we call&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://localhost:8080/report/emp-report.docx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Current supported extensions are : pdf, docx, xlsx,html, csv&lt;/p&gt;

&lt;p&gt;Report parameters can be provided as Query parameters, similar to JasperReports server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://localhost:8080/report/emp-report.pdf?dep_id=1&amp;amp;month=11
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The service is just a simple spring boot 3 application, so, it can have all the spring boot goodies: Spring security, spring data, optimized docker images, support for distributed tracing, metrics, logging.&lt;/p&gt;

&lt;p&gt;The service will run the reports against the configured datasource for the spring application, so, it currently works against a single database only, and while it can be authenticated using spring security, there is no internal access management for each report, that's out of the scope of simple report service.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fiddling with the API
&lt;/h3&gt;

&lt;p&gt;The easiest way is to run the service as a docker image. This command will run the service with security disabled, and mounts the reports directory at &lt;code&gt;./test-reports&lt;/code&gt;. In this example, the service connects to a postgres database to run the reports against it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nt"&gt;--tty&lt;/span&gt; &lt;span class="nt"&gt;--rm&lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;
 &lt;span class="nt"&gt;-v&lt;/span&gt; ./test-reports:/var/reports&lt;span class="se"&gt;\&lt;/span&gt;
 &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;SPRING_PROFILES_ACTIVE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;security-off&lt;span class="se"&gt;\&lt;/span&gt;
 &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;SPRING_DATASOURCE_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;jdbc:postgresql://localhost:5432/postgres&lt;span class="se"&gt;\&lt;/span&gt;
 &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;SPRING_DATASOURCE_USERNAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;postgres&lt;span class="se"&gt;\&lt;/span&gt;
 &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;SPRING_DATASOURCE_PASSWORD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;postgres&lt;span class="se"&gt;\&lt;/span&gt;
 &lt;span class="nt"&gt;-p&lt;/span&gt; 8080:8080&lt;span class="se"&gt;\&lt;/span&gt;
  ahmedgalalfathy/jasper-reports-api:0.1

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that you can generate a report by calling&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://localhost:8080/report/&amp;lt;report name&amp;gt;.&amp;lt;extension&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you need some ready examples to fiddle with, feel free to clone the &lt;a href="https://github.com/deathwaiting/jasper-report-api-examples" rel="noopener noreferrer"&gt;examples repository&lt;/a&gt;. It comes with a complete testing environment with sample reports and database.&lt;/p&gt;

&lt;p&gt;If you ran into some problems using this, or have anything in your mind about it , your feedback is very welcomed 👍.&lt;/p&gt;

&lt;p&gt;If you found that useful, give it a thumb up and a star in github, so I can put it in my CV before the next great global depression.&lt;/p&gt;

&lt;p&gt;Thanks for reading all this and have a great day!&lt;/p&gt;

</description>
      <category>jasperreports</category>
      <category>microservices</category>
      <category>enterprise</category>
      <category>api</category>
    </item>
  </channel>
</rss>
