DEV Community

Cover image for Converting An Image File Into PDF Using Python
Ethan
Ethan

Posted on • Originally published at ethan-dev.com

Converting An Image File Into PDF Using Python

Introduction

Hello! 😎
In this tutorial I will show you how you can easily convert an image file into a PDF file easily using Python.

I recently had to do this for work so I thought I'd make my own tool. 😸


Creating The Virtual Environment

First we need to create a virtual environment, this is a practice I like doing because it doesn't interfere with the system packages.

Open up a terminal and type the following to create the virtual environment:

python3 -m venv env
Enter fullscreen mode Exit fullscreen mode

Next active it with the following:

source env/bin/activate
Enter fullscreen mode Exit fullscreen mode

Next we need to install the libraries required for the project, open up a file called "requirements.txt" and fill it with the following:

pillow
reportlab
Enter fullscreen mode Exit fullscreen mode

Save and close the file and then run the following command to install the libraries:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Great now we can start coding. 😊


Creating The Converter

Now that we have the needed libraries installed we can now move onto actually coding the project. Open a file called "main.py" and first add the following imports:

from PIL import Image
from reportlab.pdfgen import canvas
Enter fullscreen mode Exit fullscreen mode

Once we have imported the above, we next need to create a function that actually does the converting:

def image_to_pdf(image_path, output_pdf):
    # Open the image file
    with Image.open(image_path) as img:
        # Get image dimensions
        img_width, img_height = img.size

        # Create a new PDF with the same dimensions as the image
        c = canvas.Canvas(output_pdf, pagesize=(img_width, img_height))
        c.drawInlineImage(image_path, 0, 0, width=img_width, height=img_height)

        # Save the PDF file
        c.save()
Enter fullscreen mode Exit fullscreen mode

I've decided to let the comments speak for themselves, basically we create a new PDF file with the same dimensions as the image passed.

Finally we can now called the main function:

if __name__ == "__main__":
    input_image = input("Enter the path of the image file: ")
    output_file = input("Enter the name for the output PDF file (e.g. output.pdf): ")

    image_to_pdf(input_image, output_file)
    print(f"PDF saved as {output_file}")
Enter fullscreen mode Exit fullscreen mode

Done! 😁 The above can be run via the following command:

python main.py
Enter fullscreen mode Exit fullscreen mode

Enter an imput image and an output PDF and your image should be converted into a PDF file. πŸ˜†


Conclusion

In this tutorial I have provided a way on you can create a Python script that converts an image into a PDF file.

I hope you find this tutorial useful, and as always happy coding. 😎

As always you can find the source code for the above at my Github.
https://github.com/ethand91/image-to-pdf


Like my work? I post about a variety of topics, if you would like to see more please like and follow me.
Also I love coffee.

β€œBuy Me A Coffee”

If you are looking to learn Algorithm Patterns to ace the coding interview I recommend the following course

Top comments (2)

Collapse
 
dshaw0004 profile image
Dipankar Shaw

You can just use PILLOW to convert an image into pdf.
I will try your technique to compare both PDFs.

Collapse
 
ethand91 profile image
Ethan

I guess you could. Sure please compare and let me know the results.