Font family not rendered while converting html to pdf

Viewed 373

I am using flying saucer to convert html to pdf but shows blank space for custom font(Kannada) when generated pdf is opened.I have also added the ttf file to the directory. Here is my code

String value="<html><head><title>Internationalization</title><meta charset='UTF-8'/></head><<body><h1>Internationalization</h1><table summary=''><tr><th style="font-family:NotoSansKannada-Regular;">ಕನ್ನಡ</th><th>Original title</th></tr></table></body></html>";
OutputStream outputStream = new FileOutputStream(pdfFilePath + "/Reports.pdf");
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver resolver = renderer.getFontResolver();
renderer.getFontResolver().addFont(
Main.class.getResource("frontend/fonts/NotoSansKannada-Regular.ttf").toURI().toString().replace("file:", "file://"),
"UTF-8",BaseFont.NOT_EMBEDDED
);
renderer.setDocumentFromString(value);
renderer.layout();
renderer.createPDF(bos);
outputStream.close()
1 Answers

Here is an example which render the font. Unfortunately, the result is not correct, as Flying-saucer is enable to apply the ligatures on the text.

String html = "<html>\n" +
            "  <head>\n" +
            "        <meta charset='UTF-8'/>\n" +
            "        <style>\n" +
            "            body {font-family: Noto Sans Kannada;}\n" +
            "        </style>\n" +
            "  </head>\n" +
            "  <body>ಕನ್ನಡ</body>\n" +
            "  </html>";
    ITextRenderer renderer = new ITextRenderer();
    renderer.getFontResolver().addFont("font/noto-kannada/NotoSansKannada-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

    OutputStream out = new FileOutputStream(new File("so.pdf"));
    renderer.setDocumentFromString(html);
    renderer.layout();
    renderer.createPDF(out);
    out.close();
Related