Convert PDF to Image Formats in Python

Python Convert PDF to Image

Aspose.PDF for Python via .NET supports several ways to convert PDF content to images. In practice, most workflows use one of two options:

  • the Device approach for rendering PDF pages to raster image formats
  • the SaveOptions approach for exporting PDF content to SVG

This article shows how to convert PDF files to TIFF, BMP, EMF, JPEG, PNG, GIF, and SVG.

The library includes multiple rendering classes. DocumentDevice is designed for whole-document conversion, while ImageDevice targets individual pages.

Convert PDF using DocumentDevice class

Use DocumentDevice when you want to render the entire PDF into a single multi-page TIFF file.

The TiffDevice class is based on DocumentDevice and provides the process method for converting all pages in a PDF file into one TIFF output.

Convert PDF Pages to One TIFF Image

Aspose.PDF for Python via .NET can render every page in a PDF file into one TIFF image.

Steps: Convert PDF to TIFF in Python

  1. Load the source PDF with the Document class.
  2. Create TiffSettings and TiffDevice objects.
  3. Configure TIFF options such as compression, color depth, and blank-page handling.
  4. Call the process method to write the TIFF file.

The following code snippet shows how to convert all the PDF pages to a single TIFF image.

import aspose.pdf as ap
from io import FileIO
from os import path
import sys

def convert_PDF_to_TIFF(infile, outfile):
    document = ap.Document(infile)

    resolution = ap.devices.Resolution(300)
    tiffSettings = ap.devices.TiffSettings()
    tiffSettings.compression = ap.devices.CompressionType.LZW
    tiffSettings.depth = ap.devices.ColorDepth.DEFAULT
    tiffSettings.skip_blank_pages = False

    tiffDevice = ap.devices.TiffDevice(resolution, tiffSettings)
    tiffDevice.process(document, f"{outfile}.tiff")

    print(infile + " converted into " + outfile)

Convert PDF using ImageDevice class

Use ImageDevice when you need page-by-page output in a raster image format.

ImageDevice is the base class for BmpDevice, JpegDevice, GifDevice, PngDevice, and EmfDevice.

  • The BmpDevice class allows you to convert PDF pages to BMP images.
  • The EmfDevice class allows you to convert PDF pages to EMF images.
  • The JpegDevice class allows you to convert PDF pages to JPEG images.
  • The PngDevice class allows you to convert PDF pages to PNG images.
  • The GifDevice class allows you to convert PDF pages to GIF images.

The workflow is the same for each format: load the document, create the appropriate device, then process the required page.

BmpDevice exposes the process method to render a specific page as BMP. The other image device classes follow the same pattern, so you can switch formats by changing the device class.

The following links and code samples show how to render PDF pages to common image formats:

Steps: PDF to Image (BMP, EMF, JPG, PNG, GIF) in Python

  1. Load the PDF file with the Document class.
  2. Create an instance of the required ImageDevice subclass:
  3. Loop through the pages you want to export.
  4. Call the ImageDevice.process() method to save each page as an image.

Convert PDF to BMP

import aspose.pdf as ap
from io import FileIO
from os import path
import sys

def convert_PDF_to_BMP(infile, outfile):
    document = ap.Document(infile)
    resolution = ap.devices.Resolution(300)
    device = ap.devices.BmpDevice(resolution)
    page_count = 1
    while page_count <= len(document.pages):
        image_stream = FileIO(outfile + str(page_count) + "_out.bmp", "w")
        device.process(document.pages[page_count], image_stream)
        image_stream.close()
        page_count = page_count + 1

    print(infile + " converted into " + outfile)

Convert PDF to EMF

import aspose.pdf as ap
from io import FileIO
from os import path
import sys

