Learn how to convert JPG to PDF in Python

The JPG or JPEG images are among popular raster images as they use a complex lossy compression algorithm enabling the users to create smaller graphics. The majority of devices including desktop, mobile, and other handheld devices support JPG images. Now if we need to share the bulk images, then conversion of JPG to PDF seems to be a viable solution. In this article, we are going to discuss the details on how to convert JPG to PDF in Python.

We recently developed a free Text to GIF service, which you can use to create interesting animations from simple texts.

JPG to PDF Conversion API

The scope of this article is confined to the Python language, so we are going to emphasize the utilization of Aspose.PDF Cloud SDK for Python. It is a wrapper around Cloud REST API and enables you to perform all PDF file processing capabilities within the Python applications. It provides the capabilities to create, edit and transform PDF files into various supported formats. You may also load a variety of files including EPUB, PS, SVG, XPS, JPEG, etc, and transform them into PDF.

Now the first step is the installation of SDK which is available for download over PIP and GitHub repository. Please execute the following command on the terminal/command prompt to install the latest version of SDK on the system.

 pip install asposepdfcloud

After the installation, the next major step is the free subscription to our cloud services via Aspose.Cloud dashboard. The purpose of this subscription is to only allow authorized persons to access our file processing services. If you have GitHub or Google account, simply Sign Up or, click on the Create a new Account button.

Convert JPG to PDF in Python

The conversion using a python code snippet is quite handy. Please follow the steps defined below to accomplish this requirement.

  • Firstly, create an instance of ApiClient class while providing Client ID & Client Secret as arguments
  • Secondly, create an object of PdfApi class that takes the ApiClient object as an input argument
  • Thirdly, create an instance of ImageTemplatesRequest class where we define the name of the source image, and related properties such as width, height, margin details
  • Finally, call the put_image_in_storage_to_pdf(..) method of PdfApi class to perform the conversion

Incase we need to convert more than one image in one call, please specify more instances of ImagesList within ImageTemplatesRequest object.

JPG to PDF conversion preview

Image 1:- JPG to PDF conversion preview.

JPG to PDF using cURL Commands

The cURL commands provide a convenient way to access REST APIs over the command line terminal. Since Aspose.PDF Cloud is developed as per REST architecture, so we can also access it via cURL commands. Now the first step is to create a JSON Web Token (JWT) based on your individual client credentials. So please execute the following command to generate the JWT token.

curl -v "" \
-d "grant_type=client_credentials&client_id=bbf94a2c-6d7e-4020-b4d2-b9809741374e&client_secret=1c9379bb7d701c26cc87e741a29987bb" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Once the JWT token has been generated, please use the following cURL command to convert the JPG to PDF format. The resultant file is then uploaded to cloud storage.

curl -v -X PUT "" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "{  \"IsOCR\": true,  \"OCRLangs\": \"eng\",  \"ImagesList\": [    {      \"ImagePath\": \"source.jpg\",      \"ImageSrcType\": \"Common\",      \"LeftMargin\": 10,      \"RightMargin\": 10,      \"TopMargin\": 10,      \"BottomMargin\": 10,      \"PageWidth\": 800,      \"PageHeight\": 1000,      \"MarginInfo\": {        \"Left\": 10,        \"Right\": 10,        \"Top\": 10,        \"Bottom\": 10      }    }  ]}"

For your reference, the source JPG file and a resultant PDF document are uploaded over source.jpg and Image2PDF-Resultant.pdf.


In this article, we have discussed the steps to convert JPG to PDF using Python. While following the same instructions, we can also convert PNG, BMP, and GIF images to PDF. We have also learned about the approach for converting JPG to PDF using cURL commands. Nevertheless, our Cloud SDKs are developed under an MIT license, so their complete code snippet is available for free download over GitHub.

Should you have any related queries or you encounter any issues while using our APIs, please feel free to contact us via the free customer support forum.

We also recommend visiting the following links to learn more about