1

I currently have an app built with spring boot. Im using Jasper reports. When I try to generate a report, I get the following error:

java.lang.NullPointerException: Cannot load from short array because "sun.awt.FontConfiguration.head" is null at java.desktop/sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1260) ~[na:na]

The application works fine on my computer, it only fails in docker.

Here's my Dockerfile:

FROM openjdk:17-alpine
EXPOSE 10093
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} site_informativo.jar

RUN apk update;
RUN apk add -f apt-utils;
RUN apk add -f libfreetype6;
RUN apk add --no-cache -f fontconfig;
RUN apk add -f fonts-dejavu;
RUN apk add -f msttcorefonts;
RUN apk add -f libfontconfig1;
RUN apk add -f freetype;
RUN fc-cache --force

ENTRYPOINT ["java","-jar","/site_informativo.jar"]

I have also added jasperreports-fonts EXTENSION in pom.xml but THE PROBLEM CONTINUES.

<!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports -->
<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.20.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports-fonts -->
<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports-fonts</artifactId>
    <version>6.20.0</version>
</dependency>

Some more context

If I do

FROM openjdk:17-oracle
EXPOSE 10093
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} site_informativo.jar

ENTRYPOINT ["java","-jar","/site_informativo.jar"]

I get

Font "Times New Roman" is not available to the JVM. See the Javadoc for more details.

KenobiBastila
  • 539
  • 4
  • 16
  • 52
  • The duplicate may seem strange, but the linked question contains answers on how to solve your issue (the most upvoted one is unfortunately wrong :), but you will also find the correct solution, which is provide font-extension. – Petter Friberg Jan 09 '23 at 14:16
  • As its described in the question, adding font-extension doesnt solve the issue. – KenobiBastila Jan 09 '23 at 17:53
  • 1
    Please verify that you correctly correctly generated and added the font-extension, I have several application running in container with openjdk generating jasper report with custom fonts and never had similar issue. Font-extension is fundamental if you like to ensure that pdf is always visualizzed correctly (to include the font in the pdf) – Petter Friberg Jan 11 '23 at 14:47
  • 1
    and do note that jasperreports-fonts do **not** included Times New Roman!, you need to make your own font-extensions to have this font. – Petter Friberg Jan 11 '23 at 14:50
  • 1
    I had the same issue, but I got it resolved by using a different base image `registry.redhat.io/redhat-openjdk-18/openjdk18-openshift` – akarahman Aug 08 '23 at 10:21

1 Answers1

2

Solved by adjusting the dockerfile to install the proper microsoft fonts.

FROM openjdk:17-alpine
EXPOSE 10093
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} site_informativo.jar
 
RUN apk add --no-cache msttcorefonts-installer fontconfig
RUN update-ms-fonts

ENTRYPOINT ["java","-jar","/site_informativo.jar"]

References:

How to install fonts in Docker?

KenobiBastila
  • 539
  • 4
  • 16
  • 52
  • For jasper-report you should provide font-extensions instead of installing the fonts, see this [answer](https://stackoverflow.com/a/35549391/5292302) to understand why – Petter Friberg Jan 09 '23 at 14:12
  • Even adding font-extensions, the problem continued. I had to do both. – KenobiBastila Jan 09 '23 at 17:51
  • 1
    You need to add a font-extension that includes the fonts you are using (default jasper report) does not include "Times new Roman", trust me the idea that you need to install fonts is wrong and you may even serve pdf's that are not displayed correctly on client platform (if they do not have the font installed), hence you need the extension and make sure that the font is included in pdf that is served. – Petter Friberg Jan 11 '23 at 14:53
  • 1
    @PetterFriberg I'm afraid KenobiBastila is correct in my case too: you need to do both. Even simply adding font-extension for font DejaVu (purportedly included in the Jasper report fonts library) doesn't work. Perhaps there's something else we're missing or it's a version mismatch of sorts? I'm using Jasper 6.20.0 (CE), Spring 2.7.6, and Java 17. – nkmuturi Feb 23 '23 at 13:29
  • @nkmuturi if font-extension is not work for your font, there is a problem with the font-extension (font not included correctly, wrong name, problem with classpath), installing fonts on macchine without font-extension will generate pdf that does not have the font included and maybe not correctly viewed on other macchines. – Petter Friberg Feb 23 '23 at 15:29