Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Confusion between User Access Permissions & Document Security Restrictions #2581

Closed
eabase opened this issue Apr 5, 2024 · 1 comment
Closed

Comments

@eabase
Copy link

eabase commented Apr 5, 2024

There seem to be some confusion between User Access Permissions and Document [Security] Restrictions, when handling permissions. (Or at least I am confused, because it looks like you are mixing up the two.)

  • User Access Permissions is used for setting permissions on encrypted files requiring a password.

  • Document [Security] Restrictions can be set on any PDF file (without encryption)

  • There is also an ambiguous DeprecationWarning that tell you to use another thing, and then doesn't work.

Looking at some PDF document you can have the Document [Security] Restrictions as shown in Adobe Acrobat, without any Encryption.

Here's how my PDF looks.
(Sorry, I cannot share it as it belongs to a company.)

Document_Properties2


So if I have a non-encrypted PDF with the following restrictions:

EXTRACT_TEXT_AND_GRAPHICS 
ASSEMBLE_DOC

(512 and 1024, respectively)

The question is:

  1. How do I read these from the PDF?
  2. How do I change them in the same PDF?

Environment

Which environment were you using when you encountered the problem?

# python -m platform
Windows-10-10.0.22000-SP0

# python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==4.1.0, crypt_provider=('cryptography', '42.0.5'), PIL=10.3.0

Code + PDF

This is a REPL example that shows the issue:

import pypdf
from pypdf import PdfReader, PdfWriter
from pypdf.constants import UserAccessPermissions

IFILE = 'some.pdf'
reader = PdfReader(IFILE)

reader.decode_permissions(UserAccessPermissions.all())
# ...
# <stdin>:1: DeprecationWarning: decode_permissions is deprecated and will be removed in pypdf 5.0.0. Use user_access_permissions instead.

reader.user_access_permissions(UserAccessPermissions.all())
# TypeError: 'NoneType' object is not callable
@stefan6419846
Copy link
Collaborator

It seems like you are mixing up something: A PDF file can have a user password and an owner password. The user password is required each time you open the file. An owner password is used to restrict the permissions a regular user (without specifying the owner password) should be allowed to do.

You are correct that decode_permissions has been deprecated as this rather is a property on the document. Nevertheless, you are using both functionality wrong (see #2389 (comment) as well): You either want

  • reader.decode_permissions(reader._encryption.P) (deprecated) or
  • reader.user_access_permissions (flags instance) or reader.user_access_permissions.to_dict() (dictionary similar to the one from decode_permissions().

Alternatively, you might pass any set of permission flag value into UserAccessPermissions() to get a verbose representation of it.

To change the permissions, use PdfWriter.encrypt() with the appropriate parameters.

@py-pdf py-pdf locked and limited conversation to collaborators Apr 5, 2024
@stefan6419846 stefan6419846 converted this issue into discussion #2582 Apr 5, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants