Converting a collection of scattered images into a single, searchable PDF is a frequent requirement for reporting tools, invoice generators, and archival systems. Aspose.PDF for Python via .NET is a powerful SDK that makes this conversion fast and reliable in Python applications. This guide walks you through installing the SDK, loading images with the Python Imaging Library, building the PDF page by page, tweaking compression settings, and finally saving the result, all with clear code examples.

Before You Start: Prerequisites and Installation

To follow this tutorial you need:

  • Python 3.8 or newer installed on your development machine.
  • An IDE or editor of your choice (VS Code, PyCharm, etc.).
  • Access to the internet to download the SDK package.

Install the Aspose.PDF SDK and the Pillow library (the de‑facto Python Imaging Library) with pip:

pip install aspose-pdf pillow

You can also download the SDK manually from the official release page: Aspose.PDF for Python via .NET Download. After installation, import the required namespaces in your script.

Now that the environment is ready, let’s dive into the implementation.

Generate PDF from Images in Python: Step-by-Step Walkthrough

Step 1: Load the Source Images

First, use Pillow to open each image file and keep them in a list. This prepares the image data for the PDF builder.

from PIL import Image
import os

image_folder = "input_images"
image_paths = [os.path.join(image_folder, f) for f in os.listdir(image_folder)
               if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]

images = [Image.open(p) for p in image_paths]

Step 2: Create a New PDF Document

Instantiate the Aspose.PDF Document object that will hold the pages.

import aspose.pdf as ap

pdf_doc = ap.Document()

For more details on the Document class, see the API reference.

Step 3: Add Each Image as a Separate Page

Loop through the loaded images, create a page, and embed the image. The SDK automatically scales the image to fit the page size.

for img in images:
    page = pdf_doc.pages.add()
    # Convert Pillow image to a byte stream compatible with Aspose.PDF
    with io.BytesIO() as img_stream:
        img.save(img_stream, format='PNG')
        img_stream.seek(0)
        page.resources.images.add(ap.Image(img_stream))
        # Fit the image to the page dimensions
        page.page_info.width = img.width
        page.page_info.height = img.height

Step 4: Configure Compression Options

Adjust the image compression level to reduce the final PDF size without sacrificing quality.

# Set JPEG compression for all images (if using JPEG format)
for img_res in pdf_doc.resources.images:
    img_res.compression = ap.ImageCompression.JPEG
    img_res.jpeg_quality = 80  # Quality range: 0‑100

Step 5: Save the PDF File

Finally, write the assembled document to disk.

output_path = "output/result.pdf"
pdf_doc.save(output_path)
print(f"PDF generated successfully at {output_path}")

With these steps, you have a fully functional workflow to generate PDF from images in Python.

Create PDF from Image Collection in Python - Complete Code Example

The following script puts all the pieces together into a single, runnable program.

import os
import io
from PIL import Image
import aspose.pdf as ap

# -------------------- Configuration --------------------
image_folder = "input_images"          # Folder containing source images
output_pdf = "output/result.pdf"       # Destination PDF file
jpeg_quality = 80                      # Compression quality (0‑100)

# -------------------- Load Images --------------------
image_paths = [os.path.join(image_folder, f) for f in os.listdir(image_folder)
               if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]

images = [Image.open(p) for p in image_paths]

# -------------------- Create PDF Document --------------------
pdf_doc = ap.Document()

for img in images:
    page = pdf_doc.pages.add()
    with io.BytesIO() as img_stream:
        img.save(img_stream, format='PNG')
        img_stream.seek(0)
        pdf_image = page.resources.images.add(ap.Image(img_stream))
        page.page_info.width = img.width
        page.page_info.height = img.height
        # Apply compression if the original is JPEG
        if img.format == 'JPEG':
            pdf_image.compression = ap.ImageCompression.JPEG
            pdf_image.jpeg_quality = jpeg_quality

# -------------------- Save PDF --------------------
pdf_doc.save(output_pdf)
print(f"PDF generated successfully at {output_pdf}")

Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (input_images, output/result.pdf), verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.

Advanced PDF Generation Settings: Options and Configuration

Beyond the basic workflow, Aspose.PDF offers several properties to fine‑tune the output.

Image Compression Level

Control the trade‑off between quality and file size.

pdf_image.compression = ap.ImageCompression.JPEG
pdf_image.jpeg_quality = 70   # Lower value = smaller file, lower quality

Page Size and Margins

Set a custom page size if you need a uniform layout.

page.page_info.width = 595   # A4 width in points
page.page_info.height = 842  # A4 height in points
page.page_info.margin = ap.MarginInfo(10, 10, 10, 10)

Resolution (DPI)

Higher DPI yields sharper images at the cost of larger PDFs.

pdf_doc.page_info.dpi = 300

For a full list of configurable properties, consult the API reference.

Conclusion

You now have a complete, programmatic solution to generate PDF from images in Python using Aspose.PDF for Python via .NET. The SDK handles image loading, page creation, compression, and saving with just a few lines of code, letting you focus on business logic instead of low‑level PDF handling. When you move to production, remember to acquire a proper license details are available on the pricing page and you can start with a temporary license for evaluation. Happy coding, and enjoy the simplicity of turning image collections into polished PDFs!

FAQs

  • How can I generate PDF from images in Python using Aspose.PDF?
    Use the Aspose.PDF SDK to create a Document, add a page for each image loaded with Pillow, optionally set compression, and call save() to write the PDF. The full example is included above.

  • What image formats are supported for PDF generation?
    The SDK accepts PNG, JPG, JPEG, BMP, GIF, and TIFF directly. Pillow can also convert unsupported formats into one of these before adding them to the PDF.

  • Do I need a license for commercial use?
    Yes. Obtain a temporary evaluation license from the temporary license page and purchase a full license via the pricing page for production deployments.

  • Can I adjust the PDF page size and resolution?
    Absolutely. Use page.page_info.width, page.page_info.height, and pdf_doc.page_info.dpi to control dimensions and DPI, as shown in the Configuration / Options section.

Read More