4

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>
Partho63
  • 3,117
  • 2
  • 21
  • 39
  • Would you please provide [a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) in java? – Majid Hajibaba Aug 25 '20 at 12:47
  • This is a minimal, reproducible example. Just put this code in any Spring project with FlyingSaucer and change the file location of the font and the code should run ! – Partho63 Aug 26 '20 at 05:41
  • @Partho63 bhai, exactly the same problem I am facing. How did you solve it, Bhai? or is there any alternative way? – Muhammad Saimon Feb 18 '21 at 05:15
  • @MuhammadSaimon I couldn't find any solution to this problem. I ended up using [pdfmake](http://pdfmake.org) JavaScript library. – Partho63 Feb 18 '21 at 05:20

1 Answers1

2

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.

vvg
  • 1,010
  • 7
  • 25
  • 1
    On your other question of alternative PDF generators, I have only used Javascript. They are: 1. PDFKit (http://pdfkit.org/) 2. svg2pdf (https://github.com/yWorks/svg2pdf.js) 3. Google Puppeteer (https://developers.google.com/web/tools/puppeteer) - yes, you can load HTML in a headless chrome and save the HTML to PDF. Bit memory heavy for server side rendering and generated PDFs can be large. See this article for alternatives (https://medium.com/upskillie/pdf-generation-with-nodejs-c29da5e2433c) – vvg Aug 26 '20 at 02:56
  • This answer is for showing Bangla font on html page. I have no problem with that. I have pages where Bangla fonts are showing properly. My problem is: Bangla fonts are not showing properly after generating PDF through FlyingSaucer. Anyway thanks. – Partho63 Aug 26 '20 at 05:36
  • 1
    Ok, thanks @Partho63. Since your question mentioned '... started using Flying Saucer and everything worked perfectly until I tried to include Bangla font in the PDF reports...', I interpreted it as FlyingSaucer to be fine and the issue was with fonts. You may want to edit your question to clarify. Thanks. – vvg Aug 26 '20 at 05:44