Load fonts from jar

Hi,


We have seen problems when fonts are substituted during conversion to PDF. These are probably resolved now but to be safe, we plan to store all Windows fonts in our jar (that we deploy on linux).

I have successfully specified an additional font path to a directory outside of the jar but I have not managed to load fonts when they are stored within the jar. I don’t really want to load all fonts into memory using the MemoryFontSource because there will be 300+MB of fonts.

Any ideas?

Thanks,

James

Hi James,


Thank you for contacting Aspose support.

I have evaluated the presented scenario while using the latest version of Aspose.Cells for Java 16.11.4 (attached) and the following piece of code. I am afraid, I am not able to observe any problem while loading the font from a Jar. Please note, I have used the jasperreports-fonts.jar (which contains the DejaVu Sans TrueType font files) for my testing. Moreover, the aforementioned font is not present on my machine, however, if you check the attached PDF file, you will notice that Aspose.Cells for Java has correctly embedded the font.

In case you still face any difficulty, please share the Jar containing just a few font files, we will test the scenario against that particular Jar and share our findings here for your reference.

Java
byte[] bytes = null;
InputStream in = cells.class.getResourceAsStream("/net/sf/jasperreports/fonts/dejavu/DejaVuSans.ttf");
if(in != null)
{
bytes = IOUtils.toByteArray(in);
if(bytes != null)
{
MemoryFontSource sourceMemory = new MemoryFontSource(bytes);
FontConfigs.setFontSources(new FontSourceBase[] { sourceMemory});
}
}

Workbook book = new Workbook();
Worksheet sheet = book.getWorksheets().get(0);
Style style = sheet.getCells().get(“A1”).getStyle();
style.getFont().setName(“DejaVu Sans”);
sheet.getCells().get(“A1”).setStyle(style);
sheet.getCells().get(“A1”).putValue(“Hello World!”);
book.save(dir + “output.xlsx”);
book.save(dir + “output.pdf”);
Hi again,

jameskerr:

I have successfully specified an additional font path to a directory outside of the jar but I have not managed to load fonts when they are stored within the jar. I don't really want to load all fonts into memory using the MemoryFontSource because there will be 300+MB of fonts.

Sorry for not noticing your comments that you do not wish to load all fonts in the memory using the MemoryFontSource class. In that case, it would be appropriate that you should place (or extract) the fonts on any location in Linux environment and set that path in your code.

Ok thanks. We will be converting from word docs, excel, ppt etc. Do I need to configure the location of the extra fonts once for each converter or is there a generic way?

Hi James,

I am afraid, each Aspose API set has its own way to specify the custom font folder. You have to use the respective mechanism for each Aspose API separately. If you haven't found a corresponding article in the online documentation for Aspose.Words & Aspose.Slides, I would suggest you either create separate threads in respective support forums or one combined thread in Aspose.Total support forum for further assistance in this regard.