RichieK
RichieK

Reputation: 554

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:

Many thanks.

Upvotes: 12

Views: 6820

Answers (2)

Emilia Apostolova
Emilia Apostolova

Reputation: 1787

I got the exact same error, encrypted PDF with encryption option -V 4. Turns out this is actually supported in the latest pdfminer.

If you used pip to install pdfminer, you are running a release from 2014 (20140328), which does not support it. So instead:

git clone https://github.com/euske/pdfminer.git
cd pdfminer
python setup.py install

Make sure you have pycrypto installed:

pip install pycrypto

and that you can import AES:

from Crypto.Cipher import AES

Upvotes: 0

Chingiz K.
Chingiz K.

Reputation: 216

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.

Upvotes: 20

Related Questions