Converting rich HTML pages into editable DOCX files is a frequent need for reporting and document automation workflows. GroupDocs.Conversion Cloud SDK for Node.js enables developers to perform this conversion entirely through a REST‑based library. In this tutorial you will see how to set up the SDK, write Node.JS code that performs HTML to DOCX conversion in Node.JS, use cURL to call the API, and apply configuration options for optimal results.

Prerequisites and Setup

Before you begin, make sure you have the following:

  • Node.js 14 or later installed on your development machine.
  • An active GroupDocs.Conversion Cloud account (you can sign up for a free trial on the product page).
  • Your Client ID and Client Secret from the GroupDocs Cloud dashboard.
  • Internet connectivity for calling the cloud REST API.

Install the SDK via npm:

npm install groupdocs-conversion-cloud

Download the latest package and view the source on GitHub if you need to explore the code:

Download GroupDocs.Conversion Cloud SDK for Node.js

With the SDK installed and credentials ready, you can move on to the implementation.

HTML to DOCX Conversion in Node.JS: Step-by-Step Walkthrough

Below is a concise walkthrough that shows each logical step. The full source code is provided later.

Step 1: Initialize the Conversion API Client

Create an instance of ConversionApi and configure authentication.

const { ConversionApi, Configuration } = require('groupdocs-conversion-cloud');

const config = new Configuration({
    clientId: 'YOUR_CLIENT_ID',
    clientSecret: 'YOUR_CLIENT_SECRET'
});

const conversionApi = new ConversionApi(config);

Step 2: Upload the HTML Source File

Upload the HTML file you want to convert. The API returns a file identifier that you will use for conversion.

const fs = require('fs');
const path = require('path');

const htmlPath = path.resolve(__dirname, 'sample.html');
const fileBytes = fs.readFileSync(htmlPath);

const uploadResponse = await conversionApi.uploadFile({
    file: fileBytes,
    fileName: 'sample.html'
});
const sourceFileId = uploadResponse.id;

Step 3: Set Conversion Options

Configure any DOCX‑specific options, such as embedding fonts or setting page margins. This step demonstrates DOCX Generation from HTML customization.

const convertSettings = {
    outputFormat: 'DOCX',
    options: {
        embedFonts: true,
        pageSize: 'A4',
        marginTop: 20,
        marginBottom: 20,
        marginLeft: 15,
        marginRight: 15
    }
};

Step 4: Execute the Conversion

Call the convert method with the source file ID and the settings defined above.

const convertResponse = await conversionApi.convert({
    fileId: sourceFileId,
    convertSettings: convertSettings
});
const docxFileId = convertResponse.id;

Step 5: Download the Resulting DOCX

Retrieve the generated DOCX file and save it locally.

const docxData = await conversionApi.downloadFile({ fileId: docxFileId });
fs.writeFileSync(path.resolve(__dirname, 'output.docx'), docxData);
console.log('DOCX file saved as output.docx');

The steps above illustrate a complete HTML to DOCX conversion in Node.JS workflow using the GroupDocs.Conversion Cloud SDK.

Complete Code Example: Generating DOCX from HTML Content

The following program puts all of the pieces together into a single, runnable script.

// Full working example for HTML to DOCX conversion using GroupDocs.Conversion Cloud SDK for Node.js

const { ConversionApi, Configuration } = require('groupdocs-conversion-cloud');
const fs = require('fs');
const path = require('path');

async function convertHtmlToDocx() {
    // 1. Configure the API client
    const config = new Configuration({
        clientId: 'YOUR_CLIENT_ID',
        clientSecret: 'YOUR_CLIENT_SECRET'
    });
    const conversionApi = new ConversionApi(config);

    // 2. Read the HTML file
    const htmlPath = path.resolve(__dirname, 'sample.html');
    const htmlBytes = fs.readFileSync(htmlPath);

    // 3. Upload the HTML file
    const uploadResult = await conversionApi.uploadFile({
        file: htmlBytes,
        fileName: 'sample.html'
    });
    const sourceFileId = uploadResult.id;

    // 4. Define conversion settings
    const convertSettings = {
        outputFormat: 'DOCX',
        options: {
            embedFonts: true,
            pageSize: 'A4',
            marginTop: 20,
            marginBottom: 20,
            marginLeft: 15,
            marginRight: 15
        }
    };

    // 5. Perform the conversion
    const convertResult = await conversionApi.convert({
        fileId: sourceFileId,
        convertSettings: convertSettings
    });
    const docxFileId = convertResult.id;

    // 6. Download the DOCX file
    const docxBytes = await conversionApi.downloadFile({ fileId: docxFileId });
    const outputPath = path.resolve(__dirname, 'output.docx');
    fs.writeFileSync(outputPath, docxBytes);
    console.log(`Conversion successful. DOCX saved to ${outputPath}`);
}

