colemana / PyPDF2

A utility to read and write pdfs with Python. Superseded: see https://github.com/knowah/PyPDF2
Other
80 stars 19 forks source link

Cannot encrypt a PDF file #3

Open mayito opened 11 years ago

mayito commented 11 years ago

Hi,

I'd like to create a read only PDF. I've written and executed the following code :

from PyPDF2.pdf import PdfFileReader, PdfFileWriter

# create an empty pdf writer
writer = PdfFileWriter()
writer.encrypt("user_pwd", "owner_pwd")
output = open(r"C:\Temp\TestFileLock.pdf", "wb")
writer.write(output)

An error occured in the encrypt function and generate the following traceback :

File "C:\Program Files (x86)\Python27\lib\site-packages\PyPDF2\pdf.py", line 229, in encrypt
    U, key = _alg35(user_pwd, rev, keylen, O, P, ID_1, False)
  File "C:\Program Files (x86)\Python27\lib\site-packages\PyPDF2\pdf.py", line 2140, in _alg35
    key = _alg32(password, rev, keylen, owner_entry, p_entry, id1_entry)
  File "C:\Program Files (x86)\Python27\lib\site-packages\PyPDF2\pdf.py", line 2051, in _alg32
    m.update(id1_entry.original_bytes)
AttributeError: 'str' object has no attribute 'original_bytes'