How to Protect a PDF with Passwords, Encryption & Restricted Permissions using Node.js
There’s a lot of maneuvering we can do within PDF documents to restrict how other users interact with them. Along with password protection and encryption, we can additionally choose to allow or disallow printing, content extraction, form filling, editing, annotations and more.
Using the below code, you can take advantage of a free API solution that allows you to protect AND set restrictions on any PDF document in one comprehensive request. Minimal code makes it extremely easy to use, and you’ll just need a free-tier API key to make up to 800 API calls per month with no commitments.
First, you’ll need to run this command to install the SDK:
npm install cloudmersive-convert-api-client --save
Alternatively, you can add this snippet to your package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
With that out of the way, you can copy & paste the ready-to-run Node.js code examples below to structure your API call:
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 ownerPassword = "ownerPassword_example"; // String | Password of a owner (creator/editor) of the PDF file (required)
var userPassword = "userPassword_example"; // String | Password of a user (reader) of the PDF file (optional)
var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.
var opts = {
'encryptionKeyLength': "encryptionKeyLength_example", // String | Possible values are \"128\" (128-bit RC4 encryption) and \"256\" (256-bit AES encryption). Default is 256.
'allowPrinting': true, // Boolean | Set to false to disable printing through DRM. Default is true.
'allowDocumentAssembly': true, // Boolean | Set to false to disable document assembly through DRM. Default is true.
'allowContentExtraction': true, // Boolean | Set to false to disable copying/extracting content out of the PDF through DRM. Default is true.
'allowFormFilling': true, // Boolean | Set to false to disable filling out form fields in the PDF through DRM. Default is true.
'allowEditing': true, // Boolean | Set to false to disable editing in the PDF through DRM (making the PDF read-only). Default is true.
'allowAnnotations': true, // Boolean | Set to false to disable annotations and editing of annotations in the PDF through DRM. Default is true.
'allowDegradedPrinting': true // Boolean | Set to false to disable degraded printing of the PDF through DRM. Default is true.
};
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editPdfSetPermissions(ownerPassword, userPassword, inputFile, opts, callback);
Now you can take your document administration workflows to the next level with ease!