Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert Excel Spreadsheets to PDF using a REST API in Node.js
Excel spreadsheets are widely used to maintain invoices, ledgers, inventory, accounts, and other reports. Excel to PDF conversion allows sharing Excel data with others in a portable form. As a Node.js developer, you can easily convert your Excel Spreadsheets to PDF documents programmatically on the cloud. Here, you will find code examples to convert Excel Spreadsheets to PDF using a REST API in Node.js
The following code examples are included here:
1. Cloud Configurations
2. Upload XLSX File to the Cloud
3. Convert Excel to PDF using a REST API in Node.js
4. Excel to PDF Conversion with Advanced Options
5. Convert Excel to PDF without using Cloud Storage
6. Convert Excel to PDF and Add Watermark
7. Dowload Converted PDF File
global.clientId = "112f0f38-9dae-42d5-b4fc-cc84ae644972";
global.clientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618";
global.myStorage = "";
const config = new groupdocs_conversion_cloud.Configuration(clientId, clientSecret);
config.apiBaseUrl = "https://api.groupdocs.cloud";
// initialize api
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// define convert settings
let settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.filePath = "sample.xlsx"; // input file path
settings.format = "pdf"; // convert format
settings.outputPath = "output"; // output file folder on the cloud storage
// create convert document request
let request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
// convert document
let result = await convertApi.convertDocument(request);
// show converted file path
console.log("Document converted successfully: " + result[0].url);
// initialize api
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// read file from local path
let file = fs.readFileSync('C:\\Files\\sample.xlsx');
// create convert document direct request
let request = new groupdocs_conversion_cloud.ConvertDocumentDirectRequest("pdf", file);
// convert document
let result = await convertApi.convertDocumentDirect(request);
// save output file to specified path
fs.writeFile("C:\\Files\\sample_direct.pdf", result, "binary", function (err) { });
console.log("Document converted: " + result.length);
// initialize api
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// define convert settings
let settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.filePath = "sample.xlsx";
settings.format = "pdf";
// define PDF convert options
let convertOptions = new groupdocs_conversion_cloud.PdfConvertOptions();
convertOptions.pages = [1]; // define pages (sheets) to convert
settings.convertOptions = convertOptions
settings.outputPath = "specific_pages.pdf";
// create convert document request
let request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
// convert document
let result = await convertApi.convertDocument(request);
console.log("Document converted successfully: " + result[0].url);
// initialize api
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// define convert settings
var settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.storageName = myStorage;
settings.filePath = "sample.xlsx";
settings.format = "pdf";
// define spreadsheet load options
var loadOptions = new groupdocs_conversion_cloud.SpreadsheetLoadOptions();
loadOptions.hideComments = true; // hide comments
loadOptions.onePagePerSheet = true; // set one page per sheet
settings.loadOptions = loadOptions;
// define PDF convert options
let convertOptions = new groupdocs_conversion_cloud.PdfConvertOptions();
convertOptions.centerWindow = true; // set position of the document's window. Default: false.
convertOptions.displayDocTitle = true; // display document title. Default: false
convertOptions.fromPage = 2; // Start conversion from
convertOptions.pagesCount = 3; // Number of pages(sheets) to convert
convertOptions.marginTop = 5; // top margin
convertOptions.marginLeft = 5; // left margin
settings.convertOptions = convertOptions;
settings.outputPath = "converted.pdf";
// create convert document request
var request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
// create document
convertApi.convertDocument(request)
// initialize api
let convertApi = groupdocs_conversion_cloud.ConvertApi.fromKeys(clientId, clientSecret);
// define convert settings
let settings = new groupdocs_conversion_cloud.ConvertSettings();
settings.filePath = "sample.xlsx";
settings.format = "pdf";
settings.outputPath = "converted";
// define watermark options
let watermark = new groupdocs_conversion_cloud.WatermarkOptions();
watermark.text = "CONFIDENTIAL";
watermark.bold = true;
watermark.fontSize = 44;
watermark.color = "Gray";
watermark.background = false;
watermark.rotationAngle = 30;
watermark.left = 100;
watermark.top = 250;
// define PDF convert options
let convertOptions = new groupdocs_conversion_cloud.PdfConvertOptions();
convertOptions.watermarkOptions = watermark;
// assign convert options
settings.convertOptions = convertOptions
// create convert document request
let request = new groupdocs_conversion_cloud.ConvertDocumentRequest(settings);
// convert document
let result = await convertApi.convertDocument(request);
// show output document path
console.log("Document converted successfully: " + result[0].url);
// construct FileApi
var fileApi = groupdocs_conversion_cloud.FileApi.fromConfig(config);
// create download file request
let request = new groupdocs_conversion_cloud.DownloadFileRequest("output/sample.pdf", myStorage);
// download file
let response = await fileApi.downloadFile(request);
// copy file to working directory
fs.writeFile("C:\\Files\\converted.pdf", response, "binary", function (err) { });
console.log(response);
// open file in IOStream from local/disc.
var resourcesFolder = 'C:\\Files\\sample.xlsx';
fs.readFile(resourcesFolder, (err, fileStream) => {
// construct FileApi
var fileApi = groupdocs_conversion_cloud.FileApi.fromConfig(config);
// create upload file request
var request = new groupdocs_conversion_cloud.UploadFileRequest("sample.xlsx", fileStream, myStorage);
// upload file
fileApi.uploadFile(request);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment