How to Change PDF Password JavaScript in NodeJS

Learn how to use JavaScript to reset a PDF password with the Encrypt PDF API Tool from pdfRest
Share this page

Why Use Encrypt PDF with JavaScript?

The pdfRest Encrypt PDF API Tool empowers you to efficiently manage passwords for your existing PDFs. This tutorial dives into using JavaScript to send API calls and directly change a PDF's password through the Encrypt PDF functionality.

Imagine you have a project containing highly sensitive documents, like marketing strategies or product roadmaps. These documents are already password-protected, but you need to update the original password with a stronger password, restricting access to authorized personnel only.

By leveraging the Encrypt PDF API with JavaScript, you can programmatically update PDF passwords within your existing workflow. This eliminates the need to manually open and re-encrypt documents, saving time and minimizing the risk of errors.

Change PDF Password with JavaScript Code Example

// This request demonstrates how to apply 256-bit AES encryption with a 32-bit key. Encrypted PDFs cannot be viewed without first providing the open password.
var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');

// Create a new form data instance and append the file and parameters to it
var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('current_open_password', 'curr_example_pw');
data.append('new_open_password', 'new_example_pw');
data.append('output', 'pdfrest_encrypted_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/encrypted-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.

Refer to the source of the provided code for more details.

Breaking Down the Code

The provided code snippet demonstrates how to encrypt a PDF file using the pdfRest API. Let's break down the key parts of this code:

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

These lines import the required modules: axios for making HTTP requests, FormData for building form data payloads, and fs for file system operations.

var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('current_open_password', 'curr_example_pw');
data.append('new_open_password', 'new_example_pw');
data.append('output', 'pdfrest_encrypted_pdf');

This section creates a new FormData object and appends the file to be encrypted, the current password, the new password for opening the encrypted PDF, and the desired output name for the encrypted file.

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

Here, we define the configuration for the axios request, setting the HTTP method to POST, the request URL, and the headers including the API key. The maxBodyLength is set to Infinity to allow large files to be sent.

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

Finally, the axios request is sent with the defined configuration. The response is logged to the console, or in the case of an error, the error is logged.

Beyond the Tutorial

In this tutorial, we've demonstrated how to change a password for a PDF using the pdfRest API and JavaScript. By following the steps outlined above, you've learned how to send a multipart API call to secure a PDF document with an updated password. This ensures that the document's contents are protected and can only be accessed by individuals with the correct password.

To explore and demo all of the pdfRest API Tools, visit the API Lab. For a comprehensive understanding of the available endpoints and parameters, refer to the API Reference Guide.

Note: This is an example of a multipart API call. For code samples using JSON payloads, check out the examples at this repository.

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