How to convert Word DOCX to PDF in Node.JS

First, we want to add a reference to package.json for the document processing library:

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

Now, we simply call convertDocumentAutodetectToPdf to convert our Word Document (DOCX) format file into a PDF:

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.ConvertDocumentApi();var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile.docx").buffer); // File | Input file to perform the operation on.var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertDocumentAutodetectToPdf(inputFile, callback);

So it’s pretty easy to get this done, and it is free!

Note that this function will automatically detect the type of document, whether it is a DOCX, DOC, or even other Office file formats such as XLSX, XLS, PPTX or PPT.

This same library also includes functions to read or write to DOCX files as well. For example, you can enumerate tables, images, paragraphs, headers/footers/etc. and modify or insert tables, images, and so on into the document if needed.

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