JPEG to PDF – Convert JPEG to PDF in Python

JPEG to PDF conversion

The JPG or JPEG images are among popular rater 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. However, if we need to share the bulk images, then conversion as a single PDF file seems to be a viable solution. In this article, we are going to discuss the details on how to convert JPEG to PDF in Python.

PDF Processing API

When dealing with PDF files processing, Aspose.PDF Cloud is an amazing solution providing the capabilities to create, edit and transform PDF files to various supported formats. At the same time, it also enables you to load a variety of file formats including EPUB, PS, SVG, XPS, JPEG, etc and transform them to PDF format. No software download or installation is necessary and perform all the processing in the cloud. As 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.

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

 pip install asposepdfcloud

Aspose.Cloud Dashboard

After the installation, the next major step is a 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 and provide the required information. Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

client credentials
Image 1:- Client credentials preview on the dashboard.

JPEG to PDF using Python

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

  • First, we need to create an instance of ApiClient class while providing Client ID Client Secret as arguments
  • Secondly, create an instance of PdfApi class which takes ApiClient object as input argument
  • Thirdly, create an instance of ImageTemplatesRequest class where we define name of source image, and related properties such as width, height, margin detials etc
  • Finally, call the put_image_in_storage_to_pdf(..) method of PdfApi to perform the conversion
def image2PDF():
        #Client credentials
        client_secret = "1c9379bb7d701c26cc87e741a29987bb"
        client_id = "bbf94a2c-6d7e-4020-b4d2-b9809741374e"

        #initialize PdfApi client instance using client credetials
        pdf_api_client = asposepdfcloud.api_client.ApiClient(client_secret, client_id)

        # create PdfApi instance while passing PdfApiClient as argument
        pdf_api = PdfApi(pdf_api_client)

        #source image file
        input_file = 'source.jpg'

        #resultant PDF document
        resultant_file = 'Resultant.pdf'

        image_templates_details = asposepdfcloud.ImageTemplatesRequest
            "IsOCR": True,
            "OCRLangs": "eng",
            "ImagesList": [
                "ImagePath": input_file,
                "ImageSrcType": "ImageSrcType.Common",
                "LeftMargin": 10,
                "RightMargin": 10,
                "TopMargin": 10,
                "BottomMargin": 10,
                "PageWidth": 800,
                "PageHeight": 1000,
                "MarginInfo": {
                    "Left": 10,
                    "Right": 10,
                    "Top": 10,
                    "Bottom": 10
        # call the API to convert image to PDF format
        response = pdf_api.put_image_in_storage_to_pdf(name=resultant_file, image_templates= image_templates_details)

        # print message in console (optional)
        print('Image successfully converted to PDF format !')    
    except ApiException as e:
        print("Exception while calling PdfApi: {0}".format(e))
        print("Code:" + str(e.code))
        print("Message:" + e.message)

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

Image 2 PDF conversion preview
Image 2:- Image to PDF conversion preview.

Convert JPEG to PDF using cURL Command

The cURL commands provide a convenient way of accessing REST APIs through command line terminal. Since Aspose.PDF Cloud is developed as per REST architecture, so we can also access them via cURL commands. Now the first step is to create a JSON Web Token (JWT) based on your individual client credentials specified over Aspose.Cloud dashboard. It is mandatory because our APIs are only accessible to registered users. 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 JPEG image to PDF format. The resultant file is 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 JPEG file and resultant PDF document are uploaded over source.jpg and Image2PDF-Resultant.pdf.


In this article, we have discussed the steps of converting raster images to PDF format using Python code snippet and through cURL command. Furthermore, as our Cloud SDKs are developed under 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.

Related Articles

We also recommend visiting the following links to learn more about