How do you protect a pdf in Python?

Question:

I’m looking to password protect a PDF for editing, but without needing the password to view the file.

Is there a way to do this?

I looked at PyPDF2, but I could only find full encryption.

Asked By: MYK

||

Answers:

You can use the permisions flag. For example:

from PyPDF2 import PdfFileReader, PdfFileWriter

pdf_writer = PdfFileWriter()

# CREATE THE PDF HERE

# This is the key line, not the permission_flag parameter
pdf_writer.encrypt(user_pwd='', owner_pwd=PASSWORD, permissions_flag=0b0100)


with open('NewPDF.pdf', "wb") as out_file:
    pdf_writer.write(out_file)
Answered By: MYK

Just be aware the ISO standard describes this optional feature as

ISO 32000-1:

»Once the document has been opened and decrypted successfully, a conforming reader technically has access to the entire contents of the document. There is nothing inherent in PDF encryption that enforces the document permissions specified in the encryption dictionary.«

It is better described as

Specifying access restrictions for a document, such as Printing allowed: None disables the respective function in Acrobat. However, this not necessarily holds true for third-party PDF viewers or other software. It is up to the developer of PDF tools whether or not access permissions are honored. Indeed, several PDF tools are known to ignore permission settings altogether; commercially available* PDF cracking tools can be used to disable all access restrictions. This has nothing to do with cracking the encryption; there is simply no way that a PDF file can make sure it won’t be printed while it still remains viewable.

  • Forget aiding commercial exploitation of your limited file as many online sites will unlock files for "FREE" and thus your restricted file is more likely to become touted freely across the web (great for web coverage but poor for your personal demands.) However they just need to use their browser to save the PDF copy contents. See here the "protected" Adobe Master book, in two common viewers.

enter image description here

Answered By: K J

You could try pikepdf, it could set the pdf permission.

e.g.

from pikepdf import Pdf, Permissions, Encryption, PasswordError
userkey = 'abcd'
ownerkey = 'king'
with Pdf.open(ori_pdf_file) as pdf:
    no_print = Permissions(print_lowres=False, print_highres=False)
    pdf.save(enc_pdf_file, encryption = Encryption(user=userkey, owner=ownerkey, allow=no_print))

It would prevent print while opening the enc_pdf_file with ‘abcd'(userkey).
You could read the documents for more details:
https://pikepdf.readthedocs.io/en/latest/tutorial.html#saving-changes
https://pikepdf.readthedocs.io/en/latest/api/models.html#pikepdf.Permissions

Answered By: Zhxiang Xie

I’m late to answer this and of course, @MYK is right, just worth mentioning in PyPDF2 version 3.0.0 PdfFileReader, PdfFileWriter is deprecated and some functions like getPage() and addPage() have changed.

from PyPDF2 import PdfWriter, PdfReader

out = PdfWriter()
file = PdfReader("yourFile.pdf")

num = len(file.pages)
  
for idx in range(num):
    page = file.pages[idx] 
    out.add_page(page)
  
password = "pass"

out.encrypt(user_pwd='', owner_pwd=password, permissions_flag=0b0100)

with open("yourfile_encrypted.pdf", "wb") as f:
    out.write(f)

enter image description here

and permissions are

enter image description here

Answered By: Talkhak1313
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.