How to Ensure PDF Protection using PDF.co Web API in JavaScript

The tutorial and the sample source code explain how to add any password and security limitation to any PDF file and remove any password and restrictions from an existing PDF file. This tutorial provides a guideline on these password and security features using JavaScript and explains through a complete process how to use PDF.co Web API for using these features.

Features of PDF Password and Security API Endpoint

The PDF.co web API helps edit PDF documents using multiple programming languages to support various platforms and facilitate the users. Similarly, the API allows altering any PDF document by adding or removing any password and security limitations. The most important thing to consider here is that the API provides a secure system and uses encrypted connections to transmit the data files. Moreover, the automation process saves time and cost in editing any document. Following are the details of PDF Password And Security API, along with a demo to demonstrate the use of these features through a simple example and sample code.

Endpoint Parameters

Following is the explanation of the endpoint parameters of the PDF Password And Security API for better understanding. The following parameters are for addition and deletion functionalities:

  1. URL: It is a required parameter that stores the link to the source file. It contains the URL of the PDF document the user provides for alteration. This parameter supports URLs from Dropbox, Google Drive, built-in cloud storage of PDF.co., or any temporary file before making the API call. This parameter must be a string.

  2. ownerPassword: This parameter contains the primary owner password required to add or delete security limitations and encryption methods.

  3. userPassword: An optional parameter might be necessary for viewing and printing the document.

  4. encryptionAlgorithm: This parameter contains the encryption algorithm used for the process, and it might have values such as RC4_40bit, RC4_128bit, AES_128bit, and others.

  5. allowAccessibilitySupport: This parameter contains information (true or false) regarding allowing or prohibiting content extraction for accessibility after entering the userPassword.

  6. allowAssemblyDocument: This parameter contains information (true or false) regarding allowing or prohibiting the assembling of the PDF document.

  7. allowPrintDocument: This parameter contains information (true or false) regarding allowing or prohibiting printing the PDF document.

  8. allowFillForms: This parameter contains information (true or false) regarding allowing or prohibiting filling interactive PDF forms, including the signature fields.

  9. allowModifyDocument: This parameter contains information (true or false) regarding allowing or prohibiting modification of any PDF document.

  10.  allowContentExtraction: This parameter contains information (true or false) regarding allowing or prohibiting copying any content from the PDF document.

  11.  allowModifyAnnotations: This parameter contains information (true or false) regarding interaction with text annotations and forms in the PDF documents.

  12.  printQuality: This parameter contains information (HighResolution or LowResolution) regarding the allowed printing quality of the PDF documents.

  13.  encrypt: It is an optional parameter that helps enable encryption (true or false)  for the output file while being stored in the cloud.

  14.  name: It is an optional parameter that contains the name for the output file.

Similarly, there could be parameters such as async, expiration, and profiles.

Example using JavaScript

Following are the source codes to show users how to add and remove passwords from the PDF documents using PDF.co Password and Security API. These sample source codes are in JavaScript and provide programs to add and remove passwords from the PDF documents using some of the essential API parameters. Users can provide the URL of any document they want to alter using this API.

The users need to provide their personalized API key in the program. The PDF.co platform generates this API key for users when they login to the system. Furthermore, the users have to provide the URL for the PDF document in the API request for the program to access the document. The PDF.co API provides the user with a resulting output file after program execution. The users can open the output link for accessing the PDF document to analyze the changes.

Sample Code Snippet for Password Addition

The following source code contains the program to add the password to an existing PDF document:

var request = require('request');
var options = {
  'method': 'POST',
  'url': 'https://api.pdf.co/v1/pdf/security/add',
  'headers': {
    'x-api-key': '**************************************'
  },

  formData: {
   "url": "https://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/pdf-merge/sample1.pdf",
   "ownerPassword": "12345",
   "userPassword": "54321",
   "EncryptionAlgorithm": "AES_128bit",
   "name": "output-protected.pdf",
  }

};

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

Step-by-Step Guide to Use PDF Password and Security for Addition

The following step-by-step guide explains the source code to add the password to a document:

  1. The “require” parameter includes all the essential modules in the application. It loads critical files such as the Node.js, community, and local modules. Moreover, the “request” is a Node.js module that provides a simpler way to make an HTTP call.

  2. The next thing is to provide the request method, the URL, and an API key to access the API functionalities. The users have to provide the API key in the header for authentication.

  3. The next step is to provide relevant information for alterations. The users have to provide information in the payload, such as the URL of the document,  ownerPassword, userPassword, EncryptionAlgorithm, and the name of the output file.

  4. The user has to send the POST request and wait for the response. The status 200 determines the successful request, and the code provides the URL for the resulting file. However, if the request was unsuccessful, alter the code and make corrections.

  5. The users can view the edited document by opening the resultant URL from the code.

  6. Similarly, the users can add more restrictions to the PDF document using the same API functionality and edit the parameters.

Sample PDF Document

Sample PDF Document

Secure PDF Output with Password

Below is the screenshot of the edited document after adding the password using the PDF Password And Security API.

Secure PDF Document

Sample Code Snippet for Password Removal

The following source code contains the program to remove the password from an existing PDF document:

var request = require('request');
var options = {
  'method': 'POST',
  'url': 'https://api.pdf.co/v1/pdf/security/remove',
  'headers': {
    'x-api-key': '**********************************************************'
  },

  formData: {
   "url": "https://pdf-temp-files.s3.amazonaws.com/U5Q4LRYD7E0XXXZZ02ZJLNL6NWPJ8JH0/output-protected.pdf",
   "password": "12345",
   "name": "output-unprotected.pdf",
  }

};

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

Step-by-Step Guide to Use PDF Password and Security for Removal

Below is the step-by-step guide for an explanation of the source code to remove the password from a document:

  1. The first thing is to add the required modules.

  2. The next thing is to provide the request method, the URL, and an API key to access the API functionalities.

  3. The next step is to provide relevant information for alterations. The users have to provide information in the payload, such as the document URL, password, and output file name.

  4. The user has to send the POST request and wait for the response. The status 200 determines the successful request, and the code provides the URL for the resulting file. However, if the request was unsuccessful, alter the code and make corrections.

  5. The users can view the edited document by opening the resultant URL from the code.

  6. Similarly, the users can add more restrictions to the PDF document using the same API functionality and edit the parameters.