Convert Images to PDF with Python – A Clean Approach

Casie LiuCasie Liu
4 min read

There are plenty of reasons you might want to convert images to PDF—maybe you're auto-generating reports, or just want to bundle a few visuals into one neat file. Whatever the use case, Python makes the whole process surprisingly straightforward. In this guide, I’ll show you a fast and reliable way to turn PNG, JPEG, and SVG images into high-quality PDFs using nothing more than a few lines of clean, readable code.

Set Up Spuire.PDF in Your Python Project

Before we go straight to the point, a reliable Python library is what you need to streamline the image-to-PDF conversion. Spire.PDF for Python is a professional and powerful Python component, which allows you to read, edit and convert PDFs smoothly without Adobe Acrobat. You can install it using the pip command: pip install spire.pdf, or download the package and customize the installation.

Convert PNG or JPEG to PDF in Python Easily

PNG and JPEG are two of the most common image formats you'll come across. So let’s start by converting these in Python. Since both are raster images, we can use the same approach for handling them. In code, this means creating a PDF document, reading the image and getting its dimensions, generating a page with the exact same size, and drawing the image onto it. After that, we simply save the file as a PDF. Here’s what that looks like in practice.

Code example - Convert a PNG to PDF:

from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
document = PdfDocument()

# Set the page margins to 0
document.PageSettings.SetMargins(0.0)

# Load an image file
image = PdfImage.FromFile("/book cover.png")

# Get the image width and height
imageWidth = image.PhysicalDimension.Width
imageHeight = image.PhysicalDimension.Height

# Add a page that has the same size as the image
page = document.Pages.Add(SizeF(imageWidth, imageHeight))

# Draw image at (0, 0) of the page
page.Canvas.DrawImage(image, 0.0, 0.0)

# Save to file
document.SaveToFile("/output/PNGToPdf.pdf")

# Dispose resources
document.Dispose()

Convert PNG to PDF in Python Easily

You may like: Quick & Easy Guide to Convert PDF to Images in Python >>

Convert SVG to PDF Using Python in 3 Steps

Aside from PNG and JPEG, there’s also the SVG format—which works a little differently. Unlike raster images, SVG is a vector-based format, meaning it uses paths and shapes instead of pixels. Because of this, converting SVG to PDF requires a different approach. Luckily, Spire.PDF makes it super simple with two methods: Document.LoadFromSvg() and Document.SaveToFile(). With just three straightforward steps, you can turn any SVG into a clean PDF. Let’s check out how that looks in code.

Code example - Convert SCG to PDF:

from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
document = PdfDocument()

# Load an SVG file
document.LoadFromSvg("/rtf-file-symbol.svg")

# Save the SVG file to PDF
document.SaveToFile("/output/SvgToPdf.pdf", FileFormat.PDF)

# Dispose resources
document.Dispose()

Batch Convert Multiple Images to PDF in Python

Converting a single image to PDF is straightforward using either of the methods above. But what if you need to handle multiple images? That’s just as easy. You can simply place all the images you want to convert into the same folder, loop through them one by one, get each image’s dimensions, and draw them onto PDF pages of matching size using the PdfPageBase.Canvas.DrawImage() method. Here's how to put it all together in code.

Code example - Convert five images to PDF:

from spire.pdf.common import *
from spire.pdf import *
import os

# Create a PdfDocument object
doc = PdfDocument()

# Set the page margins to 0
doc.PageSettings.SetMargins(0.0)

# Get the folder where the images are stored
path = "/input/New folder/"
files = os.listdir(path)

# Iterate through the files in the folder
for root, dirs, files in os.walk(path):
    for file in files:

        # Load a particular image
        image = PdfImage.FromFile(os.path.join(root, file))

        # Get the image width and height
        width = image.PhysicalDimension.Width
        height = image.PhysicalDimension.Height

        # Add a page that has the same size as the image
        page = doc.Pages.Add(SizeF(width, height))

        # Draw image at (0, 0) of the page
        page.Canvas.DrawImage(image, 0.0, 0.0, width, height)

# Save to file
doc.SaveToFile("/output/multipleImages.pdf")
doc.Dispose()

Convert Multiple Images to PDF in Python Quickly

The code above demonstrates how to convert multiple PNG images into a single PDF. If you're working with SVGs instead, the idea is similar—just loop through the SVG files in a folder and use Document.LoadFromSvg() for each one before saving them to PDF pages.

The Bottom Line

In this post, we explored how to convert images—both PNG/JPEG and SVG—into PDF using Python. With the help of Spire.PDF, you can easily handle single or multiple images without any tedious manual steps.

For more Python PDF tutorials, feel free to check out my Hashnode profile or browse the Spire.PDF documentation. Got questions or tips to share? Drop a comment below—I'd love to hear your thoughts!

0
Subscribe to my newsletter

Read articles from Casie Liu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Casie Liu
Casie Liu