Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Issue with Tamil Fonts in HTML to PDF Conversion

  1. #1
    Junior Member
    Join Date
    Jul 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Issue with Tamil Fonts in HTML to PDF Conversion

    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;
    }
    Last edited by sophi priya; July 15th, 2024 at 12:46 AM.

Similar Threads

  1. How to edit Fonts of Java app... !!!!
    By b.shek49794 in forum Java ME (Mobile Edition)
    Replies: 4
    Last Post: September 5th, 2013, 01:54 AM
  2. fonts in eclipse :/
    By MR bruto in forum Java IDEs
    Replies: 1
    Last Post: May 30th, 2013, 03:52 PM
  3. Combine fonts on JLabel
    By gammaman in forum AWT / Java Swing
    Replies: 1
    Last Post: May 9th, 2012, 03:05 PM
  4. Java fonts
    By Ludicrous in forum Java Theory & Questions
    Replies: 2
    Last Post: March 31st, 2012, 05:39 PM
  5. Conversion of html pages into Struts-2.0 related tags
    By rajasekhar.k2 in forum Member Introductions
    Replies: 0
    Last Post: December 23rd, 2011, 05:55 AM

Tags for this Thread