from tk_id_card_temp import my_temp # import the template
c.rotate(35)
c.setFillColorCMYK(0,0,0,0.08) # font colour
c.setFont("Helvetica", 100)
c.drawString(-1.1*inch, -0.5*inch, "SAMPLE") # watermarking
c.rotate(-35)
tk_id_card_temp.py , The template file
from reportlab.lib.units import inch
def my_temp(c):
c.translate(inch,inch)
# define a large font
c.setFont("Helvetica", 14)
# choose some colors
c.setStrokeColorRGB(0.1,0.8,0.1)
c.setFillColorRGB(0,0,1) # font colour
c.drawImage('D:\\top2.jpg',-0.9*inch,2.6*inch) #change path
#####
c.rotate(35) # Angle of water mark
c.setFillColorCMYK(0,0,0,0.08) # font colour
c.setFont("Helvetica", 100) # font family and size
c.drawString(-1.1*inch, -0.5*inch, "SAMPLE") # watermarking
c.rotate(-35)# restore the angle
#####
c.setFillColorRGB(1,0,0) # font colour
c.setFont("Helvetica", 25) # font family and size
c.drawRightString(1.7*inch,2.3*inch,'Identity Card') # Label
c.setFillColorRGB(0,0,0)
c.setFont("Helvetica", 24)
c.drawRightString(0.3*inch,1.7*inch,'ID:')
c.drawRightString(0.3*inch,1.3*inch,'Name:')
c.drawRightString(0.3*inch,0.9*inch,'Class:')
c.drawRightString(0.3*inch,0.5*inch,'Gender:')
c.drawRightString(4.0*inch,-0.5*inch,'Signature')
## Draw line and copyright information at the bottom part ##
c.line(-1.1,-0.7*inch,5*inch,-0.7*inch)
c.setFont("Helvetica",8)
c.setFillColorRGB(1,0,0) # font colour
c.drawString(0, -0.9*inch, u"\u00A9"+" plus2net.com")
return c
from reportlab.pdfgen import canvas
my_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf' # path to generate
from reportlab.lib.units import inch
from tk_id_card_temp import my_temp # import the template
c = canvas.Canvas(my_path,pagesize=(400,300)) # width and height
c=my_temp(c) # run the template
###### adding data from here ####
my_image='D:\\images\\rabbit_face2.jpg' # Path of the image
my_id,my_name,my_class,my_gender=18,'Kalu','Four','Female'# Data
c.drawImage(my_image,2.2*inch,0.7*inch) # Place Image
c.setFillColorRGB(0,0,1) # Font colour is blue
c.setFont("Helvetica", 20) # Font family and size
### add data ##
c.drawString(0.5*inch,1.7*inch,str(my_id)) # id to String
c.drawString(0.5*inch,1.3*inch,my_name) # Name
c.drawString(0.5*inch,0.9*inch,my_class)
c.drawString(0.5*inch,0.5*inch,my_gender)
###### adding data ends #####
c.showPage()
c.save()
my_students=[[1,'Alex J','Four','Female','1.png'],
[2,'Ramana K','Three','Male','2.png'],
[3,'Jack','Five','FeMale','3.png'],
[4,'Ronne','Six','FeMale','4.png']]
We kept the images inside one directory and the name of the image is kept as 4th element against each student list. You can download the zip file containing these sample images at the end of this page. my_path='D:\\testing\\my_pdf\\my_pdf.pdf' # path to generate pdf file
img_path='D:\\testing\\reportlab\\photos\\' # Directory Path of images
We used the same template as explained above.
from tk_id_card_temp import my_temp # import the template
Full code is here
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from tk_id_card_temp import my_temp # import the template
my_path='D:\\testing\\my_pdf\\my_pdf.pdf' # path to generate pdf file
img_path='D:\\testing\\reportlab\\photos\\' # Directory Path of images
my_students=[[1,'Alex J','Four','Female','1.png'],
[2,'Ramana K','Three','Male','2.png'],
[3,'Jack','Five','FeMale','3.png'],
[4,'Ronne','Six','FeMale','4.png']]
##############
c = canvas.Canvas(my_path,pagesize=(400,300)) # width and height
for ids in my_students:
c=my_temp(c) # run the template
###### adding data from here ####
my_image=img_path+ids[4] # image Path
c.drawImage(my_image,2.2*inch,0.7*inch) # Place Image
c.setFillColorRGB(0,0,1) # Font colour is blue
c.setFont("Helvetica", 20) # Font family and size
### add data ##
c.drawString(0.5*inch,1.7*inch,str(ids[0])) # id to String
c.drawString(0.5*inch,1.3*inch,ids[1]) # Name
c.drawString(0.5*inch,0.9*inch,ids[2]) # class
c.drawString(0.5*inch,0.5*inch,ids[3]) # gender
###### adding data ends #####
c.showPage()
c.save()
This code will generate a PDF file containing four pages, with each page featuring an ID card for a student. The list of students can easily be extended to include additional students, allowing the creation of a comprehensive set of ID cards.
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
import io
from reportlab.lib.utils import ImageReader
from tk_id_card_temp import my_temp # import the template
import sqlite3 # Connection library
reportlab.pdfgen.canvas
: Used to create PDF files.reportlab.lib.units.inch
: Provides a unit conversion function to specify dimensions in inches.io
: Used to handle binary data.reportlab.lib.utils.ImageReader
: Utility to read images from various sources.tk_id_card_temp
: Imports a custom template function my_temp.sqlite3
: Used to connect to the SQLite database and execute SQL commands.my_path='D:\\testing\\my_pdf\\my_pdf.pdf' # path to generate pdf file
Specifies the path where the generated PDF will be saved.
my_conn=sqlite3.connect('D:\\testing\\reportlab\\student_blob_2.db') # connect to db
Connects to the SQLite database located at the specified path.
r_set=list(my_conn.execute('SELECT * from student_b'))
Executes an SQL query to select all records from the student_b table.
Converts the result set to a list.
c=canvas.Canvas(my_path,pagesize=(400,300)) # width and height
Creates a new PDF canvas with specified dimensions (400x300 units).
for ids in r_set:
my_image=ImageReader(io.BytesIO(ids[4]))
c=my_temp(c) # run the template
###### adding data from here ####
c.drawImage(my_image,2.2*inch,0.7*inch) # Place Image
c.setFillColorRGB(0,0,1) # Font colour is blue
c.setFont("Helvetica", 20) # Font family and size
### add data ##
c.drawString(0.5*inch,1.7*inch,str(ids[0])) # id to String
c.drawString(0.5*inch,1.3*inch,ids[1]) # Name
c.drawString(0.5*inch,0.9*inch,ids[2]) # class
c.drawString(0.5*inch,0.5*inch,ids[3]) # gender
###### adding data ends #####
c.showPage()
Loop Through Records
: Iterates over each record in the result set.Read Image
: Converts the image BLOB data into an image object using ImageReader.Apply Template
: Calls the my_temp function to apply the custom template to the canvas.Place Image
: Uses drawImage to place the image on the canvas at the specified coordinates (2.2 inches from the left and 0.7 inches from the bottom).Set Font Color and Size
: Sets the font color to blue and the font size to 20.Add Text Data
: Draws the student data (ID, name, class, gender) onto the canvas at specified coordinates.New Page
: Calls showPage to finalize the current page and start a new page for the next student.c.save()
Saves the canvas, finalizing the PDF file at the specified path.
from reportlab.pdfgen import canvas
my_path='D:\\testing\\my_pdf\\my_pdf.pdf' # path to generate pdf file
from reportlab.lib.units import inch
import io
from reportlab.lib.utils import ImageReader
from tk_id_card_temp import my_temp # import the template
import sqlite3 # Connection library
my_conn = sqlite3.connect('D:\\testing\\reportlab\\student_blob_2.db') # connect to db
r_set=list(my_conn.execute('SELECT * from student_b'))
c = canvas.Canvas(my_path,pagesize=(400,300)) # width and height
for ids in r_set:
my_image = ImageReader(io.BytesIO(ids[4]))
c=my_temp(c) # run the template
###### adding data from here ####
#my_image='E:\\testing\\reportlab\\photos\\'+ids[4] # image Path
c.drawImage(my_image,2.2*inch,0.7*inch) # Place Image
c.setFillColorRGB(0,0,1) # Font colour is blue
c.setFont("Helvetica", 20) # Font family and size
### add data ##
c.drawString(0.5*inch,1.7*inch,str(ids[0])) # id to String
c.drawString(0.5*inch,1.3*inch,ids[1]) # Name
c.drawString(0.5*inch,0.9*inch,ids[2]) # class
c.drawString(0.5*inch,0.5*inch,ids[3]) # gender
###### adding data ends #####
c.showPage()
c.save()
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.