How to convert HTML to a Word DOCX Document in Node.js

The first thing we will need to do is add a reference to the library in our NPM file:

"dependencies": {
"cloudmersive-convert-api-client": "^1.2.7"
}

Then, we need to import the library:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');

Finally, all we need to do is call convertWebHtmlToDocx to convert our HTML string into a full-fledged Microsoft Word DOCX file:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveConvertApiClient.ConvertWebApi();var inputRequest = new CloudmersiveConvertApiClient.HtmlToOfficeRequest(); // HtmlToOfficeRequest |var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertWebHtmlToDocx(inputRequest, callback);

Note that the HTML is a string so it is very easy to build. Also, the HTML file can contain images, just be sure to use fully-qualified URLs. The HTML file can also contain styling and other rich content. This makes it incredibly easy to rapidly build Word DOCX files while still using your favorite formatting language, HTML.

It’s that easy!

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store