-2

Hi I am using Docx4j to convert word template to PDF. But Tamil font is not supported. It looks weird. I used below java code that seems to reduce the weird look, still words are not displaying correctly. I have installed Latha as Tamil font. Pls help

FontTablePart fontTablePart= wordMLPackage.getMainDocumentPart().getFontTablePart();
        fontTablePart.processEmbeddings();
        Set<String> fontsInUse = wordMLPackage.getMainDocumentPart().fontsInUse();
        // Make each embedded font available to the font mapper.
        for(String s : fontsInUse) {
            System.out.println("Font in use : "+s);
            PhysicalFont physicalFont = PhysicalFonts.get(s);
            fontMapper.put(s, physicalFont);
        }

Sample XML that I am trying to convert:

<w:body><w:p w14:paraId="33C1CF30" w14:textId="77777777" w:rsidR="000612C7" w:rsidRDefault="000612C7" w:rsidP="000612C7"><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>ஆசிரியர்</w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart"/><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>வகுப்பறையுள்</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart"/><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>நுழைந்தார்</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>.</w:t></w:r></w:p><w:p w14:paraId="2D364612" w14:textId="77777777" w:rsidR="000612C7" w:rsidRDefault="000612C7" w:rsidP="000612C7"><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>அவர்</w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>உள்ளே</w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart"/><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>நுழைந்தவுடன்</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart"/><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>மாணவர்கள்</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart"/><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>எழுந்தனர்</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>.</w:t></w:r></w:p><w:p w14:paraId="162AEE05" w14:textId="77777777" w:rsidR="000612C7" w:rsidRDefault="000612C7" w:rsidP="000612C7"><w:proofErr w:type="spellStart"/><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>வளவன்</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>மட்டும்</w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>தன்</w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>அருகில்</w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart"/><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>நின்றுகொண்டிருந்த</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>மாணவி</w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart"/><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>கனிமொழியுடன்</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart"/><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>பேசிக்</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart"/><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>கொண்டிருந்தான்</w:t></w:r><w:proofErr w:type="spellEnd"/><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>.</w:t></w:r></w:p><w:p w14:paraId="2CF16F1C" w14:textId="07F9E241" w:rsidR="00E46BC8" w:rsidRPr="000612C7" w:rsidRDefault="000612C7" w:rsidP="000612C7"><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>நான்</w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>அவனை</w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t xml:space="preserve"> </w:t></w:r><w:r><w:rPr><w:rFonts w:cs="Latha" w:hint="cs"/><w:cs/><w:lang w:bidi="ta-IN"/></w:rPr><w:t>எச்சரித்தேன்</w:t></w:r></w:p><w:sectPr w:rsidR="00E46BC8" w:rsidRPr="000612C7" w:rsidSect="00BB36CE"><w:headerReference w:type="default" r:id="rId11"/><w:pgSz w:w="11906" w:h="16838"/><w:pgMar w:top="1702" w:right="4162" w:bottom="576" w:left="1440" w:header="284" w:footer="450" w:gutter="0"/><w:cols w:space="708"/><w:docGrid w:linePitch="360"/></w:sectPr></w:body></w:document>

Developer404
  • 5,716
  • 16
  • 64
  • 102

2 Answers2

0

Assuming you are converting using docx4j-export-fo, either ensure the font is installed and use IdentityPlusMapper, or use BestMatchingMapper to map a suitable font.

See further https://github.com/plutext/docx4j/blob/VERSION_11_4_8/docx4j-samples-docx-export-fo/src/main/java/org/docx4j/samples/ConvertOutPDFviaXSLFO.java#L152

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
  • Font is installed, Used both IdentityPlusMapper and BestMatchingMapper. But it didnt generate – Developer404 Feb 24 '23 at 05:41
  • Unzip your docx and find a paragraph containing text in the Tamil font. Add the XML to your question. What is the name of the font you installed? – JasonPlutext Feb 24 '23 at 20:40
  • I am using Latha Font. Turns out the Apache FOP does not support complex languages. https://xmlgraphics.apache.org/fop/2.8/complexscripts.html. Thanks for your help. I am using JDOConverter to publish as PDF with multi language. Answer below – Developer404 Feb 27 '23 at 07:13
  • It does, as that link suggests. You are doing it wrong. – JasonPlutext Feb 27 '23 at 19:43
  • Do you have exact code snippet? I tired many possibilities with Docx4j. I see improvement, but still wrong. – Developer404 Mar 02 '23 at 06:10
  • As I requested previously, you need to add some sample XML to your question. One way to do this is to unzip your docx and find a paragraph containing text in the Tamil font. Or you could use docx4j to marshal a paragraph. – JasonPlutext Mar 02 '23 at 19:05
  • Added the xml in the question – Developer404 Mar 03 '23 at 01:58
0

I used OpenOffice with JDO Converter to create PDF from word document. Works like a charm. Code below, if it helps anyone:

public static void file2pdf(File input, File output) throws IOException {
        if (input == null) {
            throw new IllegalArgumentException("Input file must not be null");
        }
        if (output == null) {
            throw new IllegalArgumentException("Output file must not be null");
        }
        File path = output.getParentFile();
        if (path != null && !path.exists()) {
            path.mkdirs();
        }
        OpenOfficeConnection connection = new SocketOpenOfficeConnection("localhost", 8100);
        connection.connect();
        try {
            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
            converter.convert(input, output);
        } finally {
            connection.disconnect();
        }
    }
JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
Developer404
  • 5,716
  • 16
  • 64
  • 102