def convert_PDF_to_EMF(infile, outfile):
    document = ap.Document(infile)
    resolution = ap.devices.Resolution(300)
    device = ap.devices.EmfDevice(resolution)
    page_count = 1
    while page_count <= len(document.pages):
        image_stream = FileIO(outfile + str(page_count) + "_out.emf", "w")
        device.process(document.pages[page_count], image_stream)
        image_stream.close()
        page_count = page_count + 1

    print(infile + " converted into " + outfile)

Convert PDF to JPEG

import aspose.pdf as ap
from io import FileIO
from os import path
import sys

def convert_PDF_to_JPEG(infile, outfile):
    document = ap.Document(infile)
    resolution = ap.devices.Resolution(300)
    device = ap.devices.JpegDevice(resolution)
    page_count = 1

    while page_count <= len(document.pages):
        image_stream = FileIO(outfile + str(page_count) + "_out.jpeg", "w")
        device.process(document.pages[page_count], image_stream)
        image_stream.close()
        page_count = page_count + 1

    print(infile + " converted into " + outfile)

Convert PDF to PNG

import aspose.pdf as ap
from io import FileIO
from os import path
import sys

def convert_PDF_to_PNG(infile, outfile):
    document = ap.Document(infile)
    resolution = ap.devices.Resolution(300)

    device = ap.devices.PngDevice(resolution)
    page_count = 1
    while page_count <= len(document.pages):
        image_stream = FileIO(outfile + str(page_count) + "_out.png", "w")
        device.process(document.pages[page_count], image_stream)
        image_stream.close()
        page_count = page_count + 1

Convert PDF to PNG with default font

import aspose.pdf as ap
from io import FileIO
from os import path
import sys

def convert_PDF_to_PNG_with_default_font(infile, outfile):
    document = ap.Document(infile)
    resolution = ap.devices.Resolution(300)

    rendering_options = ap.RenderingOptions()
    rendering_options.default_font_name = "Arial"

    device = ap.devices.PngDevice(resolution)
    device.rendering_options = rendering_options

    page_count = 1
    while page_count <= len(document.pages):
        image_stream = FileIO(outfile + str(page_count) + "_out.png", "w")
        device.process(document.pages[page_count], image_stream)
        image_stream.close()
        page_count = page_count + 1

Convert PDF to GIF

import aspose.pdf as ap
from io import FileIO
from os import path
import sys

def convert_PDF_to_GIF(infile, outfile):
    document = ap.Document(infile)
    resolution = ap.devices.Resolution(300)
    device = ap.devices.GifDevice(resolution)
    page_count = 1
    while page_count <= len(document.pages):
        image_stream = FileIO(outfile + str(page_count) + "_out.gif", "w")
        device.process(document.pages[page_count], image_stream)
        image_stream.close()
        page_count = page_count + 1

    print(infile + " converted into " + outfile)

Convert PDF using SaveOptions class

Use SaveOptions when you want to export PDF content to SVG.

Scalable Vector Graphics (SVG) is an XML-based format for two-dimensional vector graphics. Because SVG remains vector-based, it is useful when you need scalable output for web, UI, or design workflows.

SVG files are text-based, searchable, and easy to post-process in other tools.

Aspose.PDF for Python via .NET can both convert SVG to PDF and export PDF pages to SVG. For PDF-to-SVG conversion, create a SvgSaveOptions object and pass it to the document.save() method.

The following steps show how to convert a PDF file to SVG with Python.

Steps: Convert PDF to SVG in Python

  1. Load the source PDF using the Document class.
  2. Create a SvgSaveOptions object and configure the required options.
  3. Call the document.save() method with the SvgSaveOptions instance to write the SVG output.

Convert PDF to SVG

import aspose.pdf as ap
from io import FileIO
from os import path
import sys

def convert_PDF_to_SVG(infile, outfile):
    document = ap.Document(infile)

    save_options = ap.SvgSaveOptions()
    save_options.compress_output_to_zip_archive = False
    save_options.treat_target_file_name_as_directory = True

    document.save(f"{outfile}.svg", save_options)