Decrypt PDF in Node.js
Contents
[
Hide
]
Decrypt PDF File using Owner Password
Recently, more and more users exchange encrypted documents in order not to become victims of Internet fraud and protect their documents. In this regard, it becomes necessary to access the encrypted PDF file, since such access can only be obtained by an authorized user. Also, people are looking for various solutions to decrypt PDF files.
In case you want to decrypt PDF file, you can use AsposePdfDecrypt function. If you want to decrypt PDF file try the next code snippet:
CommonJS:
- Call
require
and importasposepdfnodejs
module asAsposePdf
variable. - Specify the name of the PDF file that will change the decrypted.
- Call
AsposePdf
as Promise and perform the operation for decrypting file. Receive the object if successful. - Call the AsposePdfDecrypt function.
- Decrypt PDF file with password is “owner”.
- Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultDecrypt.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.
const AsposePdf = require('asposepdfnodejs');
const pdf_encrypt_file = 'ResultEncrypt.pdf';
AsposePdf().then(AsposePdfModule => {
/*Decrypt a PDF-file with password is "owner" and save the "ResultDecrypt.pdf"*/
const json = AsposePdfModule.AsposePdfDecrypt(pdf_encrypt_file, "owner", "ResultDecrypt.pdf");
console.log("AsposePdfDecrypt => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
});
ECMAScript/ES6:
- Import the
asposepdfnodejs
module. - Specify the name of the PDF file that will change the decrypted.
- Initialize the AsposePdf module. Receive the object if successful.
- Call the AsposePdfDecrypt function.
- Decrypt PDF file with password is “owner”.
- Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultDecrypt.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.
import AsposePdf from 'asposepdfnodejs';
const AsposePdfModule = await AsposePdf();
const pdf_encrypt_file = 'ResultEncrypt.pdf';
/*Decrypt a PDF-file with password is "owner" and save the "ResultDecrypt.pdf"*/
const json = AsposePdfModule.AsposePdfDecrypt(pdf_encrypt_file, "owner", "ResultDecrypt.pdf");
console.log("AsposePdfDecrypt => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);