How to Delete Pages from PDF using PDF.co Web API

The tutorial and the sample source code explain how to delete any page from a PDF document using JavaScript. This tutorial provides a complete process of deleting PDF pages using the PDF.co Web API.

Features of PDF.co Web API

The PDF.co Web API proves helpful in analyzing and editing any PDF document by using multiple programming languages according to the user’s choice. The API possesses the functionality of altering any PDF document by analyzing and deleting pages from it. The API works over a highly secured system and transmits the data files through an encrypted connection. The API automates the altering process of documents and saves time and money. Below are the full details of the PDF.co Web API to delete pages and a demo explaining these features through a sample code.

Endpoint Parameters

Following is a brief explanation of the endpoint parameters of the PDF.co Web API’s endpoint parameters for the user’s understanding:

  1. url: It is a required parameter that contains the URL of the document the user provides to make changes. The parameter can support URLs from various resources such as Dropbox, Google Drive, or PDF.co built-in storage. The API takes the URL from this parameter and loads the form to make changes to it whenever a user makes such a request.

  2. httpusername: It is an optional parameter that contains the authentication user name if the user makes sure that it is compulsory to access the source URL.

  3. httppassword: It is an optional parameter containing the authentication password provided by the user if it is compulsory for the source URL.

  4. pages: An optional parameter that provides information regarding which pages to delete from the PDF document. Using a dash “- “, the user can just pride the single page number or a comma-separated range of pages using a dash “-”. If the user does not provide any page number or range and leaves the field empty, the program will delete all the pages from the PDF document. Users must provide this parameter in the form of a string.

  5. name: It is an optional parameter that the user can choose to provide a name for the altered output file. This parameter must be in the form of a string.

  6. expiration: This optional parameter provides an expiration time for the output URL to reside in the PDF.co temporary file storage.

  7. profiles: This is an optional parameter for setting custom configurations. This parameter must also be a string.

Example Using JavaScript

Following is the source code to show users how to delete required pages from a sample PDF document using the PDF.co Web API. The sample source code is in JavaScript and provides a program to delete specific pages from the document using some of the API parameters. Users can provide the URL of any document they want to edit and alter efficiently using this secure API.

To complete this process, the user needs to provide an API key in the program, which the PDF.co platform generates itself for that specific user when they log in. Moreover, the user has to provide the URL for the PDF document in the API request for the program to access the required document. Moreover, the PDF.co API provides the user with a resulting file after completing and executing the program. The user can download the file or visit the resulting URL to witness the provided PDF document changes.

Sample Code Snippet

The following source code contains the program to delete pages from a PDF:

// Requiring module
const express = require('express');
// Creating express object
const app = express();
var request = require('request');
var options = {
   'method': 'POST',
   'url': 'https://api.pdf.co/v1/pdf/edit/delete-pages',
   'headers': {
     'x-api-key': '*******************************************************'
   },
   formData: {
     'url':'https://juventudedesporto.cplp.org/files/sample-pdf_9359.pdf',
       'pages': '1-3',
       'name': 'result.pdf'
   }
 };

 request(options, function (error, response) {
   if (error) throw new Error(error);
   console.log(response.body);
 });

Step-By-Step Guide To Use PDF.co Web API

Following is the step by step explanation of the sample source code to make the users comprehend the functioning of Web API from PDF.co:

  1. express is a Node.js framework that is helpful for web applications and helps with the server-side and networking. Express is the module or a JavaScript library to import into the code to use its functionalities.

  2. request is another module for Node.js, which provides the simplest way to make an HTTP call in Node.js.

  3. Require parameter helps in including essential modules in the application. It can load critical files in the application, such as the Node.js, community, and local modules. Users can use the modules express and request by using “require” in their application.

  4. The next thing is to provide the request method, URL, and API key to access the API functionalities. After logging in to the PDF.co platform, the users can obtain their specific API keys after logging in to the PDF.co platform to access their web API.

  5. The reason is that the users can not send a direct request, so the API key acts as an access token, and the users have to provide it in the header for authentication.

  6. The next step is to provide relevant information for alteration. The users have to provide information in the payload like the URL for the document, the page numbers to be deleted, and the resulting file’s name.

  7. Send the POST request and wait for the response. The output response will provide the error, status, file name, and others. The status 200 determines that the request was successful, and the code provides the URL for the resulting file.

  8. If the request is unsuccessful, alter the program and make the corrections accordingly to get the required output.

  9. The users can view the edited document using the resulting URL or download the file by using the download functionality of the Web API. Moreover, the users can write a customized function to download the required file effectively from the response URL.

  10. Users can also specify the server to work on a specific port number and handle the get request, but that is up to the user.

  11. Users can edit this code and provide a URL of their own choice to edit their required PDF document and provide their API key to access the Web API, or they can write their customized function to obtain the required results.

Sample PDF Document

Following is the beginning of the sample document for this example:

Sample PDF Document

Output PDF Document

Below is the screenshot of the edited sample document after deleting pages 1, 2, and 3 for visualization:

Output PDF Document

In this tutorial, you have learned how to delete any page from a PDF document using JavaScript. It provides source code, a demo, and the complete process of deleting PDF pages using the PDF.co Web API.