Search code examples
node.jspdfhummus.js

Check if PDF is password protected HummusJS, NodeJS


Does anybody perhaps know, how I can check if a PDF is password protected by using HummusJS in NodeJS? This check needs to be done when modifying a PDF.

I am struggling to find a solution online and in the documentation.

Regards


Solution

  • Ok, so I got this working, thanks to HummusJS's author.

    A parser object will be able to check if a PDF is encrypted or not. To create a PDFReader object (parser object), you can use:

    var pdfReader = hummus.createReader('./TestMaterials/XObjectContent.PDF');
    

    But when trying to modify the PDF, you will get an exception that the file is busy (EBUSY).

    The PDFWriter object, can actually return a PDFReader object that it is using:

    var pdfReader = pdfWriter.getModifiedFileParser();
    

    By using this pdfReader object, use the following to detect if it is password protected:

    pdfReader.isEncrypted()
    

    Ciao!