Free Support Forum - aspose.com

FontNotFoundException: Font Times New Roman was not found, but only sometimes

Hello!

I didn’t see this exception when building/testing on my local Windows/Intellij, and Ubuntu (WSL), but our automated build system is Jenkins running on some other linux box somewhere, and it’s getting this:

com.aspose.pdf.exceptions.FontNotFoundException: Font Times New Roman was not found

The test code is as follows:

@Test
public void fontException_butOnlyUnderJenkins() throws Exception
{
byte[] htmlBytes = IOUtils.toByteArray(this.getClass().getResourceAsStream("/test-source-for-pdf_01.html"));
HtmlLoadOptions htmlOptions = new HtmlLoadOptions();
Document doc = new Document(new ByteArrayInputStream(htmlBytes), htmlOptions);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
doc.save(baos);
}

More info…

The sample HTML has Lorem Ipsum in a few different languages. I’m noticing that the presence of Japanese causes the FontNotFoundException, but several others do not, including Chinese, Hebrew, Arabic, Korean.

This test is only a few languages, and only Lorem Ipsum, so it’s not an exhaustive test.

I’m going to try adding the Times New Roman font to the document itself. But this is very disconcerting. Is it documented anywhere what character sets might cause this exception, and what fonts will be needed if they are encountered?

More info…

I’ve tried loading the font from the application’s resources and apply it to the document. So far I’ve still been unsuccessful in getting past the FontNotFoundException.

Here is the latest code in that effort:

byte[] htmlBytes = IOUtils.toByteArray(this.getClass().getResourceAsStream("/test-source-for-pdf_02.html"));
HtmlLoadOptions htmlOptions = new HtmlLoadOptions();
htmlOptions.setEmbedFonts(true);
Font font = FontRepository.openFont(this.getClass().getResourceAsStream("/times.ttf"), FontTypes.TTF);
font.setEmbedded(true);
TextState ts = new TextState();
ts.setFont(font);
htmlOptions.getPageInfo().setDefaultTextState(ts);
Document doc = new Document(new ByteArrayInputStream(htmlBytes), htmlOptions);
// The exception happens during Document creation, so it isn't getting this far.

@LaszloU

Could you please try to install all windows fonts in the system and run the scenario again. In case issue still persists, please share your sample HTML file with us. We will test the scenario in our environment and address it accordingly.

Is there no other solution than for us to install Windows fonts on our production linux machines?

Is there no way to include the font files in our application’s jar file for Aspose.PDF to use them from there?

@LaszloU

You can surely use fonts by specifying fonts path. However, we needed to check if other alternative approach could resolve the issue. Nevertheless, please share your sample HTML file in .zip format with us. We will test the scenario in our environment and address it accordingly.

Please find zip file attached.

international_html_tests.zip (37.0 KB)

The _02 file has Japanese, the _01 file also has several other langauges.

@LaszloU

We have logged an investigation ticket as PDFJAVA-39657 in our issue tracking system for your case. We will look into its details and inform you as soon as the ticket is resolved. Please spare us some time.

Meanwhile, would you please try adding font files to application path (for Arial Unicode MS, Times New Roman, and other fonts which support foreign language characters) and use FontRepository.setLocalFontPaths() method to set font path for API. Please let us know your feedback after testing suggested approach.