How to Convert ODT to PDF in Node.js
Take advantage of a free API for automating Open Document Text conversions to PDF.
We’ve discussed the benefits of using standard, free-to-use word processing formats like Open Document Text (ODT) over proprietary formats like DOCX in previous posts. While it’s more uncommon to see ODT used in larger organizations, it’s still frequently leveraged by smaller teams and groups who want to avoid the costs associated with acquiring proprietary format licenses.
With ODT, however, we still run into the same challenges as any other word processing format. ODT is great for generating and structuring written content, but it isn’t ideal for presenting. If we share ODT files with stakeholders outside our organization, we might find that our content doesn’t display the same on everyone’s web browsers, desktops, or tablets. Additionally, since ODT files are compatible with popular proprietary word processors like MS Word, we might find that users in those proprietary environments load ODT files into a new application and unknowingly change important aspects of our original document formatting.
Converting our ODT content to PDF will solve most of these problems, leaving us with a much more secure version of our original content. While it’s simple enough to manually export ODT files via Apache OpenOffice Writer, it’s a bit more complex to automate ODT to PDF conversions in a Node.js web application. In this article, we’ll learn how to take advantage of a free API that handles this conversion quickly and easily on our behalf, returning high fidelity PDFs while abstracting the bulk of the memory from that operation to an external server.
Step 1: Install the Cloudmersive Convert API SDK
To get our API call started, we’ll first run the following NPM command to install the SDK:
npm install cloudmersive-convert-api-client --save
Alternatively, we can manually add the below snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Step 2: Configure the Convert API & Configure Authorization
In our next step, we’ll knock out two important parts of our API call in one snippet. We’ll require the client and set up the instance, and we’ll then configure API key authorization.
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';
We’ll need an API key to authorize our requests, and we can get one by visiting the Cloudmersive website & creating a free account. Free accounts allow a limit of 800 API calls per month with zero commitments (our total will reset each month).
Step 3: Convert ODT to PDF
Now we’ll use this final snippet to create an API instance, read the input file, define the callback for handling our API response, and call the API method.
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.convertDocumentOdtToPdf(inputFile, callback);
If any errors occur, we’ll find detailed error messages printed to our console. If our conversion is successful, we can simply save the PDF to a new file and process as needed thereafter in our workflow.
That’s all there is to it! If you have any questions about leveraging Cloudmersive Convert APIs in your Node.js applications, feel free to [reach out](https://portal.cloudmersive.com/contact-sales) to any member of our team.