Learn how to Convert JPG to PDF in Python
Created
May 18, 2024 05:48
-
-
Save aspose-com-gists/9f0e4e324e1d232e3be8ccc42c9b2e48 to your computer and use it in GitHub Desktop.
Convert JPG to PDF in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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