Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aspose-com-gists/9f0e4e324e1d232e3be8ccc42c9b2e48 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/9f0e4e324e1d232e3be8ccc42c9b2e48 to your computer and use it in GitHub Desktop.
Convert JPG to PDF in Python
# Path of the image file
path = "Aspose.jpg"
output_path = "ImagetoPDF.pdf"
# Initialize new PDF document
doc = pdf.Document()
# Add empty page in empty document
page = doc.pages.add()
# Create an image object
image = pdf.Image()
image.file = path
# Add image on a page
page.paragraphs.add(image)
# Save output PDF file
doc.save(output_path)
# Path of the image file
data_dir = "D:\\Files\\"
image_file = data_dir + "aspose-image.jpg"
# Initialize a new PDF document
doc = pdf.Document()
# Load image into stream
with open(image_file, "rb") as image_stream:
# Add an empty page
page = doc.pages.add()
# Create an image object
image1 = pdf.Image()
# Set the image file stream
image1.image_stream = image_stream
# Set page dimensions and margins
page.page_info.margin.bottom = 0
page.page_info.margin.top = 0
page.page_info.margin.right = 0
page.page_info.margin.left = 0
page.crop_box = image1.bitmap_size
# Add image to the page
page.paragraphs.add(image1)
# Save output PDF file
output_path = data_dir + "ImagetoPDF_stream.pdf"
doc.save(output_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment