"Font Times New Roman was not found" error using aspose-pdf 19.10 for Java in Linux

I’m trying to convert HTML into PDF. Provided HTML does not contain Times New Roman nor any other MS fonts.
I follow recipe from tutorials:

HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();
Document document = new Document(filename, htmlLoadOptions);

Document constructor returns the error.

I tried to use FontRepository class, but even though it contains expected substitutions for fonts, Times New Roman is still not replaced by anything and calling findFont method on the FontRepository would result with the same exception.
Installing MS fonts in my system might be not acceptable.
What should I do in order to substitute Times New Roman with another font?
Why is Times New Roman required for HTML not containing the font?

@rafalzonk,

Thanks for contacting support.

Can you please share complete sample code along with environment details and source files so that we may further investigate to help you out.

Hi Adnan.Ahmad,

Here’s the sample:

    InputStream is = new ByteArrayInputStream(input.getBytes());

    HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();

    Document document;
    try {
        FontRepository.setThreadStaticConfigEnabled(true);
        FontRepository.setReplaceNotFoundFonts(true);
        FontRepository.addLocalFontPath(HtmlToPdfAspose.class.getResource("/aspose-fonts/").getPath());
        FontRepository.getSubstitutions().add(new SimpleFontSubstitution("Times-Roman", "LiberationSerif"));
        FontRepository.getSubstitutions().add(new SimpleFontSubstitution("Times-New-Roman", "LiberationSerif"));

        /** more substitutions here **/

        document = new Document(is, htmlLoadOptions); //here it crashes
    } catch (Exception e) {
        throw new CantConvertToPDFException("Can't convert provided HTML to PDF: " + e.getMessage());
    }

input is the HTML content as a String.
/aspose-fonts/ is a directory where I keep substitution fonts. I verified it is opened properly.
I also checked FontRepository contains all the substitution mappings after they are set.

I tried all combinations of true/false for these flags:

        FontRepository.setThreadStaticConfigEnabled(true);
        FontRepository.setReplaceNotFoundFonts(true);

with no effect.

My OS is Linux Mint 19.2 with kernel 4.15.0-20-generic. I tried working with Java 8 and 11.
The code crashes no matter what is the content of HTML.

Please let me know if you need any further information.

@rafalzonk,

Can you please share source HTML input file with us so that we may further investigate to help you out.

@Adnan.Ahmad
As I said it does not matter what is the content of the HTML file, I tested this code with many examples.

Here’s a sample of HTML I tried the last time:
document.html.zip (1.1 KB)

@rafalzonk,

I have worked with source file shared by you along with sample code and unable to observe the issue. Can you please try to use Aspos.PDF latest version 20.2 on your end and share feedback with us.

Hey,

I tried using the latest version and indeed it changed something. Currently the message I’m getting says:

Font Arial was not found

I explicitely define substitution for this font too, so I expect a different font should be used in its place:

FontRepository.getSubstitutions().add(new SimpleFontSubstitution(“Arial”, “LiberationSerif”));

Please keep in mind I cannot install Microsoft TrueType fonts.

How can I make these substitutions work?

@rafalzonk

In order to further substitute an absent font, you can use built-in monitoring for substituted fonts:

final Map<String,String> names =  new HashMap<String,String>() ;

            pdfDocument.FontSubstitution.add(new Document.FontSubstitutionHandler()
            {
                public void invoke(Font font, Font newFont)
                {
                    //add substituted FontNames into map.
                    names.put(font.getFontName(), newFont.getFontName());
                    //or print the message into console
                    System.out.println("Warning: Font "+ font.getFontName() + " was substituted with another font -> " + newFont.getFontName());
                }
            }); 

Please try using suggested code snippet to substitute fonts with installed ones and in case you still face any issue, please feel free to let us know.

Hey @asad.ali
What is the pdfDocument's type in your example? It can’t be an instance of a Document class, because the exception I’m getting is thrown out of this class contructor.
I know Document class gives a way of defining FontSubstitutions in a way you suggested, but it is unavailable for me since I am not able to construct its object.

@rafalzonk

Yes, you are right as the code will not work because API is raising exception on initialization of Document object. We have logged an investigation ticket as PDFJAVA-39210 in our issue tracking system for this scenario. We will further investigate the feasibility to substitute fonts while converting HTML to PDF and let you know as soon as the ticket is resolved. Please be patient and spare us little time.

We are sorry for the inconvenience.

Hey @asad.ali
It’s been over 10 days. Is there any progress on the issue? How can I track the ticket you created?

@rafalzonk

As the issue has been logged recently, it is pending for analysis. Please note that the issue was logged under normal support model and will be investigated/resolved on first come first serve basis. As it is logged in our internal issue management system, you may not have access to it for tracking but, we will surely keep you informed within this forum thread regarding its resolution status. Please spare us some time.

We are sorry for the inconvenience.

Hey @asad.ali

I am the Product Manager for the team who needs this.

Do we have an update on this? This is an issue which has been carrying on for a while now and it’s important it’s resolved.

Cheers
Omar

@osheikhy

Thanks for contacting support.

Regretfully no progress is made towards resolution of the ticket which was logged earlier. Please note that we have recorded your concerns and raised the issue to next level of priority already. We will inform you within this forum thread as soon as some certain updates are available. Please spare us some time.

We are sorry for the inconvenience.

Hi @asad.ali - it has nearly been a month now.

Do you have an update?

@osheikhy

We would like to share with you that attached ticket has been resolved in upcoming version Aspose.PDF for Java 20.4. As per our investigation, the “Times New Roman” is a font that is used by default when some font is not found. When “Times New Roman” is absent then “Arial” font is used. When Arial font is absent too then API tries to find some compatible font and when compatible fonts are not found at all then the exception is raised.

We able to convert the document on one of our Linux Machine without MS fonts by adding a line below. The resultant file is attached.

FontRepository.getSubstitutions().add(new SimpleFontSubstitution("Symbol", "LiberationSerif"));

OR you could substitute all fonts in document by the code:

FontRepository.getSubstitutions().add(new PDFJAVA_39210FontSubstitute());

...

public class PDFJAVA_39210FontSubstitute extends CustomFontSubstitutionBase {
  public boolean trySubstitute(CustomFontSubstitutionBase.OriginalFontSpecification originalFontSpecification, com.aspose.pdf.Font[] substitutionFont) {
     System.out.println(originalFontSpecification.getOriginalFontName());
     substitutionFont[0] = FontRepository.findFont("LiberationSerif");            
     return true;
  }
}

Please, note, that LiberationSerif is not fully compatible to Symbol font so in resultant document some symbols are broken. On one of our Linux machine, we were unable to get rid of all exceptions as, besides the above fonts, you need suitable fonts for substitute DingBats and some other fonts.

It is unlikely that you will be able to find all alternative suitable fonts and will get an appropriate result of output files so we strictly recommend to install msttcorefonts package. msttcorefonts is considered globally legal for installation because the license for the fonts allow them to be installed and by omission, installed on non-Windows operating systems, without a Windows license. Please see the links: Is it legal to install msttcorefonts package? Is Wine legal? - Ask Ubuntu and TrueType core fonts for the Web end user license agreement.

_20_3.pdf (93.7 KB)

Good news @asad.ali - do you know when “upcoming version Aspose.PDF for Java 20.4” will be released?

@osheikhy

Aspose.PDF for Java 20.4 has been released. You may please use it in your application. Please also implement previously shared suggestions in order to get expected results.