How to Remove PDF Restrictions with JavaScript in NodeJS

Learn how to remove restrictions from PDFs to enable printing, editing content and form fields, copying text, and more with the Restrict PDF API Tool.
Share this page

Why Remove PDF Restrictions with JavaScript?

The pdfRest Restrict PDF API Tool is a powerful resource for developers looking to automate the process of modifying security settings on PDF documents. This tutorial will demonstrate how to send an API call to the /unrestricted-pdf endpoint using JavaScript, which can be particularly useful when integrating this functionality into web applications or server-side scripts. The pdfRest API offers a convenient way to manipulate PDFs programmatically, saving time and effort compared to manual processing.

A real-world example of why a user might use the Restrict PDF API could be a business that needs to distribute sensitive documents to its employees or clients. The documents may be password-protected for confidentiality, but there could be a need to remove or alter these restrictions for certain users or under specific circumstances. Automating this process with the endspoints available in the Restrict PDF API ensures that the documents are handled securely and efficiently, without the need for manual intervention.

Removing PDF Restrictions with JavaScript Code Example

/**
 * Use this request to remove security restrictions from a password protected PDF.
 * NOTE: By default, removing the permissions password will also remove encryption by open password. To keep an open password on a document, include the current_open_password form-data field set to the correct open password.
 */
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('current_permissions_password', 'current_example_pw');
data.append('output', 'pdfrest_unrestricted_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/unrestricted-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: pdfRest API Samples on GitHub

Breaking Down the Code

The code example above demonstrates how to use JavaScript to call the pdfRest API and remove security restrictions from a password-protected PDF. The process involves several steps:

var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');

This snippet imports the necessary modules: axios for making HTTP requests, FormData for constructing form data payloads, and fs for file system operations.

var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('current_permissions_password', 'current_example_pw');
data.append('output', 'pdfrest_unrestricted_pdf');

Here, a new FormData instance is created, and the PDF file along with parameters are appended to it. The current_permissions_password is the password currently protecting the PDF, and output is the desired name for the output file.

var config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://api.pdfrest.com/unrestricted-pdf',
  headers: {
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    ...data.getHeaders()
  },
  data: data
};

The config object defines the configuration for the axios request, including the API endpoint URL, headers (which must include your API key), and the data payload.

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error); 
});

This snippet sends the request using axios and handles the response or error. The response will contain information about the processed PDF file.

Beyond the Tutorial

This tutorial has walked you through how to use JavaScript to make an API call to the pdfRest Restrict PDF endpoint. By following these steps, you can programmatically remove restrictions from a password-protected PDF. To explore and demo all of the pdfRest API Tools, you can visit the API Lab. For more detailed information about the API, refer to the API Reference Guide.

Please note that this is an example of a multipart API call. Code samples using JSON payloads for different pdfRest API endpoints can be found at pdfRest API Samples on GitHub.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us