ID Card using Image and data

Template file tk_id_card_temp.py and Main file tk_id_card_main.py are the two files we will use. Inside 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.
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

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()

Creating Multiple ID cards from a List

Our list of student details are here.
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.

Two paths you have to set based on your system. One is for the generated pdf file and other one is the directory where the sample images are stored.
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 hight 

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.

Creating ID cards by using data from SQLite database table


ID cards by using SQLite table with images

To create the database follow the steps given here.
Creating SQLite database file with images and downloading
OR
Download student SQLite database file with images from here

Import Required Libraries

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.

Define Path to Generate PDF

my_path='D:\\testing\\my_pdf\\my_pdf.pdf' # path to generate pdf file
Specifies the path where the generated PDF will be saved.

Connect to SQLite Database

my_conn=sqlite3.connect('D:\\testing\\reportlab\\student_blob_2.db') # connect to db
Connects to the SQLite database located at the specified path.

Retrieve Data from Database

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.

Create Canvas for PDF

c=canvas.Canvas(my_path,pagesize=(400,300)) # width and height
Creates a new PDF canvas with specified dimensions (400x300 units).

Loop Through Records and Generate ID Cards

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()
  1. Loop Through Records: Iterates over each record in the result set.
  2. Read Image: Converts the image BLOB data into an image object using ImageReader.
  3. Apply Template: Calls the my_temp function to apply the custom template to the canvas.
  4. 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).
  5. Set Font Color and Size: Sets the font color to blue and the font size to 20.
  6. Add Text Data: Draws the student data (ID, name, class, gender) onto the canvas at specified coordinates.
  7. New Page: Calls showPage to finalize the current page and start a new page for the next student.

Save the PDF

c.save()
Saves the canvas, finalizing the PDF file at the specified path.

Full code is here
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 hight 

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()

Reportlab PDF ID cards using SQLite records with BLOB images by using Colab platform. Part - II



Conclusion

This code demonstrates how to generate a PDF file containing ID cards for students by retrieving data from an SQLite database. Each student's ID card includes their photo and details, formatted using the ReportLab library. The use of the ImageReader utility allows for efficient handling of image data stored in the database as BLOBs, ensuring that the images are correctly rendered on the PDF pages. By following this example, you can create customized PDF documents with data and images dynamically retrieved from a database.

PDF ID card from user Inputs in Tkinter
PDF Mark sheet by using data from SQLite database Generate PDF Invoice using Sales data Table from different data sources to PDF file Table from Pandas DataFrame to PDF file PDF Circles & Shapes
Create Report in PDF using Charts, images, tables and Paragraphs
Bar charts in PDF Line charts in PDF Pie Charts Paragraph
Python

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer