PDF to Image – Convert PDF to JPEG in Python

PDF to Image conversion

In this article, we are going to discuss the simplest approach of converting PDF pages to Image formats using Python SDK as well as through cURL commands. No additional software download or installation is required and perform all the conversions within the cloud. Our Cloud API is so remarkable that it only requires input PDF and destination path and takes care of all internal conversion complexities.

PDF REST API

Aspose.PDF Cloud is built on top of REST architecture enabling our customers to create, edit, manipulate PDF content and transform them to various support formats. So as per our requirements, we can use the same API for PDF to Image conversion. Also, please note that in order to provide the PDF manipulation capabilities to Python programmers, we have specifically created a programming SDK named Apsose.PDF Cloud SDK for Python which is a wrapper around Aspose.PDF Cloud API. Now in order to use the SDK, the first step is its installation. The SDK is available for free download over PIP and GitHub repository. Now execute the following command on the terminal/command prompt to install the latest version of SDK on the system.

pip install asposepdfcloud

Free Cloud Dashboard Account

The next major step is a free subscription to our cloud services via Aspose.Cloud dashboard. The purpose of this subscription is to only allow authorized persons to access our file processing services. If you have GitHub or Google account, simply Sign Up or, click on the create a new Account button and provide the required information. Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

Client credentials
Image 1:- Client Credentials on Aspose.Cloud Dashboard.

Convert PDF to JPEG in Python

Please follow the steps given below to convert a PDF file available in Cloud storage to JPEG format and save the output in cloud storage.

  • Create an instance of ApiClient class while providing Client ID Client Secret as arguments
  • Now create an instance of PdfApi class which takes ApiClient object as input argument
  • Next step is to create variables holding names of input PDF file and resultant JPEG image
  • Finally, call put_page_convert_to_jpeg(..) method of PdfApi taking input PDF, page of number to be converted and name of resultant JPEG file name.
PDF to JPEG preview
Image 2:- PDF to JPEG conversion preview.

The sample files used in the above example can be downloaded from URL2PDF.pdf and PDF2JPEG.jpeg.

PDF to Image using cURL Command

The cURL command can also be used to access REST APIs. So as per our requirements, we are going to convert the first page of the PDF file to JPEG format and the resultant file is saved to Cloud storage. However, in order to access Aspose.PDF Cloud, we need to first generate a JSON Web Token (JWT) based on your individual client credentials specified over Aspose.Cloud dashboard. It is mandatory because our APIs are only accessible to registered users. Please execute the following command to generate the JWT token.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bbf94a2c-6d7e-4020-b4d2-b9809741374e&client_secret=1c9379bb7d701c26cc87e741a29987bb" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Now that we have the JWT token, please execute the following command to perform the conversion of the PDF page to JPEG format.

curl -v -X PUT "https://api.aspose.cloud/v3.0/pdf/URL2PDF.pdf/pages/1/convert/jpeg?outPath=ConvertedPage.jpeg&width=800&height=1000" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-d{}
PDF page to JPEG
Image 3:- First-page conversion to JPEG.

Apart from JPEG format, you may also convert PDF pages to other raster images using the following APIs.

Conclusion

This article has explained the steps and related details on how we can convert PDF pages to JPEG and other raster image formats. We have explored the conversion using Python SDK as well as through cURL commands. At the same time, we recommend you to visit the Programmers guide to learn more about other exciting features offered by the API. Please note that the complete source code of Apsose.PDF Cloud SDK for Python is available for download under the MIT license over GitHub.

In case you encounter any issues while using the API or you have any further queries, please feel free to contact us via the Free product support forum.

Related Articles

We also recommend visiting the following links to learn more about