CodeNewbie Community 🌱

Cover image for Create A PDF From Multiple Images Using Python
Rizwan Hasan for TechLearners

Posted on • Edited on • Originally published at dev.to

Create A PDF From Multiple Images Using Python

In today's world PDF is a very popular file format among book readers, students, and officials, etc. Those who read books on their mobile phones or PC(Personal Computers) know the importance of PDF format in everyday life and many times need to make a single PDF from a bunch of images in order to share and read. As an example, it turned out to be a life-saving thing for me a couple of days ago. I love reading Manga. So, I downloaded a popular manga as a .zip file, after extracting there were 144 chapters as 144 folders and each folder has 19 to 32 images. So, I thought if I convert every chapter into a single PDF then it will be a lot easier to read and organize. I am going to share today how I did this thing easily using Python Programming Language.


PDF


It happens often that our friends send us some class notes as images on Messenger or WhatsApp. Instead of reading those as images, it’s more convenient to make a PDF and read it. Many people love reading comics and mangas too. Like me, they download episode images and read them. But also creating a PDF is a more convenient solution if you’re going to read it offline. Moreover, developers need this too. Downloading multiple images from websites or web-scraping many times ended up creating PDFs from those scrapped images. In such cases where you need to create a PDF from a bunch of images one time then it’s good to go with free software available on the internet. But if you need to do it more than 10 times or I say 100 times respectively, then isn’t it annoying? Strongly, you need a solution here but instead of finding a solution on ready-made software, why not you make your own solution? I ended up writing a couple of lines in Python to achieve the goal I’ve told you about earlier.


Code

Explanation

The code is very straight forward. I’ve taken five images and created an object of that images using the PIL library’s Image module. To create PDF, the image must need to have in RGB mode. So, I converted their mode to RGB. Now comes the tricky part. I wish to create a specific order of five images in PDF and that’s why I need to specify the order of images. For that, I created a Python list and on the list, I put them in my wished order. I want to create an order like this "image1, image2, image3, image4, image5" but I didn’t add the image1 in the list because if I do that then the image1 will be shown 2 times. It will show 2 times because when I’m creating the PDF, I started it just by adding the image1. So, if the image1 is already in the list and for PDF creation if I use the save option from image1 then it means image1 is already in the list and also adding when creating the PDF. For this reason, never add the first image of your desire list of images ordered.





Best of Luck

Share your opinion in the discussion section below and of course the questions if any. Don't forget to follow us.

💡 AND SUBSCRIBING to our YouTube TechLearnersInc and Telegram t.me/TechLearners will be amazing.


📌 Featured in DEV Community's Twitter

Top comments (2)

Collapse
 
genryetta profile image
GenryEtta

Thanks for the helpful PDF guide. In my turn, I want to share with everyone an excellent free PDFPlatform resource where you will find a simple solution for splitting pdf files. In my opinion, this resource is the best one for simplifying and speeding up workflows related to PDF and other files.

Collapse
 
dennyluyis profile image
dennyluyis • Edited

What a fantastic tutorial! Your personal experience with Manga really highlights the practicality of converting multiple images into a single PDF, making it easier to read and organize. Python is indeed a versatile language, and your step-by-step guide will undoubtedly help beginners and codenewbies in tackling this common task.

Additionally, it's great to see how technology, like the PDF format, plays a crucial role in our daily lives, especially for book readers, students, and professionals. Your tutorial not only provides a solution but also emphasizes the real-world relevance of the topic.

For those looking to streamline their PDF tasks even further, I'd like to recommend PDFGuru - Online PDF Maker. With pdfguru.com/ , you can effortlessly convert, edit, merge, and compress PDFs with just a few clicks. It's a user-friendly online tool that complements your Python solution, offering a convenient alternative for those who prefer a web-based approach. Keep up the excellent work, and thanks for sharing your knowledge!