Images To PDF

The ImageToPdfConverter class creates PDF documents from images.

Call the ImageToPdfConverter.ImageToPdf function using a valid file path to an image to produce a new PDF document from that image.

Calling ImageToPdfConverter.ImageToPdf using a list of image paths will produce a single PDF document containing each image on separate pages.

from fpdf import FPDF
from PIL import Image
import os

class ImageToPdfConverter:
    @staticmethod
    def image_to_pdf(image_paths, output_pdf_path):
        """
        Convert a single image or a list of images to a PDF file.

        Parameters:
        image_paths (str or list of str): A single image path or a list of image paths.
        output_pdf_path (str): The output file path for the generated PDF.
        """

        # Ensure the image_paths is a list even if a single path is given
        if isinstance(image_paths, str):
            image_paths = [image_paths]

        # Create a PDF object
        pdf = FPDF()

        for image_path in image_paths:
            # Open the image with Pillow
            image = Image.open(image_path)

            # Convert image to RGB (just in case it's a different mode)
            if image.mode in ("RGBA", "P"): 
                image = image.convert("RGB")

            # Save the modified image temporarily
            temp_image_path = f"{os.path.splitext(image_path)[0]}_temp.jpg"
            image.save(temp_image_path)

            # Get the image width and height
            image_width, image_height = image.size

            # Add a page with the size of the image
            pdf.add_page(format=(image_width, image_height))

            # Add the image onto the page
            pdf.image(temp_image_path, 0, 0, image_width, image_height)

            # Remove the temporary image file
            os.remove(temp_image_path)

        # Output the PDF to a file
        pdf.output(output_pdf_path, "F")

# Example usage:
# ImageToPdfConverter.image_to_pdf('path/to/image.jpg', 'output.pdf')
# ImageToPdfConverter.image_to_pdf(['path/to/image1.jpg', 'path/to/image2.jpg'], 'output.pdf')
from fpdf import FPDF
from PIL import Image
import os

class ImageToPdfConverter:
    @staticmethod
    def image_to_pdf(image_paths, output_pdf_path):
        """
        Convert a single image or a list of images to a PDF file.

        Parameters:
        image_paths (str or list of str): A single image path or a list of image paths.
        output_pdf_path (str): The output file path for the generated PDF.
        """

        # Ensure the image_paths is a list even if a single path is given
        if isinstance(image_paths, str):
            image_paths = [image_paths]

        # Create a PDF object
        pdf = FPDF()

        for image_path in image_paths:
            # Open the image with Pillow
            image = Image.open(image_path)

            # Convert image to RGB (just in case it's a different mode)
            if image.mode in ("RGBA", "P"): 
                image = image.convert("RGB")

            # Save the modified image temporarily
            temp_image_path = f"{os.path.splitext(image_path)[0]}_temp.jpg"
            image.save(temp_image_path)

            # Get the image width and height
            image_width, image_height = image.size

            # Add a page with the size of the image
            pdf.add_page(format=(image_width, image_height))

            # Add the image onto the page
            pdf.image(temp_image_path, 0, 0, image_width, image_height)

            # Remove the temporary image file
            os.remove(temp_image_path)

        # Output the PDF to a file
        pdf.output(output_pdf_path, "F")

# Example usage:
# ImageToPdfConverter.image_to_pdf('path/to/image.jpg', 'output.pdf')
# ImageToPdfConverter.image_to_pdf(['path/to/image1.jpg', 'path/to/image2.jpg'], 'output.pdf')
PYTHON
  • Import necessary modules: FPDF for creating PDF files, Image from PIL for processing images, and os for file operations.
  • The method image_to_pdf takes one or multiple image paths and converts them into a PDF file, saving it at the specified output path.
  • Images are converted to RGB to ensure compatibility.
  • Temporary files are used to maintain the consistency of image formats.
  • The method handles single image paths and lists of image paths seamlessly.