How to Encrypt PDF Files with JavaScript in NodeJS
Why Encrypt PDF with JavaScript?
The pdfRest Encrypt PDF API Tool is a powerful resource for developers looking to secure their PDF documents. By encrypting a PDF, you can restrict access to the document's content, ensuring that only those with the correct password can view it.
This tutorial will guide you through the process of sending an API call to encrypt a PDF using JavaScript. Imagine you have a sensitive document, such as a financial report or a legal contract, that you need to share electronically. To protect this document from unauthorized access, you would use the Encrypt PDF API to secure it with a password.
Encrypt PDF 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('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); });
Source: GitHub
Breaking Down the JavaScript Code
The provided code uses Node.js modules to send a multipart/form-data request to the pdfRest API to encrypt a PDF file. Here's a breakdown of the key parts:
var axios = require('axios'); var FormData = require('form-data'); var fs = require('fs');
This initializes the required modules: axios
for making HTTP requests, FormData
to create a form-data payload, and fs
to interact with the file system.
var data = new FormData(); data.append('file', fs.createReadStream('/path/to/file')); data.append('new_open_password', 'new_example_pw'); data.append('output', 'pdfrest_encrypted_pdf');
Here, a new FormData
instance is created, and the file to be encrypted is appended along with the new password and output file name.
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 };
The config
object contains the request configuration, including the API endpoint, headers (with your API key), and the data payload.
axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); });
The axios
library sends the request and processes the response or error. The response will contain information about the encrypted PDF, such as its URL or resource ID.
Beyond the Tutorial
By following the steps above, you've learned how to encrypt a PDF using JavaScript and the pdfRest API. Now you can secure your PDF documents and control who can view them. To explore more capabilities and try out different API tools, visit the pdfRest API Lab at API Lab. For detailed information on the API, refer to the API Reference documentation at Cloud API Reference Guide.
Note: This is an example of a multipart API call. For code samples using JSON payloads, visit GitHub.