How to Convert ODS to PNG in Node.js
A PNG snapshot of an ODS spreadsheet offers a lightweight alternative to PDF, and it’ll make storing and displaying that data far easier on the web.
Using the below ready-to-run Node.js code examples, we can take advantage of an API designed to convert our single-page or multi-page ODS files into PNG images or PNG arrays. It’s a quick and easy conversion — all we need to do is install the SDK, authorize our request with a free-tier API key (obtainable on the Cloudmersive website), and load our files into memory.
We have two options for SDK installation. We can either run this command:
npm install cloudmersive-convert-api-client --save
Or add this snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Now we can include the below ready-to-run code examples in our file and provide our API key:
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.convertDocumentOdsToPng(inputFile, callback);
That’s all there is to it — no more code required.