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. In this article, you will learn how to convert Excel Spreadsheets to PDF using Node.js.
The following topics shall be covered in this article:
- Excel to PDF Conversion REST API and Node.js SDK
- Convert Excel to PDF using a REST API in Node.js
- Convert Specific Excel Spreadsheets to PDF in Node.js
- Excel to PDF Conversion with Advanced Options
- Convert Excel to PDF without using Cloud Storage
- Convert Excel to PDF and Add Watermark
Excel to PDF Conversion REST API and Node.js SDK
For converting XLSX to PDF, I will be using the Node.js SDK of GroupDocs.Conversion Cloud API. The API allows you to convert your documents to any format you need. It supports the conversion of over 50 types of documents and images such as Word, Excel, PowerPoint, PDF, HTML, JPG, PNG, CAD. It also provides .NET, Java, PHP, Ruby, Android, and Python SDKs as its document conversion family members for the Cloud API.
You can install GroupDocs.Conversion Cloud to your Node.js application using the following command in the console:
npm install groupdocs-conversion-cloud
Please get your Client ID and Client 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 shown below:
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";
Convert Excel to PDF using a REST API in Node.js
You can convert Excel Spreadsheets to PDF documents on the cloud by following the simple steps given below:
- Upload the XLSX file to the Cloud
- Convert Excel to PDF using Node.js
- Download the converted PDF file
Upload the Excel File
Firstly, upload the XLSX file to the cloud using the following code sample:
// 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);
});
As a result, the uploaded XLSX file will be available in the files section of your dashboard on the cloud.
Convert Excel to PDF using Node.js
You can easily convert XLSX to PDF document programmatically by following the steps mentioned below:
- Create an instance of the ConvertApi
- Create an instance of the ConvertSettings
- Set the XLSX file path
- Assign “pdf” to format
- Provide the output file path
- Create ConvertDocumentRequest with ConvertSettings
- Convert by calling the ConvertApi.convertDocument() method with ConvertDocumentRequest
The following code example shows how to convert Excel Spreadsheet to a PDF document using a REST API in Node.js.
// 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);
Download the Converted File
The above code sample will save the converted PDF file on the cloud. You can download it using the following code sample:
// 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);
Convert Specific Excel Spreadsheets to PDF in Node.js
You can convert specific Excel Spreadsheets to PDF documents programmatically by following the steps mentioned below:
- Create an instance of the ConvertApi
- Create an instance of the ConvertSettings
- Set the XLSX file path
- Assign “pdf” to format
- Provide output file path
- Create an instance of PdfConvertOptions
- Provide specific Spreadsheets to convert
- Set PdfConvertOptions
- Create ConvertDocumentRequest with ConvertSettings
- Convert by calling the ConvertApi.convertDocument() method with ConvertDocumentRequest
The following code example shows how to convert a specific Excel Spreadsheet to a PDF document using a REST API in Node.js.
// 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);
Excel to PDF Conversion with Advanced Options
Please follow the steps mentioned below to convert XLSX to PDF document with some advanced settings:
- Create an instance of the ConvertApi
- Create an instance of the ConvertSettings
- Set the XLSX file path
- Assign “pdf” to format
- Provide output file path
- Create an instance of the SpreadsheetLoadOptions
- Set various load options such as hideComments, onePagePerSheet, etc.
- Create an instance of the PdfConvertOptions
- Set various convert options such as displayDocTitle, fromPage, pagesCount, margins (top, left, right, bottom), etc.
- Create ConvertDocumentRequest with ConvertSettings
- Convert by calling the ConvertApi.convertDocument() method with ConvertDocumentRequest
The following code example shows how to convert Excel Spreadsheet to a PDF document with advanced convert options.
// 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)
Convert Excel to PDF without using Cloud Storage
You can convert Excel Spreadsheets to PDF documents without using cloud storage by following the steps mentioned below:
- Create an instance of the ConvertApi
- Read input XLSX file from local path
- Create ConvertDocumentDirectRequest
- Provide target format and the input file path as input parameters
- Get results by calling the convertDocumentDirect() method with ConvertDocumentDirectRequest
- Save the output file to the local path using FileStream.writeFile() method
The following code example shows how to convert Excel Spreadsheet to a PDF document without using cloud storage. It means you will pass the input file in the request body and receive the output file in the API response.
// 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);
Convert Excel to PDF and Add Watermark
You can convert Excel Spreadsheets to watermarked PDF documents by following the steps mentioned below:
- Create an instance of the ConvertApi
- Create an instance of the ConvertSettings
- Set the XLSX file path
- Assign “pdf” to format
- Provide output file path
- Create an instance of the WatermarkOptions
- Set Watermark Text, Color, Width, Height, etc.
- Define the PdfConvertOptions and assign WatermarkOptions
- Create ConvertDocumentRequest with ConvertSettings
- Convert by calling the ConvertApi.convertDocument() method with ConvertDocumentRequest
The following code example shows how to convert Excel Spreadsheet to a PDF document and add a watermark to the converted PDF document using a REST API in Node.js.
// 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);
Try Online
Please try the following free online XLSX to PDF conversion tool, which is developed using the above API. https://products.groupdocs.app/conversion/xlsx-to-pdf
Conclusion
In this article, you have learned how to convert Excel to PDF documents on the cloud. You have also learned how to add a watermark to the converted PDF document using Node.js. Moreover, you have learned how to convert Excel Spreadsheets to PDF documents without using cloud storage. Furthermore, you have learned how to programmatically upload the XLSX file on the cloud and then download the converted file from the cloud. You can learn more about GroupDocs.Conversion 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.