Encrypt pdf file so copy content or edit is not allowed

1.6k views Asked by At

How can I encrypt the document so it is not allowed to edit text or should not allow copying content from pdf files?

I tried setting different user and admin passwords but still, I was able to edit the text in pdf editor.

import pikepdf
from pikepdf import Pdf
pdf = Pdf.open("document.pdf")
pdf.save('output_filename.pdf', encryption=pikepdf.Encryption(owner="password", user="123", R=6))
pdf.close()

Basically if there is way without password if I can encrypt document to edit than that will well and good. Thanks in Advance.

3

There are 3 answers

4
BroserBros On

You need to PyPDF2 (pip install PyPDF2) for using this script. And try this:

import PyPDF2

#pdf_in_file = open("document.pdf",'rb')
pdf_in_file = open("document.pdf",'rb')

inputpdf = PyPDF2.PdfFileReader(pdf_in_file)
pages_no = inputpdf.numPages
output = PyPDF2.PdfFileWriter()

for i in range(pages_no):
    inputpdf = PyPDF2.PdfFileReader(pdf_in_file)
    
    output.addPage(inputpdf.getPage(i))
    output.encrypt('password_u_want')

    #with open("simple_password_protected.pdf", "wb") as outputStream:
    with open("output_filename.pdf", "wb") as outputStream:
        output.write(outputStream)

pdf_in_file.close()

Here details for PyPDF2: https://pythonhosted.org/PyPDF2/PdfFileWriter.html

0
K J On

Basically you CAN set permissions for those people who do NOT WANT to copy like the blind, but you CANT set permissions for those that WANT to copy visible contents.

You can set the ADOBE internal permissions for restricting copy working in ACROBAT and any ADOBE conforming reader, like Microsoft Edge or similar.

HOWEVER those ADOBE preferences do not need to be respected by any NON-ADOBE PDF editor such as Mozilla / Firefox Browser etc.

From the Standard you signed up to for Open Source permission to USE or WRITE PDFs (Amusingly restricted itself) so I need to copy from Firefox's Browsing Editor.

enter image description here

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. Conforming readers shall respect the intent of the document creator by restricting user access to an encrypted PDF file according to the permissions contained in the file.

see
https://security.stackexchange.com/questions/227100/pdf-user-password-always-give-access-to-the-owner-password-even-when-encrypted
How does admin password security work in pdfs?
TCPDF SetProtection method is not working as expected
Is it possible to extract text from PDF, whose "Page Extraction" is not allowed?

Bottom line is Ask all your viewers to buy Acrobat or other Adobe aligned applications..

0
Kfear On

Some functions in @BroserBros are deprecated here are the fixes needed:

import PyPDF2

# pdf_in_file = open("document.pdf",'rb')
pdf_in_file = open("document.pdf", 'rb')

input_pdf = PyPDF2.PdfReader(pdf_in_file)
pages_no = len(input_pdf.pages)
output = PyPDF2.PdfWriter()

for i in range(pages_no):
    input_pdf = PyPDF2.PdfReader(pdf_in_file)

    output.add_page(input_pdf.pages[i])
    output.encrypt("password_u_want")

    # with open("simple_password_protected.pdf", "wb") as outputStream:
    with open("output_protected_file.pdf", "wb") as outputStream:
    output.write(outputStream)

pdf_in_file.close()