We'll start with a simple example of creating a single password-protected PDF and then move on to generating multiple PDFs with unique passwords for each file.
from reportlab.pdfgen import canvas
from reportlab.lib import pdfencrypt
pw=pdfencrypt.StandardEncryption('test')
my_path='D:\\testing\\my_pdf\\my_pdf.pdf'# file path
c = canvas.Canvas(my_path,bottomup=0,encrypt=pw)
c.drawString(50,200,"Hello World") # write text in page
c.showPage() # saves current page
c.save() # stores the file and close the canvas
Let's break down the code step by step.
from reportlab.pdfgen import canvas
from reportlab.lib import pdfencrypt
pw=pdfencrypt.StandardEncryption('test')
my_path='D:\\testing\\my_pdf\\my_pdf.pdf'
c = canvas.Canvas(my_path,bottomup=0,encrypt=pw)
c.drawString(50,200,"Hello World")
c.showPage()
c.save()
This script will create a single PDF file named my_pdf.pdf at the specified location, protected with the password test.
This example uses a list of data to create multiple PDFs, each protected with a unique password of its own.
from reportlab.pdfgen import canvas
from datetime import datetime
from reportlab.lib import pdfencrypt
path='D:\\testing\\my_pdf\\my_pdf_'# file path
l1 = [(1, 'John Deo', 'Four', 75, 'female','2010-02-25'),
(2, 'Max Ruin', 'Three', 85, 'male','2010-12-12'),
(3, 'Arnold', 'Three', 55, 'male','2011-03-31')] # Data to be used
for rows in l1:
f=rows[0] # first column or id as file name
my_path=path+str(f)+'.pdf' # Path with file name
dt=datetime.strptime(rows[5],'%Y-%m-%d') # date object
str_dt=datetime.strftime(dt,'%b') # Month part only like Jan, Feb , Mar
str_name=rows[1][0:3] # first 3 chars of the name
str_pw=(str_name+str_dt).lower() # johfeb maxdec arnmar
pw=pdfencrypt.StandardEncryption(str_pw)
c = canvas.Canvas(my_path,bottomup=0,encrypt=pw)
my_data=str(rows[0]) + ','+ rows[1] + ','+ rows[2]+ ','+ str(rows[3])+ ','+ rows[4]
c.drawString(50,200,my_data) # write text in page
c.showPage() # saves current page
c.save() # stores the file and close the canvas
Let's break down the code step by step.
from reportlab.pdfgen import canvas
from datetime import datetime
from reportlab.lib import pdfencrypt
path='D:\\testing\\my_pdf\\my_pdf_'
l1 = [
(1, 'John Deo', 'Four', 75, 'female','2010-02-25'),
(2, 'Max Ruin', 'Three', 85, 'male','2010-12-12'),
(3, 'Arnold', 'Three', 55, 'male','2011-03-31')
]
for rows in l1:
f=rows[0]
my_path=path+str(f)+'.pdf'
Here str() is used to convert integer to String.
dt=datetime.strptime(rows[5],'%Y-%m-%d')
str_dt=datetime.strftime(dt,'%b')
strptime() : Returns date object from input string with format details.str_name=rows[1][0:3]
str_pw=(str_name+str_dt).lower()
lower() : Change all to lower case letters
pw=pdfencrypt.StandardEncryption(str_pw)
c = canvas.Canvas(my_path,bottomup=0,encrypt=pw)
my_data=str(rows[0]) + ','+ rows[1] + ','+ rows[2]+ ','+ str(rows[3])+ ','+ rows[4]
c.drawString(50,200,my_data)
c.showPage()
c.save()
This script will generate three password-protected PDF files, each containing data from the corresponding row in the list. The password for each PDF is generated using the first three characters of the name and the month from the date.
Filename | Password |
---|---|
my_pdf_1.pdf | johfeb |
my_pdf_2.pdf | maxdec |
my_pdf_3.pdf | arnmar |
dt=datetime.strptime(rows[5],'%Y-%m-%d') # date object
str_dt=datetime.strftime(dt,'%b') # Month part only like Jan, Feb , Mar
str_name=rows[1][0:3] # first 3 chars of the name
str_pw=(str_name+str_dt).lower() # johfeb maxdec arnmar
With the PDF security handler, we can specify two types of passwords for a document:
Providing either of these passwords will allow the PDF file to be opened, decrypted, and displayed on the screen.
from reportlab.pdfgen import canvas
from reportlab.lib import pdfencrypt
pw=pdfencrypt.StandardEncryption(userPassword='xyz',
ownerPassword='abc',
canPrint=False,
canModify=False,
canCopy=False,
canAnnotate=False)
my_path='D:\\testing\\my_pdf\\my_pdf.pdf'
c = canvas.Canvas(my_path, bottomup=0, encrypt=pw)
c.drawString(50, 200, "this is protected")
c.showPage()
c.save()
By following these steps, you can create PDFs with specific restrictions on printing, modifying, copying, and annotating using Python ReportLab. This ensures that your documents are protected according to your requirements.
Here is the complete Python code:from reportlab.pdfgen import canvas
from reportlab.lib import pdfencrypt
pw = pdfencrypt.StandardEncryption(userPassword='xyz',
ownerPassword='abc',
canPrint=False,
canModify=False,
canCopy=False,
canAnnotate=False)
my_path = 'D:\\testing\\my_pdf\\my_pdf.pdf' # pdf file path
c = canvas.Canvas(my_path, bottomup=0, encrypt=pw) # canvas
c.drawString(50, 200, "this is protected") # write text in page
c.showPage() # saves current page
c.save() # stores the file and closes the canvas