Convert Images to PDF using REST API in Python

PDF is one of the popular formats for sharing and printing documents. We often need to convert documents of different formats and images to PDF. It requires lots of time and effort to develop such tools. So, it is better to use already developed specialized tools that provide an easily maintainable, flexible solution to your needs. For this purpose, image to PDF conversion REST API and Python SDK allow converting documents of supported formats to PDF programmatically on the cloud. In this article, we will learn how to convert images to PDF using a REST API in Python.

The following topics shall be covered in this article:

Image to PDF Conversion REST API and Python SDK

For converting JPG or PNG images to PDF, we will be using the Python SDK of GroupDocs.Conversion Cloud API. Please install it using the following command in the console:

pip install groupdocs_converison_cloud

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

# This code example demonstrates how to set client ID and secret.
client_id = "659fe7da-715b-4744-a0f7-cf469a392b73"
client_secret = "b377c36cfa28fa69960ebac6b6e36421"
configuration = groupdocs_conversion_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
my_storage = ""

Convert JPG to PDF using a REST API in Python

We can convert images to PDF documents by following the simple steps given below:

  1. Upload the JPG image file to the Cloud
  2. Convert JPG to PDF using Python
  3. Download the converted PDF file

Upload the Image

Firstly, we will upload the JPG image file to the cloud using the following code sample:

# This code example demonstrates how to upload JPG image file to the Cloud.
# Create instance of the API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# upload file request
request = groupdocs_conversion_cloud.UploadFileRequest("sample.jpg", "C:\\Files\\Conversion\\sample.jpg", my_storage)
# upload sample file
response = file_api.upload_file(request)

As a result, the uploaded file will be available in the files section of the dashboard on the cloud.

Convert JPG to PDF using Python

We can convert JPG images to PDF documents programmatically by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Next, create an instance of the ConvertSettings.
  • Then, set the JPG file path.
  • And, assign “pdf” to format.
  • Also, provide the output file path.
  • After that, create ConvertDocumentRequest with ConvertSettings as argument.
  • Finally, call the ConvertApi.convert_document() method with ConvertDocumentRequest to save the converted file.

The following code sample shows how to convert a JPG image to a PDF document using a REST API in Python.

# This code example demonstrates how to comvert JPT to PDF.
# Create necessary API instances
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.jpg"
settings.format = "pdf"
settings.output_path = "converted"
# Convert request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# convert
result = convert_api.convert_document(request)
# Done
print("Document converted: " + result[0].path)
Convert JPG to PDF using a REST API in Python.

Convert JPG to PDF using a REST API in Python.

Download the Converted PDF

The above code sample will save the converted PDF document on the cloud. It can be downloaded using the following code example:

# This code example demonstrates how to download PDF file from the Cloud.
# API initialization
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Download file request
request = groupdocs_conversion_cloud.DownloadFileRequest("converted\\sample.pdf", my_storage)
# Download file
response = file_api.download_file(request)
# Move downloaded file to your working directory
shutil.move(response, "C:\\Files\\Conversion\\")

JPG to PDF Conversion with Advanced Options

We can convert JPG to PDF document with some advanced settings programmatically by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Next, create an instance of the ConvertSettings.
  • Then, set the JPG file path.
  • And, assign “pdf” to format.
  • Also, provide the output file path.
  • Next, create an instance of the PdfConvertOptions and assign to the ConvertSettings.
  • Then, set various convert settings such as dpi, grayscale, image_quality, height, margins (top, left, right, bottom), etc.
  • After that, create ConvertDocumentRequest with ConvertSettings as argument.
  • Finally, call the ConvertApi.convert_document() method with ConvertDocumentRequest to save the converted file.

The following code example shows how to convert a JPG image to a PDF document with advanced convert options. Please follow the steps mentioned earlier to upload a JPG image file and download the converted PDF file.

# This code example demonstrates how to comvert JPT to PDF with Advanced options.
# Create necessary API instances
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.jpg"
settings.format = "pdf"
settings.output_path = "converted_with_options.pdf"
# PDF convert options
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.center_window = True
convertOptions.display_doc_title = True
convertOptions.dpi = 1024.0
convertOptions.fit_window = False
convertOptions.grayscale = True
convertOptions.image_quality = 100
convertOptions.linearize = True
convertOptions.margin_top = 500
convertOptions.margin_left = 500
convertOptions.margin_right = 500
convertOptions.margin_bottom = 500
settings.convert_options = convertOptions
# Convert request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# convert
result = convert_api.convert_document(request)
# Done
print("Document converted: " + result[0].path)
JPG to PDF Conversion with Advanced Options.

