How to convert HTML to a Word DOCX Document in Node.js
1 min readMay 16, 2019
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!