How to Decrypt and Remove Password Protection from a PDF using Node.js
Applications that handle and process a large volume of PDF documents can certainly benefit from a wide range of quality-of-life improvements.
Using the below code, we can freely add an API to our application that allows us to enter creator-supplied passwords and automatically remove encryption measures in a single in-memory request.
We can easily implement this API in two quick steps.
Let’s first install the SDK by running the below command:
npm install cloudmersive-convert-api-client --save
Or we can alternatively include this snippet in our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Finally, let’s copy the ready-to-run Node.js code examples below into our file and supply a free-tier API key to authorize our requests:
var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveConvertApiClient.EditPdfApi();
var password = "password_example"; // String | Valid password for the PDF file
var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editPdfDecrypt(password, inputFile, callback);
Yep, that’s all there is to it — no more code required. Simple and easy!