Skip to content

Commit

Permalink
ENH: Allow setting permission flags when encrypting
Browse files Browse the repository at this point in the history
Closes #161
Closes #308
  • Loading branch information
MartinThoma committed Apr 23, 2022
1 parent ffb2084 commit 21f2a7e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions PyPDF2/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def cloneDocumentFromReader(self, reader, after_page_append=None):
self.cloneReaderDocumentRoot(reader)
self.appendPagesFromReader(reader, after_page_append)

def encrypt(self, user_pwd, owner_pwd = None, use_128bit = True):
def encrypt(self, user_pwd, owner_pwd = None, use_128bit = True, permissions_flag=-1):
"""
Encrypt this PDF file with the PDF Standard encryption handler.
Expand All @@ -441,6 +441,13 @@ def encrypt(self, user_pwd, owner_pwd = None, use_128bit = True):
:param bool use_128bit: flag as to whether to use 128bit
encryption. When false, 40bit encryption will be used. By default,
this flag is on.
:param unsigned int permissions_flag: permissions as described in
TABLE 3.20 of the PDF 1.7 specification. A bit value of 1 means the
permission is grantend. Hence an integer value of -1 will set all
flags.
Bit position 3 is for printing, 4 is for modifying content, 5 and 6
control annotations, 9 for form fields, 10 for extraction of
text and graphics.
"""
import random
import time
Expand All @@ -454,8 +461,7 @@ def encrypt(self, user_pwd, owner_pwd = None, use_128bit = True):
V = 1
rev = 2
keylen = int(40 / 8)
# permit everything:
P = -1
P = permissions_flag
O = ByteStringObject(_alg33(owner_pwd, user_pwd, rev, keylen))
ID_1 = ByteStringObject(md5(b_(repr(time.time()))).digest())
ID_2 = ByteStringObject(md5(b_(repr(random.random()))).digest())
Expand Down

0 comments on commit 21f2a7e

Please sign in to comment.