Show Bangla font properly on pdf file generated from Java project

1.5k Views Asked by At

I am working on a porject based on Java, Spring, Hibernate, Thymeleaf, MySQL. For the project I had to generate PDF reports in multiple sections. So I started using Flying Saucer and everything worked perfectly until I tried to include Bangla font in the PDF reports. At first it showed nothing. Then I included Bangla fonts like "Kalpurush", "SolaimanLipi". Now it shows Bangla fonts but in incorrect form. Like "মোট উপার্জন" has become "ম োট উপ ার্‌জন". I have some-pages with Bangla fonts and they are working as expected. the Problem occurs when using Bangla fonts in PDF. How can I solve this problem?

And what are the alternatives to Flying Saucer which can be used to generate PDF reports with Bangla font.

My Code:

Controller

@GetMapping("/print_bangla_pdf")
private void printBanglaPdf(HttpServletResponse response) throws Throwable {
    response.setContentType("application/octet-stream");
    String headerKey = "Content-Disposition";
    String headerValue = "attachment; filename=bangla_pdf_report.pdf";
    response.setHeader(headerKey, headerValue);
    Context context = new Context();
    context.setVariable("banglaWord", "মোট উপার্জন");
    String processHTML = templateEngine.process("pdf_reports/bangla_pdf_report", context);
    ServletOutputStream outputStream = response.getOutputStream();
    ITextRenderer renderer = new ITextRenderer();
    ITextFontResolver resolver = renderer.getFontResolver();
    resolver.addFont("C:\\kalpurush.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    renderer.setDocumentFromString(processHTML);
    renderer.layout();
    renderer.createPDF(outputStream, false);
    renderer.finishPDF();
    outputStream.close();
}

Bangla_pdf_report.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <style>
        .bengaliFont {
            font-family: "Kalpurush";
        }
    </style>
</head>

<body>
    <p class="bengaliFont" th:text="'মোট উপার্জন'"></p>
    <p class="bengaliFont" th:text="${banglaWord}"></p>
</body>
</html>
1

There are 1 best solutions below

3
On

Have you tried isolating the problem? I suspect two potential causes:

  1. Is it a problem with the Thymeleaf engine?
  2. Are you missing an @import for the TTF font?

In order to eliminate the causes, I tried creating a standalone HTML file without using Thymeleaf templating and using a Google Bangla font (Galada) also in addition to Kalpurush.TTF. Galada can be imported using Google Fonts API, whereas Kalpursh.ttf had to be downloaded and imported from local file.

It renders properly in Chrome and Edge. Here's the snippet (it uses a local file, so I am not sure how it would render within SO). I am also attaching a screenshot image of how it appears in my browser. You can save the HTML locally and try it yourself. Just replace the @import for Kalpurush.TTF to refer to your local directory path.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Galada&display=swap');
        @import url('file://d:/Giri/temp/so/kalpurush.ttf');

        .bengaliFont {
            font-family: 'Galada', cursive;
        }
        .bengaliFont2 {
            font-family: 'Kalpurush';
        }
    </style>
</head>

<body>
    <p class="bengaliFont"><span>Galada font:&nbsp;</span>মোট উপার্জন</p>

    <p class="bengaliFont2"><span>Kalpurush font:&nbsp;</span>মোট উপার্জন</p>
</body>
</html>

Here's the screenshot of my browser rendering this HTML file locally.

enter image description here

This points to no problems with the font files.

You may want to take the standalone html file and run it through FlyingSaucer to generate the PDF and see if it solves the problem and then go from there.