Decrypt PDF in Node.js

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:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file that will change the decrypted.
  3. Call AsposePdf as Promise and perform the operation for decrypting file. Receive the object if successful.
  4. Call the AsposePdfDecrypt function.
  5. Decrypt PDF file with password is “owner”.
  6. 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:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file that will change the decrypted.
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the AsposePdfDecrypt function.
  5. Decrypt PDF file with password is “owner”.
  6. 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);