'pdf.js get password-protected file content

I want to retrieve the content of a password-protected file as a File object using PDF.js library. I tried to get the raw data from the promise and convert it to a File. However the file is still encrypted.

Here is a sample of code:

  const loadingTask = pdfjsLib.getDocument(file);

  loadingTask.onPassword = (callback, reason) => {
      callback(password);
  };


  loadingTask.promise.then((pdfDocument) => {
      //Here I tried to retrieve data from `pdfDocument` but it is still encrypted
      
      const data = await pdfDocument.getData(); //The data is still encrypted
      const blob = new Blob([data]);
      const pdfFile = new File([blob], 'name', { type: 'application/pdf' });
  });

Is there a way with PDF.js library to get the decrypted PDF as a File object ?

Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source