JPG to PDF Conversion with Advanced Options.

Convert JPG to PDF with Watermark in Python

We can convert JPG to PDF document and then add watermark to the converted PDF programmatically by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Next, create an instance of the ConvertSettings.
  • Then, set the JPG file path, assign “pdf” to format and provide the output file path.
  • Next, create an instance of the WatermarkOptions.
  • Then, set watermark text, color, font_size, rotation_angle, etc.
  • Next, create an instance of the PdfConvertOptions and assign to the _WatermarkOptions_.
  • Then, optionally set various convert settings.
  • And, assign PdfConvertOptions to the ConvertSettings.
  • After that, create ConvertDocumentRequest with ConvertSettings as argument.
  • Finally, call the ConvertApi.convert_document() method with ConvertDocumentRequest to save the converted file.

The following code example shows how to convert JPG to a PDF document and add a watermark to the converted PDF document using a REST API in Python.

# This code example demonstrates how to comvert JPT to PDF and add watermark to converted PDF.
# Api initialization
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.jpg"
settings.format = "pdf"
settings.output_path = "converted"
# Define watermark options
watermark = groupdocs_conversion_cloud.WatermarkOptions()
watermark.text = "THIS IS SAMPLE WATERMARK"
watermark.bold = True;
watermark.font_size = 34;
watermark.color = "Black";
watermark.background = False;
watermark.rotation_angle = 30;
watermark.left = 90;
watermark.top = 500;
# Define PDF convert options
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.watermark_options = watermark
settings.convert_options = convertOptions
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert
result = convert_api.convert_document(request)
print("Document converted successfully: " + result[0].url)
Convert JPG to PDF with Watermark in Python.

Convert JPG to PDF with Watermark in Python.

Convert JPG to PDF and Download File Directly

We can convert JPG to PDF programmatically and download the converted PDF file directly by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Next, create an instance of the ConvertSettings.
  • Then, set the JPG file path.
  • And, assign “pdf” to format.
  • Also, provide the output file path as None.
  • After that, create ConvertDocumentRequest with ConvertSettings as argument.
  • Finally, call the ConvertApi.convert_document_download() method with ConvertDocumentRequest to save the converted file on local disk.

The following code sample shows how to convert a JPG image file to a PDF document and download it directly using a REST API in Python. The API shall return the converted PDF file in response. Please follow the steps mentioned earlier to upload a file.

# This code example demonstrates how to covvert and download.
# Create necessary API instances
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.jpg"
settings.format = "pdf"
settings.output_path = None # leave OutputPath will result the output as document IOStream
# Prepare request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert
response = convert_api.convert_document_download(request)
# Move downloaded file to your working directory
shutil.move(response, "C:\\Files\\Conversion\\")

JPG to PDF Conversion without using Cloud Storage

We can convert JPG to PDF without using cloud storage by following the steps given below:

  • Firstly, create an instance of the ConvertApi
  • Next, create ConvertDocumentDirectRequest with target format and input image file path as arguments.
  • Then, call the convert_document_direct() method with ConvertDocumentDirectRequest as an argument.
  • Finally, save the converted output PDF file to the local path using FileStream.writeFile() method.

The following code sample shows how to convert JPG to a PDF document without using cloud storage. It means we will pass the input file in the request body and receive the output file in the API response.

# This code example demonstrates how to comvert JPT to PDF without using Cloud storage.
# Create necessary API instances
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare request
request = groupdocs_conversion_cloud.ConvertDocumentDirectRequest("pdf", "C:\\Files\\Conversion\\sample.jpg")
# Convert
result = convert_api.convert_document_direct(request)
# Move downloaded file to your working directory
shutil.move(result, "C:\\Files\\Conversion\\")

Try Online

Please try the following free online JPG conversion tool, which is developed using the above API. https://products.groupdocs.app/conversion/

Conclusion

In this article, we have learned how to convert a JPG to a PDF document on the cloud. We have also seen how to convert JPG to PDF and add watermark to converted documents using Python. This article also explained how to programmatically upload a JPG image file to the cloud and then download the converted PDF file from the cloud. Besides, you can learn more about GroupDocs.Conversion Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also