How to Restrict PDF with JavaScript in NodeJS

Learn how to apply file restrictions to PDF documents with the pdfRest Restrict PDF API Tool and JavaScript
Share this page

Why Use Restrict PDF with JavaScript?

The pdfRest Restrict PDF API Tool allows developers to apply security restrictions to PDF documents programmatically. This tutorial will guide you through the process of sending an API call to Restrict PDF using JavaScript.

Imagine you have an important document that you want to share with your team, but you need to ensure that it can't be edited or printed. Using the Restrict PDF tool, you can set permissions to prevent unauthorized actions on your PDF, keeping your document secure while distributing it.

Restrict PDF with JavaScript Code Example

// This request demonstrates how to apply security restrictions to a PDF using a permissions 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('new_permissions_password', 'new_example_pw');
data.append('restrictions[]', 'print_low'); 
data.append('restrictions[]', 'accessibility_off');
data.append('restrictions[]', 'edit_content');
data.append('output', 'pdfrest_restricted_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/restricted-pdf',
  headers: { 
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', // Replace with your API key
    ...data.getHeaders() // Set headers with form data headers
  },
  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

Reference to the code: pdf-rest-api-samples

Breaking Down the Code

The code above demonstrates how to use the Restrict PDF API with JavaScript. Let's break down the key parts:

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

We include 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('new_permissions_password', 'new_example_pw');
data.append('restrictions[]', 'print_low');
data.append('restrictions[]', 'accessibility_off');
data.append('restrictions[]', 'edit_content');
data.append('output', 'pdfrest_restricted_pdf');

This section creates a new FormData object and appends the PDF file and parameters to it. The parameters include the new permissions password and the restrictions you want to apply to the PDF.

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

The config object defines the request method, 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); 
});

Finally, we send the request using axios and handle the response or error accordingly.

Beyond the Tutorial

By following the steps above, you've learned how to use JavaScript to call the pdfRest Restrict PDF API to apply security restrictions to a PDF document. This can be particularly useful for protecting sensitive information or enforcing document policies. To explore and demo all of the pdfRest API Tools, visit the API Lab. For more detailed information, refer to the API Reference documentation.

Note: This is an example of a multipart API call. For code samples using JSON payloads, visit pdf-rest-api-samples.

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