Kostas Developer

Convert a list of images to PDF document using Python

In complex information systems, we often need to process document flows and in some cases aggregate and convert documents to other file formats. These advanced techniques require lots of time and effort to develop and it becomes a headache to maintain such project infrastructure. In these cases, it is wise to use a 3rd party tool that specializes in these complex computations and provides an easily maintainable flexible solution to manipulate all sorts of structured data. That's where ConvertAPI Pyhton library comes in handy.

In this example, we will take a look at how to create a PDF document from a list of images in just a few lines of code.

First, let's install a ConvertAPI Pyhton library into our project using pip:

pip install --upgrade convertapi

Then, simply paste this code below to your project:

convertapi.api_secret = '<YOUR SECRET HERE>'
convertapi.convert('pdf', {
    Files: [
        '/path/to/image-1.png'
    ]
}, from_format = 'images').save_files('/path/to/dir')

That's it, just replace your secret and path to your file and it should work straight away.

If you want to upload a bunch of images and have them merged into one PDF document, you can pass an array of images using the same conversion endpoint. This particular conversion accepts JPG, PNG, WEBP, SVG, PSD, HEIC and TIFF images and produces a single PDF file:

convertapi.api_secret = '<YOUR SECRET HERE>'
convertapi.convert('pdf', {
    Files: [
        '/path/to/image-1.png',
        '/path/to/image-2.jpg',
        '/path/to/image-3.svg'
    ]
}, from_format = 'images').save_files('/path/to/dir')

To take full advantage of this conversion, please refer to the IMAGES to PDF converter parameters. You can configure the conversion using our Live Demo UI and grab the auto-generated code snippet from the bottom of the page.

In some cases, you might need to generate a PDF document from multiple sources like images and documents. To do that, simply convert each document to PDF and merge them into a single document using PDF -> MERGE conversion. This conversion accepts a list of files and produces a single PDF output. In order to achieve all the performance benefits, you might want to check the workflows.

Using this simple to use, yet powerful conversion service you can convert your files from and to 200+ file formats!