1

I'm using a ttf file to implement a fixed font for czech letters.

As long as I run the code in my debugger (IntelliJ 2020.3) it works fine. But if I try to run the built jar file in my test project I get the following error:

Exception in thread "main" com.itextpdf.io.IOException: font.is.not.recognized
    at com.itextpdf.io.font.FontProgramFactory.createFont(FontProgramFactory.java:291)
    at com.itextpdf.io.font.FontProgramFactory.createFont(FontProgramFactory.java:214)

The code:

    InputStream in = Template_Dokument.class.getClassLoader().getResourceAsStream("font.ttf");
    byte[] targetArray = null;
    try {
        targetArray = new byte[in.available()];
        in.read(targetArray);
    } catch (IOException e) {
        e.printStackTrace();
    }
    FontProgram fontProgram = FontProgramFactory.createFont(targetArray);
    PdfFont font = PdfFontFactory.createFont(fontProgram, PdfEncodings.IDENTITY_H, false);

The error happens in the method: FontProgramFactory.createFont

RigaJona
  • 43
  • 6
  • 1
    Have you compared the contents of `targetArray` when run in IntelliJ with the contents when run from the built jar in your test project? E.g. by logging them? – mkl Jan 14 '21 at 16:13
  • I've written the array to a txt file, and it they're not equal alt all... do you have an idea why? – RigaJona Jan 14 '21 at 17:33
  • Also after round about 11600 bytes every byte is 0. I'll handle that a problem first... – RigaJona Jan 14 '21 at 17:42
  • An issue here is the use of `in.available()` to determine the size of the font file to read. – mkl Jan 18 '21 at 17:21

4 Answers4

1

The issue (most likely) is not with how you are reading the file, but rather that the ttf file is being altered when it is packaged into the jar. Adding this into the build section of your pom file should do it:

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>

See https://maven.apache.org/plugins/maven-resources-plugin/examples/binaries-filtering.html for more details

John
  • 11
  • 2
0

Apparently you have to convert the Inputstream (to byte array) in small units of 4Kb.

https://stackoverflow.com/questions/1264709/convert-inputstream-to-byte-array-in-java

This one solved it for me.

RigaJona
  • 43
  • 6
0
URL font_path = Thread.currentThread().getContextClassLoader().getResource("font/font1.ttf");
byte[] b = PdfEncodings.convertToBytes(String.valueOf(font_path), PdfEncodings.WINANSI);
PdfFont font = PdfFontFactory.createFont(b, PdfEncodings.WINANSI, true);**

The type of font isn't recognized

enzo
  • 9,861
  • 3
  • 15
  • 38
Raj Gupta
  • 1
  • 1
0

Since you can't read the path of template in the jar package, take the font file out of the jar package and put it in the jar sibling directory.