The pdfRest Split PDF API Tool is a powerful utility for developers who need to programmatically extract PDF pages into separate files. This tutorial will guide you through the process of extracting pages by sending an API call to the Split PDF endpoint using JavaScript.
For instance, a user might want to split a PDF report into individual chapters, or extract only certain pages from a large document for targeted distribution.
/** * This request demonstrates how to extract pages from a PDF document into three files by specifying the pages that will be included in each. * pages[] is an optional parameter that accepts page numbers and/or ranges separated by commas. In addition, pages[] supports several keywords: even, odd, and last. */ 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.pdf')); data.append('pages[]', 'even'); data.append('pages[]', 'odd'); data.append('pages[]', '1,3,4-6'); data.append('output', 'pdfrest_split_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/split-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.
Source: pdf-rest-api-samples on GitHub
This JavaScript code snippet uses the Axios library for HTTP requests and the FormData library to create multipart/form-data payloads. Here's how each part of the code works:
var axios = require('axios'); var FormData = require('form-data'); var fs = require('fs');
This section imports the required modules for making HTTP requests, handling form data, and interacting with the file system.
var data = new FormData(); data.append('file', fs.createReadStream('/path/to/file.pdf')); data.append('pages[]', 'even'); data.append('pages[]', 'odd'); data.append('pages[]', '1,3,4-6'); data.append('output', 'pdfrest_split_pdf');
Here, a new FormData instance is created, and the PDF file along with the split parameters are appended to it. The 'pages[]' parameter specifies the pages to include in each split file, and 'output' names the resulting files.
var config = { method: 'post', maxBodyLength: Infinity, url: 'https://api.pdfrest.com/split-pdf', headers: { 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', ...data.getHeaders() }, data : data };
The Axios request configuration is set up, including the HTTP method, endpoint URL, headers (including the API key), and the data payload.
axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); });
The Axios library sends the HTTP request with the provided configuration. The response is logged to the console on success, and any errors are caught and logged.
In this tutorial, we've learned how to use JavaScript to call the pdfRest Split PDF API to extract pages from a PDF into separate files. You can now apply this knowledge to split PDFs in your own applications. To experiment with all of the pdfRest API Tools, visit the API Lab and refer to the API Reference documentation for more information.
Note: This is an example of a multipart API call. For code samples using JSON payloads, visit pdf-rest-api-samples with JSON Payload.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.