Search code examples
pythonpdfencryptionpdfminer

PDF Miner PDFEncryptionError


I'm trying to extract text from pdf-files and later try to identify the references. I'm using pdfminer 20140328. With unencrypted files its running well, but I got now a file where i get:

File "C:\Tools\Python27\lib\site-packages\pdfminer\pdfdocument.py", line 348, in _initialize_password

raise PDFEncryptionError('Unknown algorithm: param=%r' % param)

pdfminer.pdfdocument.PDFEncryptionError: Unknown algorithm: param={'CF': {'StdCF': {'Length': 16, 'CFM': /AESV2, 'AuthEvent': /DocOpen}}, 'O': '}\xe2>\xf1\xf6\xc6\x8f\xab\x1f"O\x9bfc\xcd\x15\xe09~2\xc9\\x87\x03\xaf\x17f>\x13\t^K\x99', 'Filter': /Standard, 'P': -1548, 'Length': 128, 'R': 4, 'U': 'Kk>\x14\xf7\xac\xe6\x97\xb35\xaby!\x04|\x18(\xbfN^Nu\x8aAd\x00NV\xff\xfa\x01\x08', 'V': 4, 'StmF': /StdCF, 'StrF': /StdCF}

I checked with pdfinfo, that this file seemed to be AES encrypted, but i can open it without any problems. So i have two questions:

  • at first: how is it possible that a document is encrypted but i can open it without a password?

  • and secondly: how do i make PDFMiner read that file properly? Somewhere i read to install pycrypto to get additional algorithms but it doesnt fixed my problem.

Many thanks.


Solution

  • I had the same problem with some documents. It looks like the document is encrypted, but the password is blank. That's why we can easily open it without a password.

    I ended up fixing the problem with Ubuntu's qpdf utility. It can decrypt the file if you provide a password (blank in my case). I implemented a shell command in Python script that would decrypt the document with an empty password:

    from subprocess import call
    call('qpdf --password=%s --decrypt %s %s' %('', pdf_filename, pdf_filename_decr), shell=True)
    

    where

    `pdf_filename` - filename of encrypted pdf,
    `pdf_filename_decr` - filename of a new decrypted copy.
    

    pdfminer should extract the text now.