How to create a Web-Based Document Viewer in Node.JS

Today’s tutorial covers the creation of a web-based viewer for a variety of document types, including PDF, DOCX, XLSX, and other Office formats. In order to achieve this within a reasonable timeframe, we will be using an API, which will allow us to finish quite rapidly indeed. Here’s the general process.

First comes our API client setup, which uses this command here for npm install.

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

With our client installed, now we can call viewerToolsCreateSimple:

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.ViewerToolsApi();var inputFile = "/path/to/file"; // 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.viewerToolsCreateSimple(inputFile, callback);

Now you can enter a document and the API will return an HTML-based embed code for your viewer. Done!

Image for post
Image for post

Written by

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

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