corretto-8
corretto-8 copied to clipboard
Problem reading font data from the resources folder on Lambda with Corretto
The issue is potentially related to https://github.com/corretto/corretto-11/issues/118
SimpleJasperReportsContext context = new SimpleJasperReportsContext();
// Raleway
SimpleFontFamily raleway = new SimpleFontFamily(context);
raleway.setName("Raleway");
raleway.setPdfEmbedded(true);
raleway.setPdfEncoding("Identity-H");
SimpleFontFace ralewayRegular = new SimpleFontFace(context);
ralewayRegular.setTtf("fonts/Raleway/Raleway-Regular.ttf");
raleway.setNormalFace(ralewayRegular);
// Raleway SemiBold
SimpleFontFamily ralewaySemiBold = new SimpleFontFamily(context);
ralewaySemiBold.setName("Raleway SemiBold");
ralewaySemiBold.setPdfEmbedded(true);
ralewaySemiBold.setPdfEncoding("Identity-H");
SimpleFontFace ralewaySemiBoldRegular = new SimpleFontFace(context);
ralewaySemiBoldRegular.setTtf("fonts/RalewaySemiBold/Raleway-SemiBold.ttf");
ralewaySemiBold.setNormalFace(ralewaySemiBoldRegular);
context.setExtensions(FontFamily.class, Arrays.asList(raleway, ralewaySemiBold));
the ttf is present in the resources folder
and during invoking the lambda function got the next exception message:
Caused by: net.sf.jasperreports.engine.fonts.InvalidFontException: Error loading font "fonts/Raleway/Raleway-Regular.ttf".
at net.sf.jasperreports.engine.fonts.SimpleFontFace.loadFont(SimpleFontFace.java:206)
at net.sf.jasperreports.engine.fonts.SimpleFontFace.setTtf(SimpleFontFace.java:167)
at net.sf.jasperreports.engine.fonts.SimpleFontFace.setTtf(SimpleFontFace.java:157)
......
Caused by: java.io.IOException: Problem reading font data.
at java.desktop/java.awt.Font.createFont0(Unknown Source)
at java.desktop/java.awt.Font.createFont(Unknown Source)
at net.sf.jasperreports.engine.fonts.SimpleFontFace.loadFont(SimpleFontFace.java:198)
... 15 more
this issue is related to Java8 Corretto and Java11 Corretto
with Java8 (OpenJDK) - works without any problems Due to https://aws.amazon.com/blogs/compute/announcing-migration-of-the-java-8-runtime-in-aws-lambda-to-amazon-corretto/ we have to make it work on Corretto.
Thanks for letting us know. It does look related to https://github.com/corretto/corretto-11/issues/118, we're pulling in the right people from AWS Lambda to help on the issue.
@davecurrie thanks for looking at this. Do you have any estimates of when it will be resolved? Due to https://aws.amazon.com/blogs/compute/announcing-migration-of-the-java-8-runtime-in-aws-lambda-to-amazon-corretto/ we need to resolve it till July 19, 2021.
July 19, 2021: Any new functions using the java8 runtime will use Amazon Corretto. If you update an existing function, it will transition to Amazon Corretto automatically.
In the opposite way maybe do you have some workarounds in mind that can help us to resolve it?
Thanks for understanding!
@davecurrie after small research was tried the next:
- Tried to reproduce it locally using docker image from amazoncorretto:8 - works as expected without any issues.
- Then tried to reproduce it locally using amazoncorretto:8-alpine-jdk docker image and got the same issue as mentioned in the ticket.
So, looks like AWS Lambda uses Java Corretto Alpine version that leads to the issue. Hope this information will be helpful for resolving the issue on your side.
P.S: The same case for Java 11 as well.
Hi @sema4-daniilvolkov, thank you for the follow-up research.
For Corretto 8 on Alpine (amazoncorretto:8-alpine-jdk
), can you verify if you have installed fontconfig
? It isn't shipped by default in amazoncorretto:8-alpine-jdk
, but it's required in order to locate a custom font.
Here's my reproduction steps within amazoncorretto:8-alpine-jdk
:
- Install a font package.
apk add ttf-freefont
(This also installs fontconfig as dependency.) - Test case.
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
import java.util.Objects;
public class GitHub317 {
public static void main(String[] args) throws IOException, FontFormatException {
String fName = "/usr/share/fonts/TTF/FreeSerif.ttf";
Font customFont = Font.createFont(Font.TRUETYPE_FONT, Objects.requireNonNull(GitHub317.class.getResourceAsStream(fName)));
Objects.requireNonNull(customFont);
}
}
I didn't see any issue when running the test above.
I've added fontconfig.zip as a Lambda Layer and it started working!