// Execute the conversion
convertHtmlToDocx().catch(err => {
    console.error('Conversion failed:', err);
});

Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (sample.html, output.docx), verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.

Executing Conversion via cURL and the REST API

If you prefer to work directly with HTTP, the following cURL commands perform the same conversion.

  1. Obtain an access token

    Replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with your credentials.

    curl -X POST "https://api.groupdocs.cloud/v2.0/oauth2/token" \
         -H "Content-Type: application/x-www-form-urlencoded" \
         -d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
    

    The response contains access_token.

  2. Upload the HTML file

    curl -X POST "https://api.groupdocs.cloud/v2.0/storage/files/sample.html" \
         -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
         -F "file=@sample.html"
    
  3. Start the conversion

    curl -X POST "https://api.groupdocs.cloud/v2.0/conversion/convert" \
         -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
         -H "Content-Type: application/json" \
         -d '{
               "sourceFilePath": "sample.html",
               "outputFilePath": "output.docx",
               "outputFormat": "DOCX",
               "options": {
                   "embedFonts": true,
                   "pageSize": "A4"
               }
             }'
    
  4. Download the converted DOCX

    curl -X GET "https://api.groupdocs.cloud/v2.0/storage/files/output.docx" \
         -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
         -o output.docx
    

For a complete list of parameters, see the official API documentation.

Configuring Options for HTML Content Conversion to DOCX

The SDK exposes several properties that let you fine‑tune the output:

  • embedFonts - Embeds all referenced fonts into the DOCX, ensuring visual fidelity.
  • pageSize - Sets the page size (A4, Letter, etc.).
  • marginTop / marginBottom / marginLeft / marginRight - Adjusts page margins in points.

Example of applying these options:

const convertSettings = {
    outputFormat: 'DOCX',
    options: {
        embedFonts: true,
        pageSize: 'Letter',
        marginTop: 30,
        marginBottom: 30,
        marginLeft: 20,
        marginRight: 20
    }
};

Refer to the ConversionOptions class for the full list.

Performance Considerations for HTML Content Conversion to DOCX

When converting large or complex HTML documents, keep these tips in mind:

  1. Reuse the API client - Creating a new ConversionApi instance for each file adds overhead.
  2. Stream large HTML files - Instead of loading the entire file into memory, use streams (fs.createReadStream) and the SDK’s uploadFileStream method.
  3. Limit embedded resources - Remove unnecessary images or scripts from the HTML before uploading to reduce processing time.
  4. Batch conversions - Process files sequentially in a loop, but respect the API rate limits to avoid throttling.

Applying these practices helps maintain low latency and modest memory consumption.

Conclusion

HTML to DOCX conversion in Node.JS becomes straightforward with the GroupDocs.Conversion Cloud SDK for Node.js. By following the steps above, you can integrate reliable DOCX generation from HTML into any server‑side application, customize the output, and optimize performance for large workloads. Remember to acquire a proper license for production use; you can purchase a subscription or obtain a temporary license from the temporary license page. Happy coding!

FAQs

How do I start an HTML to DOCX conversion in Node.JS?

Use the SDK to create a ConversionApi client, upload your HTML file, set the desired conversion options, and call the convert method. The process is fully asynchronous and returns the DOCX file identifier for download.

What options can I adjust for DOCX generation from HTML?

The ConversionOptions object lets you embed fonts, choose page size, set margins, and control image handling. See the API reference for all available settings.

Can I convert HTML that contains external CSS or images?

Yes. Upload any referenced resources (CSS, images) to the same storage folder before starting the conversion, and the SDK will resolve the links during processing.

Do I need a license for production deployments?

A valid license is required for production. You can obtain a temporary license for testing from the temporary license page and purchase a full subscription when you are ready to go live.

Read More