Added encrypt option to DocTemplate.
authorjonas
Wed, 07 Jan 2009 15:03:42 +0000
changeset 3040 cb548ffb442d
parent 3039 42ecfd4a9344
child 3041 92ad2aed326b
Added encrypt option to DocTemplate.
src/reportlab/pdfgen/canvas.py
src/reportlab/platypus/doctemplate.py
tests/test_encrypt.py
--- a/src/reportlab/pdfgen/canvas.py	Tue Jan 06 13:12:45 2009 +0000
+++ b/src/reportlab/pdfgen/canvas.py	Wed Jan 07 15:03:42 2009 +0000
@@ -181,6 +181,9 @@
         self._make_preamble()
         self.state_stack = []
 
+        self.setencrypt(encrypt)
+
+    def setencrypt(self, encrypt):
         if encrypt:
             from reportlab.lib import pdfencrypt
             if isinstance(encrypt, basestring):
@@ -190,6 +193,11 @@
             elif not isinstance(encrypt, pdfencrypt.StandardEncryption):
                 raise TypeError('Expected string or instance of reportla.lib.pdfencrypt.StandardEncryption as encrypt parameter but got %r' % encrypt)
             self._doc.encrypt = encrypt
+        else:
+            try:
+                del self._doc.encrypt
+            except AttributeError:
+                pass
 
     def init_graphics_state(self):
         #initial graphics state, never modify any of these in place
--- a/src/reportlab/platypus/doctemplate.py	Tue Jan 06 13:12:45 2009 +0000
+++ b/src/reportlab/platypus/doctemplate.py	Wed Jan 07 15:03:42 2009 +0000
@@ -340,7 +340,8 @@
                     'pageCompression':None,
                     '_pageBreakQuick':1,
                     'rotation':0,
-                    '_debug':0}
+                    '_debug':0,
+                    'encrypt': None}
     _invalidInitArgs = ()
     _firstPageTemplateIndex = 0
 
@@ -716,6 +717,8 @@
                                 pagesize=self.pagesize,
                                 invariant=self.invariant,
                                 pageCompression=self.pageCompression)
+ 
+        getattr(self.canv,'setencrypt',lambda x: None)(self.encrypt)
 
         self.canv.setAuthor(self.author)
         self.canv.setTitle(self.title)
--- a/tests/test_encrypt.py	Tue Jan 06 13:12:45 2009 +0000
+++ b/tests/test_encrypt.py	Wed Jan 07 15:03:42 2009 +0000
@@ -2,7 +2,7 @@
 #Copyright ReportLab Europe Ltd. 2000-2004
 #see license.txt for license details
 __version__='''$Id$'''
-__doc__="""Testing to encrypt a very minimal pdf.
+__doc__="""Testing to encrypt a very minimal pdf using a Canvas and a DocTemplate.
 TODO: Automatiocally test that this pdf is really encrypted.
 """
 
@@ -11,32 +11,39 @@
 setOutDir(__name__)
 from reportlab.pdfgen.canvas import Canvas
 from reportlab.lib import pdfencrypt
+from reportlab.lib.styles import ParagraphStyle
+from reportlab.platypus import SimpleDocTemplate, Paragraph
 
 class EncryptTestCase(unittest.TestCase):
-    "Test generating an encrypted pdf by setting a user password."
 
-    def test(self):
-        c = Canvas(outputfile('test_encrypt.pdf'), encrypt='User')
-        c.setAuthor('\xe3\x83\x9b\xe3\x83\x86\xe3\x83\xab\xe3\x83\xbbe\xe3\x83\x91\xe3\x83\xb3\xe3\x83\x95\xe3\x83\xac\xe3\x83\x83\xe3\x83\x88')
+    def test_canvas(self):
+        "Test generating an encrypted pdf by setting a user password on the Canvas."
+        c = Canvas(outputfile('test_encrypt_canvas.pdf'), encrypt='User')
+        c.setAuthor('Anonymous')
         c.setFont('Helvetica-Bold', 36)
         c.drawString(100,700, 'Top secret')
         c.save()
 
-class PreciseEncryptTestCase(unittest.TestCase):
-    "Test generating an encrypted pdf by passing a StandardEncryption object to the Canvas."
-
-    def test(self):
+    def test_standardencryption(self):
+        "Test generating an encrypted pdf by passing a StandardEncryption object to the Canvas."
         encrypt = pdfencrypt.StandardEncryption(userPassword='User', ownerPassword='Owner')
         encrypt.setAllPermissions(0)
         encrypt.canPrint = 1
-        c = Canvas(outputfile('test_encrypt2.pdf'), encrypt=encrypt)
-        c.setAuthor('\xe3\x83\x9b\xe3\x83\x86\xe3\x83\xab\xe3\x83\xbbe\xe3\x83\x91\xe3\x83\xb3\xe3\x83\x95\xe3\x83\xac\xe3\x83\x83\xe3\x83\x88')
+        c = Canvas(outputfile('test_encrypt_canvas2.pdf'), encrypt=encrypt)
+        c.setAuthor('Anonymous')
         c.setFont('Helvetica-Bold', 36)
         c.drawString(100,700, 'Top secret')
         c.save()
 
+    def test_doctemplate(self):
+        "Test generating an encrypted pdf by setting a user password on the DocTemplate."
+        header = ParagraphStyle(name='Heading', fontSize=14, keepWithNext=1)
+        story = [Paragraph("Top secret", header)]
+        doc = SimpleDocTemplate(outputfile('test_encrypt_doctemplate.pdf'), encrypt='User')
+        doc.build(story)
+
 def makeSuite():
-    return makeSuiteForClasses(EncryptTestCase, PreciseEncryptTestCase)
+    return makeSuiteForClasses(EncryptTestCase)
 
 
 #noruntests