Issue with Tamil Fonts in HTML to PDF Conversion

  Kiến thức lập trình

While converting HTML to PDF, I encountered irregular fonts in the Tamil language. Attached are my code and the resulting PDF. 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, but in the image link above, some Tamil letters are not displayed properly.

@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_DISPOSITION, "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;
}

New contributor

Sophi Priya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT