pip install reportlab
Create your first pdf file.
from reportlab.pdfgen import canvas
my_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf'
c = canvas.Canvas(my_path,bottomup=0)
c.drawString(200,200,"Hello World") # write text in page
c.showPage() # saves current page
c.save() # stores the file and close the canvas
Above code will create the my_pdf.pdf file in the path and write the string Hello World at x=200 and y= 200 position.
c.setFont("Helvetica", 20) # font family and size
#c.setFillColorCMYK(.2,.2,0,.39) # font colour as CMYK
#c.setFillColorRGB(0,0,1) # font colour as RGB
#c.setFillColor('lightgreen')#lawngreen, lightblue,lemonchiffon
c.setFillColor('lightblue') #lavendarblush
c.drawString(200,200,"Hello World") # write text in page
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter
my_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf'# file path
c = canvas.Canvas(my_path,pagesize=letter)
c.translate(inch,inch) # starting point of coordinate to one inch
c.setFillColorRGB(0,0,1) # fill colour
l1=c.getAvailableFonts() # list of available fonts
j=1 # to adjust Y coordinate against each row
for my_font in l1: # loop through all fonts
c.setFont(my_font,16) # set font family and size
c.drawString(inch,j*inch*0.5,my_font)
j=j+1
c.showPage() # saves current page
c.save() # stores the file and close the canvas
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter, A4
We can set the margins of the page like this
c.translate(inch,inch) # set the starting point of coordinate
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter,A4
my_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf'# file path
c = canvas.Canvas(my_path,bottomup=1,pagesize=letter)
c.translate(inch,inch) #starting point of coordinate to one inch
c.setStrokeColorRGB(1,0,0) # red colour of line
c.setLineWidth(10) #width of the line
c.line(0,8*inch,7*inch,8*inch) # draw line
c.showPage() # saves current page
c.save() # stores the file and close the canvas
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
my_path='E:\\testing\\my_pdf\\my_pdf.pdf'# file path
c = canvas.Canvas(my_path,bottomup=1,pagesize=A4)
w,h=A4 # width and height of the page
xlist = [20, 70, 120, 170] # Horizontal coordinates
ylist = [h - 20, h - 70, h - 120, h - 170] # vertical
c.grid(xlist, ylist) # adding grid
c.save() # stores the file and close the canvas
Adding text inside grid.
c.grid(xlist, ylist)
c.drawString(70,h-70,"plus2net") # write text in page
Creating PDF Documents with Grid and 2D List Data
c.drawImage('D:\\top2.jpg',-0.8*inch,9.3*inch)
Here we have fixed the margin by using translate() method, so we used -0.8*inch as X value to keep the image at Left edge.
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter,A4
my_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf'
c = canvas.Canvas(my_path,bottomup=1,pagesize=letter)
c.translate(inch,inch) #starting point of coordinates
c.drawImage('D:\\top2.jpg',-0.8*inch,9.3*inch)
c.showPage() # saves current page
c.save() # stores the file and close the canvas
We can use Image from URL
c.drawImage('https://www.plus2net.com/images/top2.jpg',-0.8*inch,9.3*inch)
c.rotate(-45)
c.drawImage('D:\\top2.jpg',0,0)
c.rotate(45)
c.setLineWidth(10)
c.setStrokeColor('yellow')
c.setFillColor('lightgreen')
With these settings , the full code is here.
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter,A4
my_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf'# file path
c = canvas.Canvas(my_path,bottomup=1,pagesize=letter)
c.translate(inch,inch) # starting point of coordinate to one inch
c.setLineWidth(10) # width of the border of rectangle
c.setStrokeColor('yellow') # border colour
c.setFillColor('lightgreen') # fill colour
c.rect(1*inch,2*inch,3.5*inch,5*inch,fill=1)
c.showPage() # saves current page
c.save() # stores the file and close the canvas
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter,A4
my_path='E:\\testing\\my_pdf\\my_pdf7.pdf'# file path
# Custom page size: width=595, height=841
custom_size = (595, 841)
c = canvas.Canvas(my_path,bottomup=1,pagesize=custom_size)
c.translate(inch,inch) # starting point of coordinate to one inch
c.setLineWidth(10) # width of the border of rectangle
c.setStrokeColor('yellow') # border colour
c.setFillColor('lightgreen') # fill colour
c.rect(1*inch,2*inch,3.5*inch,5*inch,fill=1)
c.showPage() # saves current page
c.save() # stores the file and close the canvas
c.rotate(45) # rotate by 45 degree
# font colour CYAN, MAGENTA, YELLOW and BLACK
c.setFillColorCMYK(0,0,0,0.08) # colour values
c.setFont("Helvetica", 100) # font style and size
c.drawString(2*inch, 1*inch, "SAMPLE") # String written
c.rotate(-45) # restore the rotation
Full code with watermark is here
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter,A4
my_path='G:\\My drive\\testing\\pypdf2\\my_pdf.pdf'# file path
c = canvas.Canvas(my_path,bottomup=1,pagesize=letter)
c.translate(inch,inch) # starting point of coordinate to one inch
c.setLineWidth(10) # width of the border of rectangle
c.setStrokeColor('yellow') # border colour
c.setFillColor('lightgreen') # fill colour
c.rect(1*inch,2*inch,3.5*inch,5*inch,fill=1)
#start watermark
c.rotate(45) # rotate by 45 degree
# font colour CYAN, MAGENTA, YELLOW and BLACK
c.setFillColorCMYK(0,0,0,0.08) # colour values
c.setFont("Helvetica", 100) # font style and size
c.drawString(2*inch, 1*inch, "SAMPLE") # String written
c.rotate(-45) # restore the rotation
c.setFont("Helvetica", 10)
c.setFillColorRGB(1,0,0) # font colour
c.drawString(0, -0.9*inch, u"\u00A9"+" plus2net.com")
c.showPage() # saves current page
c.save() # stores the file and close the canvas
from reportlab.pdfgen import canvas
from reportlab.lib import pdfencrypt
pw=pdfencrypt.StandardEncryption('test') # password is test
my_path='D:\\testing\\my_pdf\\my_pdf.pdf'# file path
c = canvas.Canvas(my_path,bottomup=0,encrypt=pw)
c.drawString(200,200,'Hello world')
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.