Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert Images to PDF using a REST API in Python.

Learn how to convert images to PDF using a REST API in Python: https://blog.groupdocs.cloud/2021/12/03/convert-images-to-pdf-using-rest-api-in-python/

The following topics shall be covered in this article:

  1. Image to PDF Conversion REST API and Python SDK
  2. Convert JPG to PDF using a REST API in Python
  3. JPG to PDF Conversion with Advanced Options
  4. Convert JPG to PDF without using Cloud Storage
  5. Convert JPG to PDF with Watermark in Python
# 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 = ""
# 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\\")
# 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)
# 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\\")
# 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\\")
# 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)
# 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)
# 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment