How to Convert RTF to PDF in Node.js
Rich Text Format files are plug-and-play with word processors on any system. To use them in a PDF reader, however, we first need to convert those files to PDF.
Using the below code, we can easily convert our RTF files to PDF format with a free-to-use API. All we need is a free-tier API key — which we can get by registering a free account on the Cloudmersive website — and we’ll be able to make up to 800 API calls per month with no additional commitments.
Let’s first install the SDK. We can either run the following command:
npm install cloudmersive-convert-api-client --save
Or add the following snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Finally, we can structure our request using the ready-to-run code examples below:
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 callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.convertDocumentRtfToPdf(inputFile, callback);
Now we can easily convert RTF to PDF in seconds.