Convert Image to PDF in Python

PDF is one of the ruling file formats, which is primarily used for sharing and printing documents. Often, you prefer to convert the documents/files of different formats to PDF before sharing. In specific cases, you need to convert PNG, JPG, and other images to PDF format programmatically. To achieve that, this article covers how to convert an image to a PDF in Python. You will also learn about the conversion of multiple images to a single PDF.

Python Image to PDF Converter Library - Free Download

Aspose.Words for Python is a powerful and feature-rich library that lets you work with text documents including Word, PDF, etc. It also offers a high-fidelity conversion of text documents into various popular formats. We will use this library to convert images to PDFs in this article. You can install the library from PyPI using the following pip command.

> pip install aspose-words

Convert an Image to PDF in Python

The following are the steps to convert an image to a PDF file in Python.

  • First, create a new document using Document class.
  • Then, create a DocumentBuilder object and initialize it with the Document object.
  • Insert image into the document using DocumentBuilder.insert_image(fileName) method.
  • Finally, save the PDF document using Document.save(fileName) method.

The following code sample shows how to convert a PNG image to a PDF in Python.

Convert Multiple Images to a PDF in Python

The following are the steps to convert multiple images to a single PDF file in Python.

  • First, create a new document using Document class.
  • Then, create a DocumentBuilder object and initialize it with the Document object.
  • Get the list of the image files from the desired folder.
  • Loop through the list of the image files and insert each image into the document using DocumentBuilder.insert_image(fileName) method.
  • Finally, save the PDF document using Document.save(fileName) method.

The following code sample shows the conversion of multiple images to a single PDF in Python.

Image to PDF Python Converter - Get a Free License

You can convert images to PDF without evaluation limitations by getting a free temporary license.

Python Image to PDF Library - Read More

You can explore more about Aspose.Words for Python using documentation. In case you would have any queries, feel free to let us know via our forum.

Conclusion

In this article, you have learned how to convert an image to PDF format in Python. Also, you have seen how to convert multiple images to a single PDF programmatically. You can easily integrate the provided code samples into your application and perform the image-to-PDF conversion.

See Also