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>