How to Delete Pages from PDF Files with JavaScript in NodeJS
Why Delete PDF Pages with JavaScript?
The pdfRest Split PDF API Tool is a powerful resource that allows you to programmatically delete pages from a PDF document. This tutorial will guide you through the process of sending an API call to the Split PDF endpoint using JavaScript.
Imagine you have a large PDF report and you need to remove specific sections prior to distribution. Instead of manually deleting pages, you can automate the process using the Split PDF API, saving time and reducing the risk of errors.
In this tutorial we will be using the pdfRest Split PDF API Tool to generate a new PDF file with your unwanted pages deleted from your original PDF file. We will generate a PDF with a single page deleted.
Delete PDF Pages with JavaScript Code Example
/** * This request demonstrates how to split 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')); data.append('pages[]', '1-5,7-last') // delete page 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 code reference: pdf-rest-api-samples on GitHub
Breaking Down the Code
The provided code uses the Axios library to send an HTTP POST request to the pdfRest API. Here's how it works:
var axios = require('axios'); var FormData = require('form-data'); var fs = require('fs');
This snippet includes the necessary modules: Axios for HTTP requests, FormData to build the multipart/form-data payload, and fs to read the file system.
var data = new FormData(); data.append('file', fs.createReadStream('/path/to/file')); data.append('pages[]', '1-5,7-last') // delete page 6 data.append('output', 'pdfrest_split_pdf');
A FormData instance is created, and the PDF file along with the split parameters are appended. The 'pages[]' parameter defines how the PDF should be split. By including pages 1-5 and 7-last, page 6 is deleted from the document.
var config = { // ... other configurations ... 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 };
The configuration object for Axios includes the API endpoint, headers with the API key, and the form data. 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); });
The Axios instance sends the request and handles the response or error. The response data is logged to the console.
Beyond the Tutorial
In this tutorial, you've learned how to delete pages from a PDF using the pdfRest Split PDF API and JavaScript. This can be incredibly useful for a variety of applications where pages need to be removed from PDF documents.
To explore more possibilities and to demo all of the pdfRest API Tools, visit the API Lab. For a deeper understanding and more options, refer to the API Reference documentation.
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.