The pdfRest PDF to Excel API Tool allows developers to convert PDF documents into editable Excel files programmatically. This tutorial will demonstrate how to make an API call to the PDF to Excel API using JavaScript.
A user might use PDF to Excel conversion when they need to extract tabular data from a PDF report and manipulate or analyze it in a spreadsheet format.
// This request demonstrates how to convert a PDF to an Excel file. 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')); data.append('output', 'pdfrest_excel_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/excel', 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.
Source code reference: pdf-rest-api-samples
The code above uses the axios
library to make HTTP requests and form-data
to handle multipart/form-data, which is required for file uploads. Let's break down the key parts of the code:
var data = new FormData(); data.append('file', fs.createReadStream('/path/to/file')); data.append('output', 'pdfrest_excel_pdf');
This creates a new FormData
object and appends the PDF file and output parameters. The file is read from the specified path, and the output parameter is the desired name for the converted Excel file.
var config = { method: 'post', maxBodyLength: Infinity, url: 'https://api.pdfrest.com/excel', headers: { 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', ...data.getHeaders() }, data : data };
The configuration object for the axios
request sets the HTTP method to POST, allows for an unlimited request body length, specifies the API endpoint, and includes the necessary headers, such as the API key and content-type. Replace 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' with your actual API key.
axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); });
This sends the request and logs the response or error. The response will contain information about the converted Excel file, such as a download link or resource ID, depending on the API's configuration.
By following this tutorial, you've learned how to make a multipart API call to convert a PDF to an Excel file using JavaScript. You can now integrate this functionality into your applications to automate the process of extracting data from PDFs into a more accessible and editable format.
Feel free to demo all of the pdfRest API Tools in the API Lab and refer to the API Reference documentation for more information.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at JSON Payload excel.js.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.