How to Convert Images to PDF in Python

  1. Add Files in the Folder
  2. Install the Module Requests
  3. Add your PDF.co API Key
  4. Add Source Files
  5. Run the Python Program
  6. PDF.co Web API Demo

In this demonstration, we will convert Images to PDF in Python using the PDF.co Web API. We will use the Images to PDF Web API /v1/pdf/convert/from/image endpoint. Below is a GIF of two sample images and the converted PDF output.

Sample Images and Converted PDF
Sample Images and Converted PDF Output

Step 1: Add Files in the Folder

First, add your sample file to the Python program folder. You can download our Python sample code here.

Step 2: Install the Module Requests

Next, if you haven’t yet, kindly install the requests module. Enter this line python -m pip install requests in your command line.

Step 3: Add your PDF.co API Key

Let’s, open the Python sample code and proceed to line 6. Add your PDF.co API Key in this line. You can get your API Key in your PDF.co dashboard.

PDF.co API Key

Step 4: Add Source Files

Let’s add the source files.

In line 12, add the image files you’d like to convert to PDF in an array.
In line 14, enter your desired output filename.

Source Image and PDF File Name

Step 5: Run the Python Program

You can now run the Python program and you’ll find the PDF output in your folder.

Python Program Result

Step 6: PDF.co Web API Demo

Here are the Images of PDF Web API in action.

PDF.co PNG from Images Web API in Python
PDF.co Images Web API in Python

In this tutorial, you learned how to convert images to PDF in Python using the PDF.co Web API. The Web API supports JPEG, PNG, and TIFF image formats. You also learned which part of the source code to change to get you started right away.