How to convert a PNG Array to PDF in Node.JS

Sometimes PDF format is simply the most convenient way to present your content. To that end, I’ll be demonstrating the setup of a system for converting PNG arrays into PDF. As a bonus, this is incredibly quick.

With npm install, setup of our client is a breeze:

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

Calling our function comes next, demonstrated here:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveConvertApiClient.ConvertDocumentApi();var inputFile1 = "/path/to/file"; // File | First input file to perform the operation on.var inputFile2 = "/path/to/file"; // File | Second input file to perform the operation on.var opts = {'inputFile3': "/path/to/file", // File | Third input file to perform the operation on.'inputFile4': "/path/to/file", // File | Fourth input file to perform the operation on.'inputFile5': "/path/to/file", // File | Fifth input file to perform the operation on.'inputFile6': "/path/to/file", // File | Sixth input file to perform the operation on.'inputFile7': "/path/to/file", // File | Seventh input file to perform the operation on.'inputFile8': "/path/to/file", // File | Eighth input file to perform the operation on.'inputFile9': "/path/to/file", // File | Ninth input file to perform the operation on.'inputFile10': "/path/to/file" // File | Tenth 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.convertDocumentPngArrayToPdf(inputFile1, inputFile2, opts, callback);

As you can see, you may use up to 10 input files with one call. If you need to use more PNGs, simply perform this function as many times as necessary, then merge the PDF files together using another of our API functions, mergeDocumentPdf.

Image for post
Image for post

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