Python PDF Generation: Image Processing with Pillow and ReportLab
Previous Postto see how to navigate a directory structure with Python. Well, let's take it a step further: with Python, it's very easy to automate the task of converting multiple image files into a single PDF.
In this post, we'll learn how to create a Python PDF, and to do so, we'll use the Pillowand ReportLab How to use libraries to process and convert images to PDFswhich can be useful for creating reports or portfolios. This post will give you the skills you need to create a Python PDF.

Pillow Libraries: The Basics of Image Processing
Pillowis one of the most popular libraries for handling image files in Python. It makes it easy to open images, resize them, save them in different formats, and more. In the PDF creation task, it is used to determine the size and resolution of an image and convert it to a PDF as is.
Installing and basic usage of Pillow
Pillow is easy to install using pip. If you're using VS code, you can run it in a Terminal window. I already have the package installed on my machine, so it says that the installation requirements are already met.
pip install Pillow
Let's look at a simple example of opening and verifying an image after installation.
from PIL import Image
img_path = "C:/Users/user/Documents/lease_renewal_process.png"
img = Image.open(img_path)
print(img.size) print # image sizeThis code performs the basic task of using Pillow to open an image and output the size of that image. This information will be important later when generating a PDF.
ReportLab: The Basics of PDF Creation
ReportLabis a very useful library for generating PDFs in Python. You can freely add text, images, shapes, and more, and you can even set custom page sizes. When converting an image to a PDF, the process works by setting up a page that fits the size of the image and inserting the image on top of it.
Installing and Basic Usage of ReportLab
ReportLab can also be installed using the pip command.
pip install reportlabLet's look at an example of creating a simple PDF file after installation.
from reportlab.pdfgen import canvas
pdf_path = "C:/Users/user/Documents/output.pdf"
c = canvas.Canvas(pdf_path)
c.drawString(100, 750, "Hello, this is a PDF!")
c.save()This code creates a PDF file that includes the text "Hello, this is a PDF!". You can add images as well as text to a PDF page, and you can take advantage of this feature by using the Python PDFautomatically, you can perform a variety of tasks.

Convert from image to PDF
Now let's take a look at an example of combining Pillow and ReportLab to convert multiple image files into a single PDF, which involves importing multiple image files, creating a page for each size, and inserting the image files onto the page.
Full code example
import os
from PIL import Image
from reportlab.pdfgen import canvas
def process_images(c, dir_path):
img_list = sorted([img_name for img_name in os.listdir(dir_path) if img_name.endswith(".png")])
for img_name in img_list:
img_path = os.path.join(dir_path, img_name)
img = Image.open(img_path)
img_width, img_height = img.size
c.setPageSize((img_width, img_height)) set page size to fit # image size
c.drawInlineImage(img_path, 0, 0, width=img_width, height=img_height) # Insert image
c.showPage()
def create_pdf_from_images(main_dir, output_pdf):
c = canvas.Canvas(output_pdf)
process_images(c, main_dir)
c.save()
print(f"A PDF file has been created: {output_pdf}")
main_dir = "C:/Users/user/Documents/images" # Folder where images are stored (adjust to suit your environment)
output_pdf = "C:/Users/user/Documents/output.pdf" # Path to the PDF to output (adjust to your environment)
create_pdf_from_images(main_dir, output_pdf)Code description
- process_images Function: This function imports PNG files from the specified directory, processes the images, and adds pages to the PDF for each image size.
- create_pdf_from_images Function: This function creates a Canvas object, processes the image file, and saves it as a PDF.
- Image processing with PillowThe key is to check the size of each image to ensure that the PDF page is sized to fit the image.
- Generate PDFs with ReportLabConvert each image file to PDF one page at a time through a Canvas object, and finally save it as a PDF file.
Cautions when converting from image to PDF
There are a few caveats to generating PDFs with Python. First, you should check the resolution and size of your images beforehand; if you set the page size too small, the images may not output properly. Also, be sure to specify the image file format you want to convert, as different extensions of images may require different processing.
Conclusion: The usefulness of Python PDF generation
In this post, we'll use the Python PDF Learn about creating, Pillowand ReportLabYou've learned how to convert images to PDFs with the help of this technique, which allows you to document multiple image files in a single PDF, which is very useful for creating reports or portfolios. Python PDFif you want to automate the task of generating them, follow the steps in this post!






