Generating PDF ID card by using user input data & image through Tkinter window using ReportLab
Tkinter window to take user inputs
We need five input data from user to create the ID Card.
s_name : Student name s_id : Student ID s_class : Student Class s_gender : Student Gender s_filename : Student picture as Path to the file.
We used StringVar() to collect data and pass the same to main file.
Button b2 is used to add click event to close the window command=lambda:my_w.destroy(). Here is the full code.
We Import the blank template and use the same for creating the ID card.
from tk_id_card_temp import my_temp # import the template
Inside our main file tk_id_card_main.py we receive the five data from the above Tkinter window.
from tk_id_input import s_name,s_id,s_class,s_gender,s_filename
These variables are StringVar() so we will use get() method to collect the data and use them for creating the ID card . Full code is here
tk_id_card_main.py
from reportlab.pdfgen import canvas
import tkinter as tk
my_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf' # update the path
from reportlab.lib.units import inch
from tk_id_card_temp import my_temp # import the template
from tk_id_input import s_name,s_id,s_class,s_gender,s_filename #tkinter
## comment below values if you are using above Tkinter window
#s_filename='D:\\images\\rabbit_face2.jpg'
c = canvas.Canvas(my_path,pagesize=(400,300))
c=my_temp(c) # run the template
c.drawImage(s_filename,2.2*inch,0.7*inch) #Add image
###### Adding Collected data ####
c.setFillColorRGB(0,0,1)
c.setFont("Helvetica", 20)
c.drawString(0.5*inch,1.7*inch,str(s_id.get()))
c.drawString(0.5*inch,1.3*inch,s_name.get())
c.drawString(0.5*inch,0.9*inch,s_class.get())
c.drawString(0.5*inch,0.5*inch,s_gender.get())
######
c.showPage()
c.save()
We used one template by adding logo, labels , line and some copyright text. We disused about the blank template in Part 1. Full code is here