How to Convert JPG to PDF in Python

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

In this demonstration, we will convert JPG to PDF in Python using the Web API. We will use the Image 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 Folder

First, add your sample file to 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 API Key

Let’s, open the Python sample code and proceed to line 6. Add your API Key in this line. You can get your API Key in your dashboard. 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: Web API Demo

Here are the Images to PDF Web API in action. PNG from Images Web API in Python PNG from Images Web API in Python

In this tutorial, you learned how to convert images to PDF in Python using the 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.

Related Pages:

Related Samples: