Skip to content

Instantly share code, notes, and snippets.

@blog-aspose-cloud
Last active July 27, 2022 19:14
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 blog-aspose-cloud/f8079f51465ae7c8dd3dca5f528f5154 to your computer and use it in GitHub Desktop.
Save blog-aspose-cloud/f8079f51465ae7c8dd3dca5f528f5154 to your computer and use it in GitHub Desktop.
JPG to PDF in Python
Learn how to convert JPG to PDF using Python SDK.
# obtain client credentials from https://dashboard.aspose.cloud/
def image2PDF():
try:
#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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment