Back to Contents

Python Convert PNG to PDF using PIL

Installation

pip install Pillow

Usage

Please refer to Pillow’s documentation

Working Example

Aim:
Code:
from PIL import Image

#### Define Functions #####

class png2pdfConverter():
    def __init__(self,images_path_list):
        self.images_path_list = images_path_list
        
    def exportPDF(self,output_path):
        image_list = []
        for image_path in self.images_path_list:
            temp_image = Image.open(image_path).convert('RGB')
            image_list.append(temp_image)
        image_list[0].save(output_path,save_all=True, append_images=image_list[1:])

##########################

image_paths = ["./a.png","./b.png","someDir/c.png"]
output_path = "./output.pdf"
p2pc = png2pdfConverter(image_paths)
p2pc.exportPDF(output_path)
    

Source

Documentation link: Pillow’s documentation

Previous Post: Python Manipulate PDF

Next Post: Python Capture Webpage