Password Protect PDF in Node.js SDK
API for encrypting PDF documents using Cloud Node.js SDK
Get StartedHow to encrypt PDF via Node.js SDK
To encrypt PDF document, we’ll use Aspose.PDF Cloud Node.js SDK. This Cloud SDK assists Node.js programmers in developing cloud-based PDF creator, annotator, editor, and converter apps using Node.js programming language via Aspose.PDF REST API. Simply create an account at Aspose for Cloud and get your application information. Once you have the App SID & key, you are ready to give the Aspose.PDF Cloud Node.js SDK.
Package Manager Console Command
npm install asposepdfcloud --save
Steps to encrypt PDF via Cloud Node.js
Aspose.PDF Cloud developers can easily load & encrypt PDF in just a few lines of code.
- Read the local PDF file.
- Upload the PDF file to the Aspose.PDF Cloud storage.
- Encrypt it in the cloud using strong encryption and passwords.
- Download the result if needed it.
Encrypt PDF using Node.js
import credentials from "../../../Credentials/credentials.json" with { type: "json" }; // json-file in this format: { "id": "*****", "key": "*******" }
import fs from 'node:fs/promises';
import path from 'node:path';
import { PdfApi } from "../../src/api/api.js";
import { CryptoAlgorithm } from "../../src/models/cryptoAlgorithm.js";
const configParams = {
LOCAL_FOLDER: "C:\\Samples\\",
PDF_DOCUMENT_NAME: "sample.pdf",
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
ENCRYPT_ALGORITHM: CryptoAlgorithm.AESx256,
USER_PASSWORD: "User-Password",
OWNER_PASSWORD: "Owner-Password",
};
const pdfApi = new PdfApi(credentials.id, credentials.key);
const pdfEncoder = {
async uploadDocument () {
const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
const pdfFileData = await fs.readFile(fileNamePath);
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData)
.then(() => console.log("File: '" + configParams.PDF_DOCUMENT_NAME +"' successfully uploaded."));
},
async downloadResult() {
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
await fs.writeFile(filePath, changedPdfData.body);
console.log("Downloaded: " + filePath);
},
async encrypt_document() {
const user_password_encoded = btoa(configParams.USER_PASSWORD)
const owner_password_encoded = btoa(configParams.OWNER_PASSWORD)
const response = await pdfApi.postEncryptDocumentInStorage(configParams.PDF_DOCUMENT_NAME, user_password_encoded, owner_password_encoded, configParams.ENCRYPT_ALGORITHM);
if (response.body.code == 200)
console.log("encrypt_document(): Document #'" + configParams.PDF_DOCUMENT_NAME + "' successfully encrypted.")
else
throw new Error("encrypt_document(): Failed to encrypt document #'" + configParams.PDF_DOCUMENT_NAME + "'. Response code: {" + response.code + "}")
},
}
async function main() {
try {
await pdfEncoder.uploadDocument();
await pdfEncoder.encrypt_document();
await pdfEncoder.downloadResult();
} catch (error) {
console.error("Error:", error.message);
}
}
Encrypt PDF
Encrypting a PDF document is a simple yet powerful way to protect sensitive content, ensure privacy, and maintain control over how the document is used. Whether you’re sharing business contracts, financial reports, or personal records, encryption helps safeguard your information from unauthorized access, tampering, or misuse.
By applying encryption and setting appropriate permissions, you enhance the document’s security, ensure compliance with industry regulations, and build trust with your audience. In today’s digital world, PDF encryption is not just a feature—it’s a necessity for responsible data handling. Encrypt PDF documents with Aspose.PDF Cloud Node.js SDK.
With our Node.js library you can:
- Add PDF document’s header & footer in text or image format.
- Add tables & stamps (text or image) to PDF documents.
- Append multiple PDF documents to an existing file.
- Work with PDF attachments, annotations, & form fields.
- Apply encryption or decryption to PDF documents & set a password.
- Delete all stamps & tables from a page or entire PDF document.
- Delete a specific stamp or table from the PDF document by its ID.
- Replace single or multiple instances of text on a PDF page or from the entire document.
- Extensive support for converting PDF documents to various other file formats.
- Extract various elements of PDF files & make PDF documents optimized.
- You can try out our free Apps to test the functionality online.
- Learning Resources
- Documentation
- Source Code
- API References
- Product Support
- Free Support
- Paid Support
- Blog
- Why Aspose.PDF Cloud for Node.js?
- Customers List
- Security