import imp
try:
imp.find_module('reportlab') # check for library
found = True
except ImportError:
found = False
print(found)
Install it if not available
pip install reportlab
from reportlab.pdfgen import canvas
my_path='my_pdf_file.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
!wget https://www.plus2net.com/python/download/1.png
Now that our image is successfully uploaded to the Colab platform, we're ready to incorporate it into our PDF document. It's important to pay close attention to the X and Y coordinates, which are crucial for positioning the image precisely in the desired location within the document. By adjusting these X and Y values, we can easily relocate the image to a different spot. This flexibility allows us to customize the layout of our PDF, ensuring that the image complements the document's overall design and content
c.drawImage('1.png',200,600) # Place the image
As we adjust the positioning of our image, remember that the X coordinate increases as we move to the right, while the Y coordinate increases as we move towards the top. This behavior can be changed by changing to using bottomup=0
from reportlab.pdfgen import canvas
my_path='my_pdf_file.pdf' # Name of the file
c = canvas.Canvas(my_path,bottomup=1) # create Canvas
c.setFont("Helvetica", 20) # font family and size
c.setFillColorRGB(0,0,1) # font colour as RGB
c.drawString(200,800,"Hello World") # write text in page
c.drawImage('1.png',200,600) # Place the image
c.showPage() # saves current page
c.save() # stores the file and close the canvas
# Download the database with a BLOB column storing the image.
!wget https://www.plus2net.com/python/download/student_blob.db
import sqlite3 # Connection library
my_conn = sqlite3.connect('student_blob.db') # connect to Database
query="PRAGMA table_info([student_profile])" # SQL to get table structure
my_data=list(my_conn.execute(query)) # rows of data as list
print(my_data) # structure of the table
query="SELECT * FROM student_profile LIMIT 0,5" # sql to display rows
my_data=list(my_conn.execute(query)) # rows of data as list
print(my_data) # includes the BLOB column data
We will first display one single row of data. Here we will get three columns id, student, profile_photo ( Image ) . import io
from PIL import Image
from reportlab.lib.utils import ImageReader
from reportlab.lib.units import inch
my_path='my_pdf.pdf' # file name
r_set=my_conn.execute('SELECT * from student_profile WHERE id=2');
for row in r_set:
image = Image.open(io.BytesIO(row[2]))
io_img = ImageReader(image) # read image
c = canvas.Canvas(my_path,bottomup=1) # create Canvas
c.setFont('Times-Bold',16)
c.drawString(2*inch,6*inch,str(row[0])) # write namepage
c.drawString(3*inch,6*inch,row[1]) # write namepage
c.drawImage(io_img,4*inch,6*inch) # Add image
c.showPage() # saves current page
c.save() # save and close
import io
from PIL import Image
from reportlab.lib.utils import ImageReader
from reportlab.lib.units import inch
my_path='my_pdf.pdf'
c = canvas.Canvas(my_path,bottomup=1)
c.setFont('Times-Bold',16)
r_set=my_conn.execute('SELECT * from student_profile ORDER BY id DESC');
h=5 # Y coordinate to increase
for row in r_set:
c.drawString(1*inch,h*inch-0.5,str(row[0])) # Id column
c.drawString(2*inch,h*inch-0.5,row[1]) # Name column
image = Image.open(io.BytesIO(row[2])) # get image from blob column
io_img = ImageReader(image) # read the image
c.drawImage(io_img,4*inch,h*inch) # Draw the image
h=h+1.5 # increase the Y coordinate
c.showPage() # saves current page
c.save() # save and close
from google.colab import files
files.download('my_pdf_multiple.pdf')
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.