How to Convert Email MSG Files to PDF using Node.js

Cloudmersive
2 min readNov 6, 2023

--

If we’re storing or sharing our Outlook MSG files outside of our email application, converting them to PDF format makes the most sense.

Thankfully, using the below code, we can easily convert our MSG files to PDF using a free API solution. In our request, we can also choose whether to convert the entire email into the text portion of our PDF (including the subject line & other such information) or to return those details separately as properties in the API response object.

We can begin structuring our request by installing the SDK. We can either run the following command:

npm install cloudmersive-convert-api-client --save

Or we can add the below snippet to our package.json:

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

Finally, we can include the below code examples in our file, and we can authorize our request for free with a free-tier API key to make up to 800 API calls per month with zero additional commitments:

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").buffer); // File | Input file to perform the operation on.

var opts = {
'bodyOnly': true // Boolean | Optional; If true, the HTML string will only include the body of the email. Other information such as subject will still be given as properties in the response object. Default is false.
};

var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertDocumentMsgToPdf(inputFile, opts, callback);

Keeping bodyOnly equal to “True” will return only the body of the email as text and return all other information as properties in the API response.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet