2

I am wondering if there is an easy way to combine multiple png images into a single pdf in python. I want each image to be a single page in the pdf. Is pypdf the best library to use for this? Any help would be greatly appreciated.

Thanks!

English Grad
  • 1,365
  • 5
  • 21
  • 40

5 Answers5

3

I've got the same problem.

So I've created python function to unite multiple pictures in one pdf. Code is available at my github page on https://github.com/wizard1989/Unite-multiple-pictures-into-pdf. It uses "reportlab".

Code is based on answers from the following links:
Create PDF from a list of images
Combining multiple pngs in a single pdf in python
png images to one pdf in python
How can I convert all JPG files in a folder to PDFs and combine them? https://www.blog.pythonlibrary.org/2012/01/07/reportlab-converting-hundreds-of-images-into-pdfs/

Here is example of how to unite images into pdf.

We have folder "D:\pictures" with pictures of types png and jpg. We want to create file pdf_with_pictures.pdf out of them and save it in the same folder.

outputPdfName = "pdf_with_pictures"
pathToSavePdfTo = "D:\\pictures"
pathToPictures = "D:\\pictures"
splitType = "none"
numberOfEntitiesInOnePdf = 1
listWithImagesExtensions = ["png", "jpg"]
picturesAreInRootFolder = True
nameOfPart = "volume"

unite_pictures_into_pdf(outputPdfName, pathToSavePdfTo, pathToPictures, splitType, numberOfEntitiesInOnePdf, listWithImagesExtensions, picturesAreInRootFolder, nameOfPart)
3
from PIL import Image

image1 = Image.open(r'C:/Users/uidg3972/Desktop/New folder/Gopi24.jpg')
image2 = Image.open(r'C:/Users/uidg3972/Desktop/New folder/Gopi-1.jpg')
image3 = Image.open(r'C:/Users/uidg3972/Desktop/New folder/SNP_1291.jpg')

im1 = image1.convert('RGB')
im2 = image2.convert('RGB')
im3 = image3.convert('RGB')

imagelist = [im1,im2,im3]

im1.save(r'C:/Users/uidg3972/Desktop/New folder/mergedImages.pdf',save_all=True, 
append_images=imagelist)
GOPI A.R
  • 61
  • 2
  • Do try the above code, i just tried it. It worked well..... Path of the image should be '/' not '\' symbol. Usually we make a mistake in backslash button try with forward slash..! (Same as URL links in any of web-pages kind of) – GOPI A.R Aug 25 '20 at 07:54
  • ! pip install pillow and ! pip install image and finally you need .... ! pip in stall img2pdf to run the above code smoothly....! let me if any questions or doubts..!! This is my first contribution, please support and motivate. Thanks .... – GOPI A.R Aug 25 '20 at 07:58
  • 1
    should be imagelist = [im2,im3] otherwise im1 is duplicated – Chachni Jul 02 '21 at 20:17
2

I've recently found img2pdf, very useful and easy to use... the code is so simply as following (and you can complicate it with some others additional parameters):

img2pdf *png -o output_all_pngs.pdf

I hope it helps you!

jgarces
  • 519
  • 5
  • 17
1

There's a python port to WKHtmlToPdf:

https://pypi.python.org/pypi/wkhtmltopdf/0.1

Easy to create page breaks between img tags in an html doc using css which you can pass to this lib.

http://wkhtmltopdf.org/usage/wkhtmltopdf.txt

jenson-button-event
  • 18,101
  • 11
  • 89
  • 155
0

This python code converts multiple png images to single pdf in A4 format:

i = 0
image_list = []
for x in range(0,5):
    image = Image.open(f'/content/image{i}.png')
    image = image.resize((1250,800))
    im = Image.new('RGB',
              (1240 , 1754), 
              (255, 255, 255))
    im.paste(image,image.getbbox())
    image_list.append(im)
    i+=1

image_list[0].save(r'/content/pdfs.pdf', 'PDF', quality=100, 
save_all=True, append_images=image_list[1:])