How to determine if PDF is encrypted javascript client side [closed]
Asked Answered
G

2

11

Need to determine if if pdf uploaded by user is password protected without using external libs. So far got this POC.

Any one know cases when this might not work?

<input type='file' onchange='openFile(event)'><br>
<script>
    var openFile = function (event) {

        var input = event.target;

        var reader = new FileReader();
        reader.onload = function (event) {
            console.clear();
            var contents = event.target.result;
            if (contents.indexOf('/Encrypt') !== -1) {
                console.log("Is encrypted");
            } else {
                console.log("Not encrypted");
            }
            console.log("File contents: " + contents);
        };

        reader.onerror = function (event) {
            console.error("File could not be read! Code "  +event.target.error.code);
        };

        reader.readAsText(input.files[0]);

    };
</script>
Gillies answered 29/8, 2018 at 15:38 Comment(2)
Did it work as expected?Chet
Yes still working as expected.Gillies
G
3

You can use the below code to find whether the PDF is encrypted or not

const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = function () {

var files = new Blob([reader.result], {type: 'application/pdf'});
files.text().then(x=> {
    console.log("isEncrypted", x.includes("Encrypt"))
    console.log("isEncrypted", x.substring(x.lastIndexOf("<<"), x.lastIndexOf(">>")).includes("/Encrypt"));
    console.log(file.name);
});
Glorianna answered 1/4, 2022 at 11:16 Comment(0)
J
0

The provided code snippet is an HTML file that uses the PDF.js library to check if a PDF file is password protected.

<!DOCTYPE html>
<html>
<head>
  <title>Check if PDF is Password Protected</title>
  <!-- Include PDF.js -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.11.338/pdf.min.js"></script>
</head>
<body>
  <input type="file" id="fileInput" accept=".pdf">
  <div id="output"></div>

  <script>
    document.getElementById('fileInput').addEventListener('change', function(event) {
      var file = event.target.files[0];
      var fileReader = new FileReader();

      fileReader.onload = function() {
        var typedarray = new Uint8Array(this.result);

        // Load the PDF document
        pdfjsLib.getDocument(typedarray).promise.then(function(pdf) {
          // PDF loaded successfully
          document.getElementById('output').innerText = 'This PDF is not password protected.';
        }).catch(function(error) {
          if (error.name === 'PasswordException') {
            document.getElementById('output').innerText = 'This PDF is password protected.';
          } else {
            document.getElementById('output').innerText = 'Error loading PDF: ' + error.message;
          }
        });
      };

      fileReader.readAsArrayBuffer(file);
    });
  </script>
</body>
</html>

Jemina answered 31/5 at 10:29 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Subvention

© 2022 - 2024 — McMap. All rights reserved.