Convert PDF to Different Image Formats in Python

Overview

This article explains how to convert PDF to different image formats using Python. It covers the following topics.

Python Convert PDF to Image

Python Convert PDF to PNG

  1. Import the AsposePdfPython module, which provides a Python wrapper for the Aspose.PDF library.
  2. Open a PDF document using the document_open function, which takes the file name as an argument and returns a Document object.
  3. Get the pages of the document using the document_get_pages function, which takes a Document object as an argument and returns a PageCollection object.
  4. Get the desired page of the document using the page_collection_get_page function, which takes a PageCollection object and an index as arguments and returns a Page object.
  5. Create a PngDevice object using the png_device_create function, which takes no arguments. This object can convert PDF pages to PNG images with default parameters.
  6. Save the desired page of the document as a PNG image using the png_device_save_page_to_file function, which takes a PngDevice object, a Page object and a file name as arguments.
  7. Close the handles of the PngDevice and the Document objects using the close_handle function, which takes an object as an argument and releases its resources.

from AsposePdfPython import *

doc = document_open("blank_pdf_document.pdf")
pages = document_get_pages(doc)
page = page_collection_get_page(pages,1)

pngDevice = png_device_create()
png_device_save_page_to_file(pngDevice,page,"test.png")

Python Convert PDF to JPEG

  1. Open a PDF document using the document_open function, which takes the file name as an argument and returns a Document object.
  2. Get the pages of the document using the document_get_pages function, which takes a Document object as an argument and returns a PageCollection object.
  3. Get the desired page of the document using the page_collection_get_page function, which takes a PageCollection object and an index as arguments and returns a Page object.
  4. Create a Resolution object using the resolution_create function, which takes the resolution value in dots per inch (DPI) as an argument.
  5. Create a JpegDevice object using the jpeg_device_create_from_width_height_resolution function, which takes the width, height and resolution values as arguments. This object can convert PDF pages to JPEG images with the specified parameters.
  6. Save the desired page of the document as a JPEG image using the jpeg_device_save_page_to_file function, which takes a JpegDevice object, a Page object and a file name as arguments.
  7. Close the handles of the JpegDevice and the Document objects using the close_handle function, which takes an object as an argument and releases its resources.

    from AsposePdfPython import *

    doc = document_open("blank_pdf_document.pdf")
    pages = document_get_pages(doc)
    page = page_collection_get_page(pages,1)

    res = resolution_create(300)
    jpegDevice = jpeg_device_create_from_width_height_resolution(1239,1754,res)
    jpeg_device_save_page_to_file(jpegDevice,page,"test.jpeg")