I am using iText7 trying to load a font which is in src/main/resources/pdf/fonts
, when I give the full path to the font on my filesystem and run my code from a main method I don't have any problems, the issues occur when I am running the code inside a war file with tomcat.
I have tried a few different ways to load the font but none of them seem to work.
Either iText complains that the font is not recognised com.itextpdf.io.IOException: font.is.not.recognized
(example one below) or I get a null pointer exception with example two (below) as the code is unable to load the font using the supplied path (although this works when giving the full file system path and running via a main method).
I can see that the font is definitely being included in the war file in the correct directory (/WEB-INF/classes/pdf/fonts
). Below is what I am trying to do:
private static final String FONT = "/pdf/fonts/Frutiger-LT-Std-55-Roman_18821.ttf";
try (InputStream is = MyClass.class.getClassLoader().getResourceAsStream(FONT))
{
byte[] fontBytes = IOUtils.toByteArray(is);
final PdfFont font = PdfFontFactory.createFont(fontBytes, PdfEncodings.IDENTITY_H);
}
Or:
private static final String FONT = "/pdf/fonts/Frutiger-LT-Std-55-Roman_18821.ttf";
final PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
I am not sure what it is that I need to do to load the font for iText to use when running in a web app.
I am thinking that maybe as the font does seem to have been read into the input stream that I need to somehow register the font with iText before passing it the byte array..