While converting HTML to PDF, I encountered irregular fonts in the Tamil language. The fonts appear distorted or improperly rendered. I need a solution to ensure that the Tamil text displays correctly in the PDF output.
"பெயர், அலைபேசி எண் மற்றும் தகவலறிந்தவரின் முகவரி" This is the correct format for the Tamil language.
Here I Attached My code,
@RequestMapping(path = "/pdfView")
public ResponseEntity<?> getPDFView(@RequestParam String language, HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
String fileName = "tamil.pdf";
WebContext context = new WebContext(request, response, servletContext);
Locale locale = new Locale(language);
LocaleContextHolder.setLocale(locale);
Context context1 = new Context(locale);
// Define the path to your custom font
String customFontPath = "/static/fonts/NotoSansTamil-Regular.ttf";
// Use DefaultFontProvider and add custom font
DefaultFontProvider fontProvider = new DefaultFontProvider(false, false, false);
fontProvider.addFont(customFontPath);
Map<String, String> messages = getAllMessages(locale);
context.setVariable("messages", messages);
context.setVariable("pdf2Entry", data);
String orderHtml = templateEngine.process("HtmlPageName", context);
ByteArrayOutputStream target = new ByteArrayOutputStream();
ConverterProperties converterProperties = new ConverterProperties();
converterProperties.setFontProvider(fontProvider);
HtmlConverter.convertToPdf(orderHtml, target, converterProperties);
byte[] bytes = target.toByteArray();
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DIS POSITION, "inline; filename=" + fileName)
.contentType(MediaType.APPLICATION_PDF).body(bytes );
}
public Map<String, String> getAllMessages(Locale locale) throws IOException {
Map<String, String> messages = new HashMap<>();
String localeString = locale.toString();
String filePath = null;
InputStream inputStream = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (localeString.equals("ta")) {
filePath = "tamil.json";
} else {
filePath = "english.json";
}
inputStream = classLoader.getResourceAsStream(filePath);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(inputStream);
for (JsonNode entry : rootNode) {
String key = entry.get("key").asText();
String message = entry.get("value").asText();
messages.put(key, message);
}
return messages;
}