Nguyen Tran Nhat Vu
Nguyen Tran Nhat Vu

Reputation: 25

Docx4j convert HTML to docx does not apply font-family Times to div

I am trying to convert HTML to Docx but some how <div> with Times New Roman font set in CSS does not applied when converted to Docx, div with Segoe UI is ok though. (My Microsoft Word do have option to change text font to Times New Roman and I can convert html <p> tag to Times, just can't do the same with <div>)

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            .times {
                font-family: 'Times New Roman', Times, serif;
                font-size: 12px;
            }
            .segoe {
                font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
                font-size: 12px;
            }
        </style>
    </head>
    
    <body>
        <div class="times">HELLO in Times New Roman</div>
        <div class="segoe">HELLO in Segoe UI</div>
    </body>

This is my code to convert HTML to Docx file.

htmlContent = htmlContent.replace("<br>", "");

WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();

wordPackage.getMainDocumentPart().addAltChunk(AltChunkType.Xhtml, htmlContent.getBytes());

File exportFile = new File(EXPORT_DOCX_FILE_PATH);
wordPackage.save(exportFile);

screenshot

Upvotes: 0

Views: 94

Answers (2)

JasonPlutext
JasonPlutext

Reputation: 15863

Per my answer at https://stackoverflow.com/a/79026295/1031689 if you add an altChunk like that, you are relying on Word to convert it to docx content on opening (which it apparently does badly in this case as well).

docx4j-ImportXHTML can convert it for you - hopefully better - if you add that to your classpath and invoke wordPackage.getMainDocumentPart().convertAltChunks()

Upvotes: 0

Vishrut Kannan
Vishrut Kannan

Reputation: 1

The issue is likely due to Times New Roman being recognized incorrectly during the DOCX conversion process. You could check the documentation of the library you are using for the DOCX conversion to see if font embedding is supported. Make sure that somewhere in the documentation it is explicitly mentioned that the desired font is embedded and adjust the conversion code as necessary.

Upvotes: 0

Related Questions