Convert Images to PDF with Python

Alice Yang
4 min readNov 8, 2023

--

Convert Images to PDF with Python
Convert Images to PDF with Python

Converting image files into a PDF document allows you to consolidate related visual content in one file. This can be particularly useful when dealing with a collection of photos, diagrams, or scanned documents that you want to organize and manage as a cohesive unit. In this article, we will explore how to convert images to PDF using Python.

We will discuss the following topics:

Python Library to Convert Images to PDF

To convert images to PDF format with Python, we can use the Spire.PDF for Python library.

Spire.PDF for Python is a feature-rich and user-friendly library that enables creating, reading, editing, and converting PDF files within Python applications. With this library, you can perform a wide range of manipulations on PDFs, including adding text or images, extracting text or images, adding digital signatures, adding or deleting pages, merging or splitting PDFs, creating bookmarks, adding text or image watermarks, inserting fillable forms and many more. In addition, you are also able to convert PDF files to various file formats, such as Word, Excel, images, HTML, SVG, XPS, OFD, PCL, and PostScript.

You can install Spire.PDF for Python from PyPI using the following pip command:

pip install Spire.Pdf

For more detailed information about the installation, you can check this official documentation: How to Install Spire.PDF for Python in VS Code.

Convert an Image to a PDF with Python

Converting an image to a PDF can be achieved in two steps. Firstly, create a new page within the PDF document, making sure its dimensions match those of the original image. Next, place the image onto the newly created page.

The following code example shows how to convert an image to a PDF file using Python:

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

# Create a PdfDocument object
doc = PdfDocument()

# Remove page margins
doc.PageSettings.SetMargins(0.0)

# Load an image
image = PdfImage.FromFile("C:/Users/Administrator/Desktop/AI.jpg")

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

# Add a page with the same width and height to the PDF
page = doc.Pages.Add(SizeF(width, height))

# Draw the image on the newly added page
page.Canvas.DrawImage(image, 0.0, 0.0, width, height)

# Save the resulting PDF
doc.SaveToFile("ImageToPdf.pdf")
doc.Close()

Convert Multiple Images to a Single PDF with Python

Similarly, to convert multiple images to a single PDF, you first need to add a corresponding number of pages to the PDF, ensuring that their dimensions match those of the images. Then, place each of the images onto the respective PDF page.

The following code example shows how to convert multiple images under a specific directory to a single PDF file using Python:

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

# Create a PdfDocument object
doc = PdfDocument()

# Remove page margins
doc.PageSettings.SetMargins(0.0)

# Get the directory where the images are stored
image_dir = "C:/Users/Administrator/Desktop/Images/"

# Iterate through the images in the directory
for fileName in os.listdir(image_dir):
image_path = os.path.join(image_dir, fileName)

# Load the image
image = PdfImage.FromFile(image_path)
# Get the image width and height
width = image.PhysicalDimension.Width
height = image.PhysicalDimension.Height

# Add a page with the same width and height to the PDF
page = doc.Pages.Add(SizeF(width, height))

# Draw the image on the page
page.Canvas.DrawImage(image, 0.0, 0.0, width, height)

# Save the resulting PDF
doc.SaveToFile("MultipleImagesToOnePdf.pdf")
doc.Dispose()

Add an image to a specific location of a PDF with Python

In addition to converting an image or multiple images to PDF, you’re also able to draw them at specific locations on PDF pages.

The following code example shows how to add an image to a specific location of a PDF page using Python:

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

# Create a PdfDocument object
doc = PdfDocument()
# Load an existing PDF file
doc.LoadFromFile("Sample.pdf")

# Get the first page of the PDF
page = doc.Pages[0]

# Load an image
image = PdfImage.FromFile("C:/Users/Administrator/Desktop/logo.png")

# Specify the X and Y coordinates to draw the image
x = 80.0
y = 80.0

# Draw the image on the first page at the specified location
page.Canvas.DrawImage(image, x, y, image.PhysicalDimension.Width * 0.7, image.PhysicalDimension.Height * 0.7)

# Save the resulting PDF
doc.SaveToFile("AddImageToPdf.pdf", FileFormat.PDF)
doc.Close()

Conclusion

This article demonstrated how to convert an image or multiple images to PDF, along with how to add an image to a specific location of PDF using Python. We hope you find it helpful.

Related Topics

--

--

Alice Yang

Skilled senior software developers with five years of experience in all phases of software development life cycle using .NET, Java and C++ languages.