Can not load Fonts from JAR file in Aspose Words, Slides, PDF, and Cells in Java

We have a application that had modules that use Aspose Words, Slides, PDF, and Cells to support various services. We are in the process of migrating our applications to the cloud. As part of this we need to bundles ALL of our resources used by a microservice into a single fat JAR for deployment to the cloud.

For ALL of these use cases we are able to load the fonts just fine in our legacy environments and in our dev environments. When we package our fonts as resources in the JAR we are unable to load the fonts. We are able to load other resources from the JARs just fine for other parts of our services but are unable to load font files into Aspose.

We have a workaround that we are using that involves extracting the fonts from the jar onto the local disk and then loading them from there but we would prefer to just load the fonts as resources directly from the JAR like we do with all of our other non Aspose resources. This workaround may not work for us long term due to container restrictions.

Is there any way we can fix this OR is it possible that JAR resource loading be added as improvement tickets?

@RyanWilliamsUSC In Aspose.Words you can load the fonts from stream:
https://docs.aspose.com/words/java/specify-truetype-fonts-location/#loading-fonts-from-stream
So in your case you can read the fonts embedded into your Jar as stream and then use them using StreamFontSource

Thanks. We are going to try that for Words.

Now my question is there a way to do this with Slides, Cells, and PDF? They have different font loading code than Words.

Aspose PDF does not have StreamFontSource.
Aspose Cells does not have StreamFontSource.
Aspose Slides looks to be a completely different design than Words, PDF, and Cells.

We are using version 22.9 of the Aspose libraries.

@RyanWilliamsUSC
In Aspose.Slides you can use FontsLoader.loadExternalFont(byte[] data) to load fonts one by one from the source.
See also: How to specify Custom Fonts Used With Presentation

@RyanWilliamsUSC,

For Aspose.Cells for Java, it does not have StreamFontSource but it does have MemoryFontSource. You can get the fonts in the Jar into byte array. Using FontConfigs.setFontSources: you can load font data from an array of bytes. See the document on configuring fonts for your reference.
e.g.
Sample code:

byte[] bytes = null;
InputStream in = cells.class.getResourceAsStream("/net/sf/.../fonts/dejavu/DejaVuSans.ttf");
if(in != null)
{
   bytes = org.apache.commons.io.IOUtils.toByteArray(in);
   if(bytes != null)
   {
      com.aspose.cells.MemoryFontSource sourceMemory = new com.aspose.cells.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("f:\\files\\output.xlsx");
book.save("f:\\files\\output.pdf");

Hope, this helps a bit.

MemoryFontSource is not a good option for us.

We are migrating from monolithic on premises servers to containers in the cloud. Each of our containers are tiny but we scale the number of them down and up as needed. Problem with MemoryFontSource for us is each container needs to have the fonts in memory. With the stream font loading the font is known by Aspose but only loaded into memory if it is needed.

The fat Jar file for each of our services is in our artifacts repository, We only pay to store 1 instance of that file regardless how many containers are using it. When it is loaded into memory we pay for the amount of memory used on each container. We do not want to increase the memory footprint of all of our containers by the size of our fonts unless they are being used.

Our workaround solution uses scratch disk space which does not have the issues with memory footprint BUT restricts us to only being able to run these containers on engines that give us scratch disk space. For now we can do this but it is going to cause us some problems down the road which is why we want to load the fonts directly from the JAR like we do with other resources.

We are still working on implementing StreamFontSource for words. If that works for us then that is exactly what we need. The problem is for the other Aspose libraries that we use this is not available.

Thanks

@RyanWilliamsUSC,

Alright, if this works for your needs well, kindly let us know and we can log appropriate tickets for Aspose.Cells, Aspose.PDF and other libraries to support the same feature.

That would be great.

We will update you once we get StreamFontSource in Words working and verified for our use case.

Thanks!

@RyanWilliamsUSC

Regarding Aspose.PDF for Java, an investigation task as PDFJAVA-42070 has been created in our issue management system to further investigate your requirements. We will let you as soon as it is resolved and we have some feedback to share in this regard. Please spare us little time.