How to Convert Microsoft Excel to PDF with JavaScript in NodeJS
Why Convert Excel to PDF with JavaScript?
The pdfRest Convert to PDF API Tool is a powerful resource for developers looking to automate the process of converting Excel spreadsheets into PDFs. This API tool is particularly useful when you need to ensure that documents are in a standardized format for distribution, storage, or printing. In this tutorial, we will demonstrate how to send an API call to Convert to PDF using JavaScript, which is a common language for web-based applications and server-side scripting.
Imagine you are running an online service where users upload documents in different formats, such as Word, Excel, or images, and you need to convert these documents into PDFs before sending them to other users or storing them securely. Using JavaScript to call the Convert to PDF API, you can seamlessly integrate this functionality into your application, providing a smooth user experience and reliable document handling.
Convert Excel to PDF with JavaScript Code Example
var axios = require('axios'); var FormData = require('form-data'); var fs = require('fs'); // Create a new form data instance and append the PDF file and parameters to it var data = new FormData(); data.append('file', fs.createReadStream('/path/to/file.xlsx')); data.append('compression', 'lossy'); data.append('downsample', '300'); data.append('tagged_pdf', 'off'); data.append('output', 'pdfrest_pdf'); // define configuration options for axios request var config = { method: 'post', maxBodyLength: Infinity, // set maximum length of the request body url: 'https://api.pdfrest.com/pdf', headers: { 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', // Replace with your API key ...data.getHeaders() // set headers for the request }, data : data // set the data to be sent with the request }; // send request and handle response or error axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); }); // If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample.
Reference to the code: pdf-rest-api-samples on GitHub.
Breaking Down the Code
The provided code snippet is a JavaScript example that uses the Axios library to make an HTTP POST request to the pdfRest Convert to PDF API endpoint. Here's a breakdown of the key components:
var data = new FormData(); data.append('file', fs.createReadStream('/path/to/file.xlsx')); data.append('compression', 'lossy'); data.append('downsample', '300'); data.append('tagged_pdf', 'off'); data.append('output', 'pdfrest_pdf');
This section creates a new FormData object and appends the file to be converted along with various conversion parameters:
file
: The file stream of the document you want to convert.compression
: The type of compression to apply to the PDF ('lossy' or 'lossless').downsample
: The DPI to which images should be downsampled.tagged_pdf
: Whether to create a tagged PDF ('on' or 'off').output
: The name of the output parameter, which is set to 'pdfrest_pdf'.
var config = { method: 'post', maxBodyLength: Infinity, url: 'https://api.pdfrest.com/pdf', headers: { 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', ...data.getHeaders() }, data : data };
This config object contains the settings for the Axios request:
method
: The HTTP method, which is 'post' for this API call.maxBodyLength
: Allows the request body to be of any size by setting it to Infinity.url
: The API endpoint URL.headers
: Contains the API key for authentication and the headers from FormData.data
: The FormData object containing the file and parameters.
The Axios request is sent, and the response or error is handled in the subsequent then
and catch
blocks.
Beyond the Tutorial
In this tutorial, you've learned how to use JavaScript to call the pdfRest Convert to PDF API, which allows for the conversion of various file formats to PDF. This is a practical tool for any developer looking to integrate document conversion features into their applications.
Demo all of the pdfRest API Tools in the API Lab and refer to the API Reference Guide for more detailed information on how to use the API.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdf-rest-api-samples on GitHub.