Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Allow setting permission flags when encrypting #803

Merged
merged 1 commit into from
Apr 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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