We are converting mail messages based on iText 7.1.2
and htmlPDF 2.0.2
. The conversion is done within a static method which is called by parallel threads for every html-based message. The code looks simplified like this (streams are closed in a finally block):
ConverterProperties properties = new ConverterProperties();
FontProvider fontProvider = new DefaultFontProvider();
for (String font : ITEXT_FONTS) {
FontProgram fontProgram = FontProgramFactory.createFont(font);
fontProvider.addFont(fontProgram);
}
properties.setFontProvider(fontProvider);
fos = new FileOutputStream(targetFile);
HtmlConverter.convertToPdf(is, fos, properties);
The for-loop is used to add chinese fonts from the Noto
package located in the classpath. In our environment we now see sometimes the following error scenario:
Caused by: java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3236)
at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
at com.itextpdf.io.util.StreamUtil.inputStreamToArray(StreamUtil.java:212)
at com.itextpdf.html2pdf.resolver.font.DefaultFontProvider.addShippedFreeFonts(DefaultFontProvider.java:111)
at com.itextpdf.html2pdf.resolver.font.DefaultFontProvider.<init>(DefaultFontProvider.java:97)
at com.itextpdf.html2pdf.resolver.font.DefaultFontProvider.<init>(DefaultFontProvider.java:81)
The questions are:
- Is the creation of the DefaultFontProvider legit for every single call or should there be only one instance (e.g. because of the costs of creation)?
- If the DefaultFontProvider is initialized only once -> is this instance thread save?
Thanks in advance!