Render Excel Data to PDF using REST API in Node.js

You can easily render Microsoft Excel spreadsheet data to PDF on the cloud. It can be useful in such a case when you have to present your data to relevant stakeholders without sharing the actual Excel data files with them. As a Node.js developer, you can render spreadsheet data from XLS or XLSX files in PDF documents programmatically on the cloud. This article will be focusing on how to render Excel data to PDF using a REST API in Node.js.

Document Viewer REST API and Node.js SDK

For rendering XLS or XLSX spreadsheets, I will be using the Node.js SDK of GroupDocs.Viewer Cloud API. It allows you to programmatically render and view all sorts of popular document and image file formats such as Word, Excel, PowerPoint, PDF, Visio, Project, Outlook, JPG, PNG, etc. It also provides .NET, Java, PHP, Ruby, Android, and Python SDKs as its document viewer family members for the Cloud API.

You can install GroupDocs.Viewer Cloud to your Node.js application using the following command in the console:

npm install groupdocs-viewer-cloud 

Please get your Client ID and Secret from the dashboard before you start following the steps and available code examples. Once you have your ID and secret, add in the code as demonstrated below:

global.clientId = "112f0f38-9dae-42d5-b4fc-cc84ae644972";
global.clientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618";
global.myStorage = "";

const configuration = new groupdocs_viewer_cloud.Configuration(clientId, clientSecret);
configuration.apiBaseUrl = "https://api.groupdocs.cloud";

Render Excel Data to PDF using a REST API in Node.js

You can render Microsoft Excel spreadsheet data to PDF by following the simple steps mentioned below:

Upload the Document

Firstly, upload the XLSX file to the cloud using the code example given below:

// construct FileApi
let fileApi = groupdocs_viewer_cloud.FileApi.fromConfig(configuration);
let resourcesFolder = 'C:\\Files\\sample.xlsx';

// read files one by one
fs.readFile(resourcesFolder, (err, fileStream) => {
  // create upload file request
  let request = new groupdocs_viewer_cloud.UploadFileRequest("sample.xlsx", fileStream, myStorage);
  // upload file
  fileApi.uploadFile(request);
});

As a result, the XLSX file will be uploaded to Cloud Storage and will be available in the files section of your dashboard.

Render Excel to PDF using Node.js

Please follow the steps mentioned below to render Excel data to PDF programmatically.

  • Create an instance of the ViewAPI
  • Create an instance of the FileInfo
  • Provide the input file path
  • Create an instance of the ViewOptions
  • Assign fileInfo to ViewOptions
  • Set the viewFormat as “PDF”
  • Create a view request by calling the CreateViewRequest method with ViewOptions
  • Get a response by calling the createView() method with CreateViewRequest

The following code snippet shows how to render Excel data to PDF using a REST API in Node.js.

// api initialization
let viewApi = groupdocs_viewer_cloud.ViewApi.fromKeys(clientId, clientSecret);

// provide input file
let fileInfo = new groupdocs_viewer_cloud.FileInfo();
fileInfo.filePath = "sample.xlsx";

// define view options
let viewOptions = new groupdocs_viewer_cloud.ViewOptions();
viewOptions.fileInfo = fileInfo;
viewOptions.viewFormat = groupdocs_viewer_cloud.ViewOptions.ViewFormatEnum.PDF;

// create view request
let request = new groupdocs_viewer_cloud.CreateViewRequest(viewOptions);

// cretae view
let response = await viewApi.createView(request);

console.log("Document rendered: " + response.file.path);
Render Excel Data to PDF using a REST API in Node.js

Render Excel Data to PDF using a REST API in Node.js

Download the Rendered File

The above code sample will save the rendered PDF file on the cloud. You can download it using the following code sample:

// initialize api
var fileApi = groupdocs_viewer_cloud.FileApi.fromConfig(configuration);

// create file download request
let request = new groupdocs_viewer_cloud.DownloadFileRequest("/viewer/sample_xlsx/sample.pdf", myStorage);

// download file
let response = await fileApi.downloadFile(request);

// save image file in working directory
fs.writeFile("C:\\Files\\sample.pdf", response, "binary", function (err) { });
console.log(response);

Render Excel to PDF With Rendering Options using Node.js

You can use specific rendering options to render Excel data into PDF programmatically by following the steps given below:

  • Create an instance of the ViewAPI
  • Provide the input file path to the FileInfo
  • Create an instance of the ViewOptions
  • Assign fileInfo and set the viewFormat as “PDF”
  • Create an instance of the PdfOptions
  • Create an instance of the SpreadsheetOptions
  • Set the SpreadsheetOptions such as textOverflowMode, renderGridLines, etc.
  • Create a view request by calling the CreateViewRequest method with ViewOptions
  • Get a response by calling the createView() method with CreateViewRequest

The following code snippet shows how to render Excel data to PDF with rendering options using a REST API in Node.js.

// api initialization
let viewApi = groupdocs_viewer_cloud.ViewApi.fromKeys(clientId, clientSecret);

// provide input file path
let fileInfo = new groupdocs_viewer_cloud.FileInfo();
fileInfo.filePath = "sample.xlsx";

// define view options
let viewOptions = new groupdocs_viewer_cloud.ViewOptions();
viewOptions.fileInfo = fileInfo;
viewOptions.viewFormat = groupdocs_viewer_cloud.ViewOptions.ViewFormatEnum.PDF;

// define pdf render options
viewOptions.renderOptions = new groupdocs_viewer_cloud.PdfOptions();

// create spreadsheet options
viewOptions.renderOptions.spreadsheetOptions = new groupdocs_viewer_cloud.SpreadsheetOptions();

// hide text in adjacent columns
viewOptions.renderOptions.spreadsheetOptions.textOverflowMode = groupdocs_viewer_cloud.SpreadsheetOptions.TextOverflowModeEnum.HideText;

// show grid line
viewOptions.renderOptions.spreadsheetOptions.renderGridLines = true;

// show hidden cloumns
viewOptions.renderOptions.spreadsheetOptions.renderHiddenColumns = true;

// show hidden rows
viewOptions.renderOptions.spreadsheetOptions.renderHiddenRows = true;

// create view request
let request = new groupdocs_viewer_cloud.CreateViewRequest(viewOptions);

// create view
let response = await viewApi.createView(request);

console.log("Document rendered: " + response.file.path);
Render Excel to PDF With Rendering Options using Node.js

Render Excel to PDF With Rendering Options using Node.js

Try Online

Please try the following free online spreadsheet rendering tool, which is developed using the above API. https://products.groupdocs.app/viewer/xlsx

Conclusion

In this article, you have learned how to render Excel spreadsheet data to PDF on the cloud. You have also learned how to render Excel data to PDF with rendering options in Node.js. This article also explained how to programmatically upload the XLSX file on the cloud and then download the rendered PDF file from the cloud. You can learn more about GroupDocs.Viewer Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also