PyPDF and print restrictions

Chris Curvey ccurvey at gmail.com
Thu Jul 30 09:35:46 EDT 2009


On Jul 27, 4:16 pm, Chris Curvey <ccur... at gmail.com> wrote:
> Has anyone out there been able to enforce print restrictions on a PDF
> document by usingPyPDF? The documentation for "encrypt" states:
>
>  # @param user_pwd The "user password", which allows for opening and
> reading
>  # the PDF file with the restrictions provided.
>
> But there is no parameter for providing a restriction, and I can't
> find a reference to any kind of restriction besides this comment in
> the docs.
>
> Thanks in advance!

I found some (I think old-ish) documentation on encryption and
permissions (of course, I've lost the link).  I think there are
additional permissions that have been implemented since the doc that I
found, but this patch works for me.

Index: C:/Documents and Settings/ccurvey/PyPDF/pyPdf/pdf.py
===================================================================
--- C:/Documents and Settings/ccurvey/PyPDF/pyPdf/pdf.py	(revision 1)
+++ C:/Documents and Settings/ccurvey/PyPDF/pyPdf/pdf.py	(revision 2)
@@ -118,7 +118,10 @@
     # @param use_128bit Boolean argument as to whether to use 128bit
     # encryption.  When false, 40bit encryption will be used.  By
default, this
     # flag is on.
-    def encrypt(self, user_pwd, owner_pwd = None, use_128bit = True):
+    # @param perm_mask bitmask of the permissions that should be
granted.
+    # Defaults to -1, which is "everything permitted"
+    def encrypt(self, user_pwd, owner_pwd = None, use_128bit = True,
+                perm_mask=-1):
         import md5, time, random
         if owner_pwd == None:
             owner_pwd = user_pwd
@@ -130,8 +133,8 @@
             V = 1
             rev = 2
             keylen = 40 / 8
-        # permit everything:
-        P = -1
+
+        P = perm_mask
         O = ByteStringObject(_alg33(owner_pwd, user_pwd, rev,
keylen))
         ID_1 = md5.new(repr(time.time())).digest()
         ID_2 = md5.new(repr(random.random())).digest()
Index: C:/Documents and Settings/ccurvey/PyPDF/pyPdf/__init__.py
===================================================================
--- C:/Documents and Settings/ccurvey/PyPDF/pyPdf/__init__.py
(revision 1)
+++ C:/Documents and Settings/ccurvey/PyPDF/pyPdf/__init__.py
(revision 2)
@@ -1,2 +1,11 @@
 from pdf import PdfFileReader, PdfFileWriter
+
+PERM_NONE = 0
+PERM_PRINT = 2
+PERM_MODIFY = 4
+PERM_COPY_TEXT = 8
+PERM_ANNOTATE = 16
+
+PERM_ALL = PERM_PRINT | PERM_MODIFY | PERM_COPY_TEXT | PERM_ANNOTATE
+
 __all__ = ["pdf"]



More information about the Python-list mailing list