Convert PDF to Image in Python

PDF to Image
Convert PDF to Image | Build PDF to Image converter in Python

PDF is one of the popular formats for information sharing as it preserves document fidelity on the majority of platforms. Albeit all popular web browsers support PDF format but we may have a requirement to convert PDF to Image (i.e. JPG, PNG, BMP, TIFF, etc). Therefore, in this article, we are going to focus on converting PDF to TIFF format. We are preferring TIFF because they are lossless i.e. you do not lose the quality after saving and editing the file, no matter how many times you do it.

PDF Conversion API

So in order to facilitate our customers with programmatic PDF creation, manipulation and conversion, Aspose.PDF Cloud has been developed. Furthermore, for PDF to TIFF conversion using Python, so we are going to use Aspose.PDF Cloud SDK for Python which is a wrapper around Cloud API. You may download and install the SDK from the PIP repository by executing the following command in the terminal/command prompt.

pip install asposepdfcloud

Now in order to use Cloud APIs, you need to create a free subscription account over Aspose.Cloud dashboard. If you have GitHub or Google account, simply Sign Up or, click on the Create a new Account button and obtain client credentials.

Convert PDF to TIFF using Python

When converting PDF files to TIFF format, we get the option to specify TIFF compression values such as LZW, CCITT4, CCITT3, RLE, and None. Similarly, the API also enables you to control ColorDepth where possible values can be Default, Format8bpp, Format4bpp, or Format1bpp. We may also set the orientation of the resultant image as None, Landscape, Portrait. The API provides us following three mechanisms for converting PDF to TIFF and we can select either of them as per our requirement.

  1. PDF (located on storage) to TIFF and resultant file in response
  2. PDF (located on storage) to TIFF and upload resultant file to cloud storage
  3. PDF (in request content) to TIFF and upload resultant file to cloud storage

In the following section, we are loading a PDF file from cloud storage and converting it to a TIFF image. After conversion, the resultant file is stored in cloud storage (2nd option as specified above)

  • First of all, we need to create an instance of ApiClient class that requires Client ID Client Secret as arguments
  • Secondly, create an instance of PdfApi class that requires the ApiClient object as an input argument
  • Thirdly, create variables defining input PDF and resultant TIFF names
  • Set the resolution, compression factor, color depth, margin, orientation, dimensions, and resolution details
  • Finally, call put_pdf_in_storage_to_tiff(…) method to initiate the conversion operation
PDF to Image
Image2:- PDF to TIFF conversion preview.

For your reference, we have also uploaded the input marketing.pdf and resultant.tiff.

PDF to TIFF using cURL Command

In this section, we are going to access Aspose.PDF Cloud through cURL commands. However, first, we need to generate a JSON Web Token (JWT) based on your client credential. 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=88d1cda8-b12c-4a80-b1ad-c85ac483c5c5&client_secret=406b404b2df649611e508bbcfcd2a77f" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Once the JWT Token has been generated, please execute the following command to render the PDF document to a TIFF image and save the resultant output Cloud storage.

curl -X PUT "https://api.aspose.cloud/v3.0/pdf/marketing.pdf/convert/tiff?outPath=Resultant.tiff&brightness=100&compression=LZW&colorDepth=Format8bpp&leftMargin=10&rightMargin=10&topMargin=10&bottomMargin=10&orientation=None&skipBlankPages=true&width=1024&height=800&xResolution=300&yResolution=300&pageIndex=1&pageCount=1" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-d{}

Conclusion

The article has explained the details on how to convert PDF to TIFF using Python as well as cURL commands. You may consider accessing the APIs through the swagger interfaces within the browser of your choice. Please note that we have also uploaded the complete source code of Aspose.PDF Cloud SDK for Python over GitHub. So you may consider downloading and modifying it as per your requirements. In case you encounter any issues or you have any further queries, you may also contact us via the free customer support forum.

Related Articles

We recommend visiting the following articles to learn about: