5

In python 3, I have a list of images of various formats (pdf, png, jpg, gif), and I'm merging them all into one multi-page pdf.

Using PyPDF2, PDF files can be merged. But png, jpg, etc, are not supported. This is very well covered here: Merge PDF files

Using img2pdf, image types png, jpg, etc, can be converted into PDF and merged. However, it doesn't support input PDF files. Here: Create PDF from a list of images

So, since I can have PDFs, PNGs, JPGs as input, I have use to handle it like this:

from PyPDF2 import PdfFileMerger
import img2pdf

if not ext == 'pdf':
    with open("output.pdf", "wb") as f:
        f.write(img2pdf.convert(images))
else:
    merger = PdfFileMerger()
    for pdf in images:    
        merger.append(pdf)
    merger.write("output.pdf")

Question is: do I need these 2 libraries to merge a list of images, including PDFs, into one PDF? Put it another way, is there a library that can have any image as input, including PDFs, and merge them all into one PDF?

Raf
  • 1,628
  • 3
  • 21
  • 40

0 Answers0