Convert Images to PDF using REST API in Python

PDF is one of the popular formats for sharing and printing documents. We often need to convert documents of different formats and images to PDF. It requires lots of time and effort to develop such tools. So, it is better to use already developed specialized tools that provide an easily maintainable, flexible solution to your needs. For this purpose, image to PDF conversion REST API and Python SDK allow converting documents of supported formats to PDF programmatically on the cloud. In this article, we will learn how to convert images to PDF using a REST API in Python.

The following topics shall be covered in this article:

Image to PDF Conversion REST API and Python SDK

For converting JPG or PNG images to PDF, we will be using the Python SDK of GroupDocs.Conversion Cloud API. Please install it using the following command in the console:

pip install groupdocs_converison_cloud

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

Convert JPG to PDF using a REST API in Python

We can convert images to PDF documents by following the simple steps given below:

  1. Upload the JPG image file to the Cloud
  2. Convert JPG to PDF using Python
  3. Download the converted PDF file

Upload the Image

Firstly, we will upload the JPG image file to the cloud using the following code sample:

As a result, the uploaded file will be available in the files section of the dashboard on the cloud.

Convert JPG to PDF using Python

We can convert JPG images to PDF documents programmatically by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Next, create an instance of the ConvertSettings.
  • Then, set the JPG file path.
  • And, assign “pdf” to format.
  • Also, provide the output file path.
  • After that, create ConvertDocumentRequest with ConvertSettings as argument.
  • Finally, call the ConvertApi.convert_document() method with ConvertDocumentRequest to save the converted file.

The following code sample shows how to convert a JPG image to a PDF document using a REST API in Python.

Convert JPG to PDF using a REST API in Python.

Convert JPG to PDF using a REST API in Python.

Download the Converted PDF

The above code sample will save the converted PDF document on the cloud. It can be downloaded using the following code example:

JPG to PDF Conversion with Advanced Options

We can convert JPG to PDF document with some advanced settings programmatically by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Next, create an instance of the ConvertSettings.
  • Then, set the JPG file path.
  • And, assign “pdf” to format.
  • Also, provide the output file path.
  • Next, create an instance of the PdfConvertOptions and assign to the ConvertSettings.
  • Then, set various convert settings such as dpi, grayscale, image_quality, height, margins (top, left, right, bottom), etc.
  • After that, create ConvertDocumentRequest with ConvertSettings as argument.
  • Finally, call the ConvertApi.convert_document() method with ConvertDocumentRequest to save the converted file.

The following code example shows how to convert a JPG image to a PDF document with advanced convert options. Please follow the steps mentioned earlier to upload a JPG image file and download the converted PDF file.

JPG to PDF Conversion with Advanced Options.

JPG to PDF Conversion with Advanced Options.

Convert JPG to PDF with Watermark in Python

We can convert JPG to PDF document and then add watermark to the converted PDF programmatically by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Next, create an instance of the ConvertSettings.
  • Then, set the JPG file path, assign “pdf” to format and provide the output file path.
  • Next, create an instance of the WatermarkOptions.
  • Then, set watermark text, color, font_size, rotation_angle, etc.
  • Next, create an instance of the PdfConvertOptions and assign to the _WatermarkOptions_.
  • Then, optionally set various convert settings.
  • And, assign PdfConvertOptions to the ConvertSettings.
  • After that, create ConvertDocumentRequest with ConvertSettings as argument.
  • Finally, call the ConvertApi.convert_document() method with ConvertDocumentRequest to save the converted file.

The following code example shows how to convert JPG to a PDF document and add a watermark to the converted PDF document using a REST API in Python.

Convert JPG to PDF with Watermark in Python.

Convert JPG to PDF with Watermark in Python.

Convert JPG to PDF and Download File Directly

We can convert JPG to PDF programmatically and download the converted PDF file directly by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Next, create an instance of the ConvertSettings.
  • Then, set the JPG file path.
  • And, assign “pdf” to format.
  • Also, provide the output file path as None.
  • After that, create ConvertDocumentRequest with ConvertSettings as argument.
  • Finally, call the ConvertApi.convert_document_download() method with ConvertDocumentRequest to save the converted file on local disk.

The following code sample shows how to convert a JPG image file to a PDF document and download it directly using a REST API in Python. The API shall return the converted PDF file in response. Please follow the steps mentioned earlier to upload a file.

JPG to PDF Conversion without using Cloud Storage

We can convert JPG to PDF without using cloud storage by following the steps given below:

  • Firstly, create an instance of the ConvertApi
  • Next, create ConvertDocumentDirectRequest with target format and input image file path as arguments.
  • Then, call the convert_document_direct() method with ConvertDocumentDirectRequest as an argument.
  • Finally, save the converted output PDF file to the local path using FileStream.writeFile() method.

The following code sample shows how to convert JPG to a PDF document without using cloud storage. It means we will pass the input file in the request body and receive the output file in the API response.

Try Online

Please try the following free online JPG conversion tool, which is developed using the above API. https://products.groupdocs.app/conversion/

Conclusion

In this article, we have learned how to convert a JPG to a PDF document on the cloud. We have also seen how to convert JPG to PDF and add watermark to converted documents using Python. This article also explained how to programmatically upload a JPG image file to the cloud and then download the converted PDF file from the cloud. Besides, you can learn more about GroupDocs.Conversion Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.

See Also