Skip to content

Commit

Permalink
ENH: Allow setting permission flags when encrypting (py-pdf#803)
Browse files Browse the repository at this point in the history
Closes py-pdf#161
Closes py-pdf#308
  • Loading branch information
MartinThoma authored and VictorCarlquist committed Apr 29, 2022
1 parent 5ade870 commit 9c17796
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 9c17796

Please sign in to comment.