Commits

Gokmen Gorgen committed 0c0c47c

pdftk integration.

  • Participants
  • Parent commits a7f729b

Comments (0)

Files changed (3)

File core/example.pdf

Binary file added.

File core/utils.py

+import random
+import string
 import tempfile
 from subprocess import call
 

File core/views.py

+import os
+import tempfile
 from django.http import HttpResponse
 from django.views.generic import View
 from reportlab.pdfgen import canvas
+from core.utils import pdftk
 
 
 class CoreIndex(View):
         response = HttpResponse(mimetype='application/pdf')
         response['Content-Disposition'] = 'filename=somefilename.pdf'
 
+        # First, create watermark..
         # Create the PDF object, using the response object as its "file."
-        p = canvas.Canvas(response)
+        watermark_path = tempfile.mktemp()
+        watermark = canvas.Canvas(watermark_path)
 
         # Draw things on the PDF. Here's where the PDF generation happens.
         # See the ReportLab documentation for the full list of functionality.
-        p.drawString(100, 100, "Hello world.")
+        watermark.drawString(100, 100, "Hello world.")
 
         # Close the PDF object cleanly, and we're done.
-        p.showPage()
-        p.save()
+        watermark.showPage()
+        watermark.save()
+
+        # Now stamp watermark and encrypt original file.
+        doc_path = os.path.join(os.path.dirname(__file__), 'example.pdf')
+        output_path = pdftk(doc_path, watermark_path, password=True)
+
+        # And last, write output to response.
+        response.write(file(output_path, 'r').read())
+
         return response