2

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..

mkl
  • 90,588
  • 15
  • 125
  • 265
berimbolo
  • 3,319
  • 8
  • 43
  • 78
  • 2
    I added some tags to your question, to attract people who know something about Tomcat and war files. – Amedee Van Gasse Apr 04 '18 at 22:20
  • @AmedeeVanGasse I have tried giving iText the full path to the font file on the file system, expecting this would work, as in your example here (https://developers.itextpdf.com/content/best-itext-questions-stackoverview/font-examples/itext7-why-doesnt-fontfactorygetfont-work-all-fonts), however I get a very non descript exception `com.itextpdf.io.IOException: table.1.does.not.exist.in.2`. Is this something you can help with? – berimbolo Apr 05 '18 at 11:20
  • Sorry, I cannot help you, I have never done any Tomcat development myself. – Amedee Van Gasse Apr 05 '18 at 11:54
  • Can you share the font file in question? – mkl Apr 05 '18 at 13:23
  • I have literally just worked out what the problem is. Its not really iText specific like I initially thought. I noticed the font in the exploded war file has a different size which I thought odd, I then noticed that the font opens fine with a font viewer in linux when its in `src/main/resources`, but that trying to open the font from inside the `WEB-INF` directory in the war file it seemed corrupted. I figured it had to be to do with the maven resources plugin. – berimbolo Apr 05 '18 at 13:37
  • 1
    A google of maven resources plugin and Itext fonts bought me to this SO post, I hope this might help someone else https://stackoverflow.com/questions/25404145/register-font-is-corrupting-ttf-file, this has taken me literally hours to get to the bottom of – berimbolo Apr 05 '18 at 13:38
  • 1
    @Joris why did you damage the code formatting (pulling the final bracket out of the code context)? Why did you delete the final paragraph from the question body? – mkl Jul 11 '18 at 11:19

0 Answers0