Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Last active August 18, 2022 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/056830644553f8d23af6be2dcd8379ae to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/056830644553f8d23af6be2dcd8379ae to your computer and use it in GitHub Desktop.
How to Convert PDF to JPEG, PNG, and GIF Images in Python using Rest API
# How to convert PDF to GIF image format in Python.
# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.storage_name = storage_name
settings.file_path = "python-testing/sample-file.pdf"
settings.format = "gif"
loadOptions = groupdocs_conversion_cloud.PdfLoadOptions()
loadOptions.password = "password"
settings.load_options = loadOptions;
convertOptions = groupdocs_conversion_cloud.GifConvertOptions()
convertOptions.gray_scale = True
convertOptions.from_page = 1
convertOptions.pages_count = 1
convertOptions.quality = 100
convertOptions.rotate_angle = 90
convertOptions.use_pdf = False
settings.convert_options = convertOptions
settings.output_path = "python-testing"
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
response = convert_api.convert_document(request)
print("Successfully converted PDF to GIF format: " + str(response))

You can convert PDF to image formats programmatically on the cloud. In this article, we will learn how to convert PDF to image formats using REST API in Python.

The following topics are covered in this article:

  1. PDF to Image Conversion REST API and Python SDK
  2. How to Convert PDF to JPG/JPEG Image in Python using REST API
  3. Convert PDF to PNG File Format in Python using REST API
  4. Convert PDF to GIF Image File in Python using REST API
# API initialization to download converted file
import shutil
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Create download file request
request = groupdocs_conversion_cloud.DownloadFileRequest("python-testing\\sample-file.jpeg", storage_name)
# Download converted file
response = file_api.download_file(request)
# Move the downloaded image jpeg or jpg file to your local directory
shutil.move(response, "H:\\groupdocs-cloud-data\\")
# Upload pdf file to cloud storage
# Create an instance of the File API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Call upload file request
request = groupdocs_conversion_cloud.UploadFileRequest("python-testing\sample-file.pdf", "H:\\groupdocs-cloud-data\\sample-file.pdf", storage_name)
# Upload pdf file to the cloud
response = file_api.upload_file(request)
print(response.uploaded)
# How to convert PDF to JPG or JPEG format in Python.
try:
# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.storage_name = storage_name
settings.file_path = "python-testing/sample-file.pdf"
settings.format = "jpeg"
loadOptions = groupdocs_conversion_cloud.PdfLoadOptions()
loadOptions.password = "password"
settings.load_options = loadOptions;
convertOptions = groupdocs_conversion_cloud.JpegConvertOptions()
convertOptions.gray_scale = True
convertOptions.from_page = 1
convertOptions.pages_count = 1
convertOptions.quality = 100
convertOptions.rotate_angle = 90
convertOptions.use_pdf = False
settings.convert_options = convertOptions
settings.output_path = "python-testing"
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
response = convert_api.convert_document(request)
print("Successfully converted PDF to JPEG file format: " + str(response))
except groupdocs_conversion_cloud.ApiException as e:
print("Exception while calling API: {0}".format(e.message))
# How to convert PDF to PNG file format in Python.
# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.storage_name = storage_name
settings.file_path = "python-testing/sample-file.pdf"
settings.format = "png"
loadOptions = groupdocs_conversion_cloud.PdfLoadOptions()
loadOptions.password = "password"
settings.load_options = loadOptions;
convertOptions = groupdocs_conversion_cloud.PngConvertOptions()
convertOptions.gray_scale = True
convertOptions.from_page = 1
convertOptions.pages_count = 1
convertOptions.quality = 100
convertOptions.rotate_angle = 90
convertOptions.use_pdf = False
settings.convert_options = convertOptions
settings.output_path = "python-testing"
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
response = convert_api.convert_document(request)
print("Successfully converted PDF to PNG file format: " + str(response))
# Import Python Library in a python application to use conversion API http://api.groupdocs.cloud
import groupdocs_conversion_cloud
# Get client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
client_id = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Get File API configurations
configuration = groupdocs_conversion_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
storage_name = "LocaltStorage"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment