Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create iText BaseFont based on InputStream

I have a font file "arial.ttf" in a web application and I can only obtain its contents as InputStream.

InputStream inputFont = getResourceAsStream("/resources/arial.ttf");

How can I create an iText BaseFont based on the InputStream? The createFont method doesn't accept it.

BaseFont bf = BaseFont.createFont(inputFont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

createFont(InputStream,String,boolean) can't invoke createFont(java.lang.String,java.lang.String,boolean) in BaseFont.

like image 322
99maas Avatar asked Jul 29 '15 17:07

99maas


1 Answers

Try something like the following:

byte[] bytes = IOUtils.toByteArray(Thread.currentThread().getContextClassLoader()
        .getResourceAsStream("/resources/arial.ttf"));
BaseFont.createFont("arial.ttf", BaseFont.IDENTITY_Harial.ttf, BaseFont.EMBEDDED, true, bytes, null);

You have to specify the .ttf in the font name to tell the method it should interpret it as ttf format.

like image 62
CSchulz Avatar answered Sep 21 '22 16:09

CSchulz