We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Apply 2 or more custom fonts for html to PDF converting

Hi,
I trying convert HTML file with 2 custom fonts (one for Latin characters and Cyrillic, second for Hebrew characters), but if text in one place (one paragraph), second font not applied.

public class PDF {

    static {
        URL resource = PDF.class.getClassLoader().getResource(fonts/);
        if (resource != null) {
            List<String> fonts = FontRepository.getLocalFontPaths();
            fonts.add(resource.getPath());
            FontRepository.setLocalFontPaths(fonts);
        }
    }

    public void pdf(String html, ByteArrayOutputStream outputStream) {
        try {
            FontRepository.loadFonts();
            Document doc = new Document();
            Page page = doc.getPages().add();
            HtmlFragment htmlFragment = new HtmlFragment(html);
            HtmlLoadOptions options = new HtmlLoadOptions();
            options.setEmbedFonts(true);
            htmlFragment.setHtmlLoadOptions(options);
            page.getParagraphs().add(htmlFragment);

            doc.save(outputStream, SaveFormat.Pdf);
            MemoryCleaner.clear();
            MemoryCleaner.clearCurrentThreadLocals();

        } catch (Exception e) {
			e.printStackTrace();
        }
    }
}

html file:

<html>
<head>
    <meta charset="utf-8"/>
    <style>

        @font-face {
            font-family: 'NotoSans';
            src: url('../fonts/NotoSans-Regular.ttf');
        }

        @font-face {
            font-family: 'NotoSansHebrew';
            src: url('../fonts/NotoSansHebrew-Regular.ttf');
        }

        * {
            font-family: 'NotoSans', 'NotoSansHebrew', sans-serif;
            font-size: 9pt;
        }

    </style>
</head>
</head>
<body>
<p>Latin text - English tex Russian text - Русский текст Hebrew text - טקסט עברי</p>
</body>
</html>

in result: Latin text - English tex Russian text - Русский текст Hebrew text - ☐ ☐ ☐ ☐ ☐ ☐ ☐ ☐

If text in different languages not in the same paragraph:

Latin text - English text


Russian text - Русский текст


Hebrew text - טקסט עברי

randomly got different result, sometimes correct:
Latin text - English text
Russian text - Русский текст
Hebrew text - טקסט עברי
sometimes
Latin text - English text
Russian text - Русский текст
Hebrew text - ☐ ☐ ☐ ☐ ☐ ☐ ☐ ☐

Linux, WebSphere.
Aspose.PDF for Java, version 20.2.

@dieselokby

We have logged an issue as PDFJAVA-39240 in our issue tracking system for the sake of correction. We will further look into details of it and keep you posted with the status of its correction. Please be patient and spare us little time.

We are sorry for the inconvenience.