Convert Images to a PDF Using Python (Including Merging)
In everyday office or document work, we often need to merge multiple images into a single PDF file. Whether organizing scans, creating an e-book, or archiving materials, converting images to PDF is a very practical task. This article shows how to use Python and the Spire.PDF for Python library to easily convert and merge images into a PDF.
Why Use Spire.PDF for Python?
Spire.PDF for Python is a powerful PDF manipulation library that not only supports creating, reading, and editing PDF documents but also provides rich image-handling features. Compared with other libraries, Spire.PDF’s API is simple and intuitive, enabling easy image-to-PDF conversion and allowing precise control of page size and image layout.
Install it via PyPI:
pip install spire.pdf
Complete Code Example
The following code demonstrates how to merge all JPG/JPEG images in a specified folder into a single PDF file:
from spire.pdf import *
import os
# Folder path containing images
image_folder = r"C:\Users\Administrator\Desktop\Images"
# Output PDF file path
output_file = "output/CombinedImages.pdf"
# Ensure the output directory exists
os.makedirs(os.path.dirname(output_file), exist_ok=True)
# Create a PDF document object
doc = PdfDocument()
# Remove page margins so images fill the whole page
doc.PageSettings.SetMargins(0.0)
# Get all JPG/JPEG files and sort them
image_files = sorted([
f for f in os.listdir(image_folder)
if f.lower().endswith((".jpg", ".jpeg"))
])
# Add each image to the PDF
for image_name in image_files:
image_path = os.path.join(image_folder, image_name)
# Load the image
image = PdfImage.FromFile(image_path)
# Get image dimensions
width = image.PhysicalDimension.Width
height = image.PhysicalDimension.Height
# Create a page with the same size as the image
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 merged PDF file
doc.SaveToFile(output_file)
doc.Dispose()
Code Explanation
- Import libraries and set paths: Import Spire.PDF and the os module, and define the image folder path and output file path.
- Create the PDF document: Create an empty PDF document with
PdfDocument()and remove page margins withSetMargins(0.0)so images can fill the page completely. - Read image files: Use
os.listdir()to get files in the folder, filter for JPG and JPEG usingendswith(), and sort withsorted()to ensure images are merged in filename order. - Add images one by one: For each image, load it with
PdfImage.FromFile(), get its original dimensions, create a PDF page with matching size, and draw the image on the page usingDrawImage(). - Save and release resources: Save the PDF with
SaveToFile()and callDispose()to free document resources.
Output
After running the code above, the program will automatically generate CombinedImages.pdf in the output folder. Each page of the PDF corresponds to one original image, and the page size matches the image dimensions, ensuring optimal display.
Extensions
Based on the code above, you can easily extend functionality:
- Support more image formats: Add
.png,.bmp, etc., to the filter. - Custom page size: Use a fixed page size instead of matching the image size.
- Add image compression: Adjust image quality to control the PDF file size.
- Batch processing: Generate separate PDFs for multiple folders.
Summary
Using Spire.PDF for Python to convert images to PDF results in concise, easy-to-understand code without requiring additional dependencies. Whether for personal or enterprise use, this feature can be quickly integrated. I hope this helps you improve efficiency in document handling and makes image management and sharing more convenient.
评论
发表评论