Convert JPG to PDF in Python | Convert JPG into PDF

Are you tired of manually converting your JPG images to PDF files? Want to automate the process with Python? This post shows you how to convert JPG to PDF in Python.

We often prefer PDF for sharing or storing data because it preserves the original layout and formatting. PDF documents are more secure and can be password‑protected, making them a great option for sharing sensitive images. Let’s dive into converting a JPG image to a PDF document.

This article covers the following topics:

  1. Python JPG to PDF Conversion Library
  2. Convert a JPG to a PDF in Python
  3. JPG to PDF Conversion: An Alternative Approach
  4. Convert JPG to PDF Online
  5. Free Resources

Python Library to Convert JPG to PDF

For converting a JPG to a PDF document, we will use Aspose.PDF for Python. It is a robust, feature‑rich library that lets developers convert JPG images to PDF files easily. With its intuitive API, Aspose.PDF can also transform other image formats such as JPEG, PNG, and BMP into high‑quality PDFs.

Please either download the packages or install the APIs from PyPI using the following pip command:

> pip install aspose-pdf

Convert a JPG to a PDF in Python

Follow these steps to convert any JPG image to a PDF document:

  1. Create an instance of the Document class.
  2. Add a new empty page to the Document Pages collection.
  3. Create an instance of the Image class.
  4. Set the Image.file property.
  5. Add the image to the page.
  6. Save the document with the save() method.

The code sample below demonstrates the conversion:

Convert a JPG to a PDF in Python

Convert a JPG to a PDF in Python

Convert JPG to PDF in Python - Alternative Approach

You can also convert a JPG to PDF using a stream:

  1. Create an instance of the Document class.
  2. Load the image into a stream.
  3. Add a new empty page to the Document Pages collection.
  4. Create an instance of the Image class.
  5. Assign the image stream to the image_stream property.
  6. (Optional) Specify page dimensions, margins, and a crop box.
  7. Add the image to the page.
  8. Save the document with the save() method.

The following code shows how to perform the conversion using a stream:

Convert JPG to PDF in Python - Alternative Approach

Convert JPG to PDF in Python - Alternative Approach

Start Converting JPG to PDF for free!

Visit our Temporary License page to get a complimentary, unrestricted license and unlock the full potential of Aspose.PDF for Python with no limitations!

Convert JPG to PDF Online

You can also convert JPG images to PDF online using our high‑quality and free JPG to PDF converter web app. The app uses the same powerful library, so no installation or coding is required. Just upload your JPG file and download the PDF in seconds!

Image

JPG to PDF – Free Resources

Explore more about creating, manipulating, and converting PDF documents with these resources:

Conclusion

In this article, we have learned how to convert a JPG to a PDF in Python. By following the steps above, you can add this capability to your Python applications and build your own JPG‑to‑PDF converter tool. If you have questions, let us know via our free support forum.

See Also