Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • PDF Invoice Generator
Menu

Python Reportlab & Pillow Script to Generate Bulk Certificates as PNG Image & PDF With Custom Fonts

Posted on May 5, 2023

 

 

Welcome folks today in this blog post we will be generating bulk certificates as png image and pdf document with custom fonts in python. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make a new folder and inside it we need to make a list of names for which we need to generate the certificates as shown below

 

 

names.txt

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
Aahana Ganjewar
Aarya Redekar
Aaryamonvikram Singh
Aaryash Nayak
Aayush Vetkar
Adarsh Tiwari
Aditya Rajesh Ailsinghani
Aditya Sarpotdar
Aditya Surve
Akash Khatri
Alex Bafna
Aneesh Shamraj
Anushka Dnyanmothe
Archit Rathod
Archit Rathod
Arihant Tonage
Arpita Pawar
Aryan Ruparel
Atharva Kadam
Atmaj Koppikar
Avantika Gavankar
Ayaan Gani
Bhagavatiraj Yadav
Bhavesh Daphale
Bhavesh Pharate
Darshan Rander
Deep Shukla
Deep Shukla
Devika Panjwani
Dhruv Gogri
Dhruv Jain
Disha Ramchandani
Farhan Shaikh
Ganesh Vaishnav
Garima Kakwani
Garima Kakwani
Gaurav Advani
Hamza Khan
Harsh Modi
Harsh Nagpal
Hiten Gerella
Hrishit Kotadia
Idris Ratlamwala
Isha Nalawade
Isha Suresh Parab
Ishika Khokhani
Jai Parmani
Jay Aslaliya
Jay verma
Jayu jain
Jayu Jain
Joshua Ashok Torlikonda
Jugal Ghia
Karan Chandwani
Karan Chopra
Kartik Soneji
Keyul Jain
Khushi Bhatia
Krish Shah
Krishna Dave
Krishna khadke
Kriya shah
Kushal Tejwani
Manan Mehta
Manasi Ghutukade
Manav Doshi
Mandar Kasangottuwar
Manish Bhatia
Manjunath Vasam
Manoah Ashok Torlikonda
Manoah Ashok Torlikonda
Md. Rashid Aziz
Md. Rashid Aziz
Mihika Bodke
Mokshit Surana
Mounika Jindam
Muskan Gupta
Muskan Khasturia
Nahush Patil
Neeraj Patil
Nidhi Kashyap
Nikhil Kumar Sharma
Nikunj Mistry
Nimit Jhunjhunwala
Om Mandiyan
Omar Vora
Param charha
Parth Shah
Piush Paul
Piyush Hinduja
Prajwal Dhule
Pranav Dani
Pranjali Ashok Dere
Pranjali Gaonkar
Prasad Iyer
Prasham Bhagwat
Pratik Kundnani
Pratik Thakare
Prem Dhawane
Prithvi Jadwani
Puneet Pankaj Bhatia
Rahul Chabbaria
Rajiv Rane
Rhutam Thakur
Rishiraj Girmal
Rising Coders
Ritesh Mestry
Riz Lala
Rohit Sonejee
Ruchika Motwani
Sachin Jangir
Sahil Samel
Saket Thota
Sanjeevan Bapat
Sanket Ardekar
Saris shashikant singh
Saurav Uppoor
Shivam Trivedi
Shravan Chenna
Shrey Kadam
Shreya Vinod Amin
Shreyans Jain
Shreyans Jain
Shyren More
Siddhant Kothari
Smit Madhani
Sneh Davaria
Sneha Dahikar
Soham Sachdev
Soham Sanghvi
Sourav Macwan
Sumanta Jena
Suraj Chavan
Tamanna Ahuja
Tania Awatramani
Tania Awatramani
Tanisha Gupta
Tanooj Raghani
Tanya Chandnani
Tinna Vasnani
Udit Kumar Jain
Umang singh
Vansh Kamal Chanchlani
Vansh Wadhwa
Viknesh Devendran
Vivek Santosh Namaye
Yash Dudani
Yash Jain
Yash Khatwani
Yash shah
Yash Tripathi
Yohan Gupta
Zubiya Alvi

 

 

And also we need to install the below libraries as shown below using the pip command

 

 

pip install reportlab

 

 

pip install pillow

 

 

And after that you need to make an app.py file and copy paste the following code

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from PIL import Image, ImageFont, ImageDraw
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.utils import ImageReader
 
 
# Global Variables
FONT_FILE = ImageFont.truetype(r'font/Righteous-Regular.ttf', 180)
FONT_COLOR = "#FFFFFF"
 
template = Image.open(r'template.png')
WIDTH, HEIGHT = template.size
 
def make_certificates(name):
    '''Function to save certificates as a .png file'''
 
    image_source = Image.open(r'template.png')
    draw = ImageDraw.Draw(image_source)
 
    # Finding the width and height of the text.
    name_width, name_height = draw.textsize(name, font=FONT_FILE)
 
    # Placing it in the center, then making some adjustments.
    draw.text(((WIDTH - name_width) / 2, (HEIGHT - name_height) / 2 - 30), name, fill=FONT_COLOR, font=FONT_FILE)
 
    # Saving the certificates in a different directory.
    image_source.save("./out/" + name +".png")
 
    image = Image.open("./out/" + name +".png")
 
    # Define the output PDF filename
    output_filename = "./pdf/" + name + ".pdf"
 
    # Set the PDF document size to letter
    pdf_canvas = canvas.Canvas(output_filename, pagesize=letter)
 
    # Add the image to the PDF document
    pdf_canvas.drawImage(ImageReader(image), 0, 0, width=letter[0], height=letter[1])
 
    # Save the PDF document
    pdf_canvas.save()
 
    
    print('Saving Certificate of:', name)        
 
if __name__ == "__main__":
 
    names = []
 
    with open('names.txt', 'r') as file:
        for line in file:
            name = line.strip()  # remove any leading/trailing whitespaces
            names.append(name)
 
    for name in names:
        make_certificates(name)
 
    print(len(names), "certificates done.")

 

 

 

 

 

As you can see this will be the directory structure of the end app you need to create two folders first one for the image and secondly for the pdf documents. And also we will have the folder to store the custom google fonts that you want to use for the certificates and also we have the png image which we are using for the template of the certificate

 

 

 

Recent Posts

  • Node.js Express Project to Remove Background of Images Using Rembg & Formidable Library in Browser
  • Node.js Tutorial to Remove Background From Image Using Rembg & Sharp Library in Command Line
  • Python 3 Flask Project to Remove Background of Multiple Images Using Rembg Library in Browser
  • Python 3 Rembg Library Script to Bulk Process Multiple Images and Remove Background in Command Line
  • Python 3 Rembg Library Script to Remove Background From Image in Command Line
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme