Image to PDF Conversion in Python: Complete Tutorial with Code

Image to PDF Conversion in Python: Complete Tutorial with Code

In the digital world, we often need to convert images to PDF format for ease of sharing, archiving, or printing. Python makes this conversion simple with its wide array of libraries.

This guide will explain how to convert images to PDF format using Python, covering multiple approaches, code examples, and detailed explanations.

Requirements

For this project, we need to install the Pillow library, a powerful image processing library in Python. We’ll also use reportlab for more customization if needed.

Install these libraries using:

pip install pillow reportlab

What Each Library Does

  • Pillow: Provides image processing capabilities, including loading, manipulating, and saving images in various formats.
  • ReportLab: Generates PDFs from scratch with advanced formatting, which is useful if you want more control over the PDF layout.

Using the Pillow Library

The Pillow library provides a straightforward way to convert images to PDF. We’ll use its save() method to save an image in PDF format.

Example 1: Converting a Single Image to PDF

from PIL import Image

def convert_single_image_to_pdf(image_path, pdf_path):
    # Open the image file
    image = Image.open(image_path)
    
    # Convert image mode to RGB if it’s in RGBA or other formats
    if image.mode in ("RGBA", "P"):
        image = image.convert("RGB")
    
    # Save as PDF
    image.save(pdf_path, "PDF")
    print(f"{image_path} has been successfully converted to {pdf_path}")

# Usage example
convert_single_image_to_pdf("sample.jpg", "output.pdf")

Output:

Explanation:

  • Opening the Image: Image.open(image_path) loads the image file.
  • Mode Conversion: If the image has an alpha channel (like in PNG), we convert it to RGB to avoid transparency issues in the PDF.
  • Saving as PDF: image.save(pdf_path, "PDF") saves the image in PDF format.

Example 2: Converting Multiple Images into a Single PDF

If you have multiple images that you want to compile into one PDF document, the Pillow library allows this too.

from PIL import Image

def convert_multiple_images_to_pdf(image_paths, pdf_path):
    # Open all images and ensure they are in RGB mode
    images = [Image.open(img).convert("RGB") for img in image_paths]
    
    # Save all images as a single PDF
    images[0].save(pdf_path, save_all=True, append_images=images[1:])
    print(f"Images have been successfully combined into {pdf_path}")

# Usage example
image_files = ["image1.jpg", "image2.jpg", "image3.jpg"]
convert_multiple_images_to_pdf(image_files, "combined_output.pdf")

Explanation:

  • Loading Multiple Images: We load each image and ensure they’re in RGB mode.
  • Combining into a Single PDF: The save_all=True and append_images arguments let us save all the images in one PDF file.

Output:

3. Using the reportlab Library for Advanced Customization

If you need more control over the layout of the PDF, such as adding headers, footers, or arranging images precisely, reportlab is a better option.

Example 3: Creating a PDF with Multiple Images Using reportlab

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from PIL import Image

def create_pdf_with_images_reportlab(image_paths, pdf_path):
    # Create a canvas with a letter page size
    pdf = canvas.Canvas(pdf_path, pagesize=letter)
    width, height = letter
    
    for image_path in image_paths:
        # Open the image and resize it to fit the PDF
        image = Image.open(image_path)
        aspect_ratio = image.width / image.height
        img_width = width - 100
        img_height = img_width / aspect_ratio
        
        # Draw image on the PDF canvas
        pdf.drawImage(image_path, x=50, y=height - img_height - 50, width=img_width, height=img_height)
        
        # Add a new page if there are more images
        pdf.showPage()
    
    pdf.save()
    print(f"PDF created with images at {pdf_path}")

# Usage example
images = ["image1.jpg", "image2.jpg", "image3.jpg"]
create_pdf_with_images_reportlab(images, "custom_output.pdf")

Explanation:

  • Canvas Creation: A canvas is created with the page size set to letter.
  • Aspect Ratio Calculation: We calculate the aspect ratio to scale the image proportionally.
  • Drawing Images: Each image is drawn on the canvas.
  • New Pages: A new page is added after each image, allowing multiple images in a single document.

Output:

4. Converting Different Image Formats

With Pillow, this method works for various image formats (.jpg, .png, .bmp, etc.) without any modification. Both Pillow and reportlab support a range of image formats, making them flexible for most use cases.

Conclusion

Converting images to PDF format is a common requirement across various fields, from digital archiving to creating professional portfolios or simplifying document sharing. With Python’s extensive libraries, especially Pillow and ReportLab, achieving this is both accessible and customizable, depending on the complexity of your needs.

In this guide, we explored two primary methods to perform image-to-PDF conversion:

  1. Using the Pillow Library: Ideal for straightforward tasks, Pillow allows quick conversion of single or multiple images to PDF format with just a few lines of code. It’s particularly useful when you need a simple solution that requires minimal customization. We saw how to handle different image formats (such as PNG or JPG), address transparency issues by converting images to RGB, and combine multiple images into a single PDF document effortlessly.
  2. Using the ReportLab Library: When a more tailored PDF layout is needed, ReportLab offers fine-grained control. With it, you can format pages, scale images while maintaining aspect ratios, and even add elements like headers, footers, and multiple pages per image. This makes ReportLab ideal for more complex applications, such as creating branded documents, adding text annotations, or managing specific page layouts.

Both libraries offer robust solutions, with Pillow as a quick, lightweight option and ReportLab as a feature-rich tool for advanced PDF generation. By understanding the strengths and limitations of each, you can select the approach best suited to your project’s requirements.

Python’s versatility, combined with these libraries, ensures that you can scale your solution, whether you’re working on a one-time conversion or automating the process in a larger application. This skill empowers developers to streamline workflows, enhance digital presentation, and make document management more efficient. With the code examples and concepts from this guide, you are now equipped to build powerful and flexible image-to-PDF converters tailored to any need.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>