Template file tk_id_card_temp.py and Main file tk_id_card_main.py are the two files we will use. Insid the template file tk_id_card_main.py we will keep our Logo image and labels and in main file tk_id_card_temp.py we will add our data of the ID card.
We will call the template file from the main file
from tk_id_card_temp import my_temp # import the template
Creating ID cards in PDF by Python ReportLab with image and data using template with Label and Logo
Inside the template file we placed the Logo ( image ) and the Labels.
Water mark is placed inside 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
Main file tk_id_card_main.py
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 hight
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()