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.

Image Format: TIFF

Image Format: BMP

Image Format: EMF

Image Format: JPG

Image Format: PNG

Image Format: GIF

Image Format: SVG

Python Convert PDF to Image

Aspose.PDF for Python uses several approaches to convert PDF to image. Generally speaking, we use two approaches: conversion using the Device approach and conversion using SaveOption. This section will show you how to convert PDF documents to image formats such as BMP, JPEG, GIF, PNG, EMF, TIFF, and SVG formats using one of those approaches.

There are several classes in the library that allow you to use a virtual device to transform images. DocumentDevice is oriented for conversion whole document, but ImageDevice - for a particular page.

Convert PDF using DocumentDevice class

Aspose.PDF for Python makes a possible to convert PDF Pages to TIFF images.

The TiffDevice (based on DocumentDevice) class allows you to convert PDF pages to TIFF images. This class provides a method named process which allows you to convert all the pages in a PDF file to a single TIFF image.

Convert PDF Pages to One TIFF Image

Aspose.PDF for Python explain how to convert all pages in a PDF file to a single TIFF image:

Steps: Convert PDF to TIFF in Python

  1. Create an object of the Document class.
  2. Create TiffSettings and TiffDevice objects
  3. Call the process method to convert the PDF document to TIFF.
  4. To set the output file’s properties, use the TiffSettings class.

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


    import aspose.pdf as ap

    input_pdf = DIR_INPUT + "sample.pdf"
    output_pdf = DIR_OUTPUT + "convert_pdf_to_tiff.tiff"
    # Open PDF document
    document = ap.Document(input_pdf)

    # Create Resolution object
    resolution = ap.devices.Resolution(300)

    # Create TiffSettings object
    tiffSettings = ap.devices.TiffSettings()
    tiffSettings.compression = ap.devices.CompressionType.LZW
    tiffSettings.depth = ap.devices.ColorDepth.DEFAULT
    tiffSettings.skip_blank_pages = False

    # Create TIFF device
    tiffDevice = ap.devices.TiffDevice(resolution, tiffSettings)

    # Convert a particular page and save the image to stream
    tiffDevice.process(document, output_pdf)

Convert PDF using ImageDevice class

ImageDevice is the ancestor 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.

Let’s take a look at how to convert a PDF page to an image.

BmpDevice class provides a method named process which allows you to convert a particular page of the PDF file to BMP image format. The other classes have the same method. So, if we need to convert a PDF page to an image, we just instantiate the required class.

The following steps and code snippet in Python shows this possibility

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

  1. Load the PDF file using Document class.
  2. Create an instance of subclass of ImageDevice i.e.
  3. Call the ImageDevice.Process() method to perform PDF to Image conversion.

Convert PDF to BMP


    import aspose.pdf as ap

    input_pdf = DIR_INPUT + "many_pages.pdf"
    output_pdf = DIR_OUTPUT + "convert_pdf_to_bmp"
    # Open PDF document
    document = ap.Document(input_pdf)

    # Create Resolution object
    resolution = ap.devices.Resolution(300)
    device = ap.devices.BmpDevice(resolution)

    for i in range(0, len(document.pages)):
        # Create file for save
        imageStream = io.FileIO(
            output_pdf + "_page_" + str(i + 1) + "_out.bmp", 'x'
        )
        # Convert a particular page and save the image to stream
        device.process(document.pages[i + 1], imageStream)
        imageStream.close()

Convert PDF to EMF


    import aspose.pdf as ap

    input_pdf = DIR_INPUT + "sample.pdf"
    output_pdf = DIR_OUTPUT + "convert_pdf_to_emf"
    # Open PDF document
    document = ap.Document(input_pdf)

    # Create Resolution object
    resolution = ap.devices.Resolution(300)
    device = ap.devices.EmfDevice(resolution)

    for i in range(0, len(document.pages)):
        # Create file for save
        imageStream = io.FileIO(
            output_pdf + "_page_" + str(i + 1) + "_out.emf", 'x'
        )
        # Convert a particular page and save the image to stream
        device.process(document.pages[i + 1], imageStream)
        imageStream.close()

Convert PDF to JPEG


    import aspose.pdf as ap

    input_pdf = DIR_INPUT + "many_pages.pdf"
    output_pdf = DIR_OUTPUT + "convert_pdf_to_jpeg"
    # Open PDF document
    document = ap.Document(input_pdf)

    # Create Resolution object
    resolution = ap.devices.Resolution(300)
    device = ap.devices.JpegDevice(resolution)

    for i in range(0, len(document.pages)):
        # Create file for save
        imageStream = io.FileIO(
            output_pdf + "_page_" + str(i + 1) + "_out.jpeg", "x"
        )
        # Convert a particular page and save the image to stream
        device.process(document.pages[i + 1], imageStream)
        imageStream.close()  

Convert PDF to PNG


    import aspose.pdf as ap

    input_pdf = DIR_INPUT + "sample.pdf"
    output_pdf = DIR_OUTPUT + "convert_pdf_to_png"
    # Open PDF document
    document = ap.Document(input_pdf)

    # Create Resolution object
    resolution = ap.devices.Resolution(300)
    device = ap.devices.PngDevice(resolution)

    for i in range(0, len(document.pages)):
        # Create file for save
        imageStream = io.FileIO(
            output_pdf + "_page_" + str(i + 1) + "_out.png", 'x'
        )
        # Convert a particular page and save the image to stream
        device.process(document.pages[i + 1], imageStream)
        imageStream.close()

Convert PDF to GIF


    import aspose.pdf as ap

    input_pdf = DIR_INPUT + "many_pages.pdf"
    output_pdf = DIR_OUTPUT + "convert_pdf_to_gif"
    # Open PDF document
    document = ap.Document(input_pdf)

    # Create Resolution object
    resolution = ap.devices.Resolution(300)

    device = ap.devices.GifDevice(resolution)

    for i in range(0, len(document.pages)):
        # Create file for save
        imageStream = io.FileIO(
            output_pdf + "_page_" + str(i + 1) + "_out.gif", 'x'
        )
        # Convert a particular page and save the image to stream
        device.process(document.pages[i + 1], imageStream)
        # Close stream
        imageStream.close()  

Convert PDF using SaveOptions class

This part of article shows you how to convert PDF to SVG using Python and SaveOptions class.

Scalable Vector Graphics (SVG) is a family of specifications of an XML-based file format for two-dimensional vector graphics, both static and dynamic (interactive or animated). The SVG specification is an open standard that has been under development by the World Wide Web Consortium (W3C) since 1999.

SVG images and their behaviors are defined in XML text files. This means that they can be searched, indexed, scripted and if required, compressed. As XML files, SVG images can be created and edited with any text editor, but it is often more convenient to create them with drawing programs such as Inkscape.

Aspose.PDF for Python supports the feature to convert SVG image to PDF format and also offers the capability to convert PDF files to SVG format. To accomplish this requirement, the SvgSaveOptions class has been introduced into the Aspose.PDF namespace. Instantiate an object of SvgSaveOptions and pass it as a second argument to the Document.Save() method.

The following code snippet shows the steps for converting a PDF file to SVG format with Python.

Steps: Convert PDF to SVG in Python

  1. Create an object of the Document class.
  2. Create SvgSaveOptions object with needed settings.
  3. Call the Document.Save() method and pass it SvgSaveOptions object convert the PDF document to SVG.

Convert PDF to SVG


    import aspose.pdf as ap

    input_pdf = DIR_INPUT + "sample.pdf"
    output_pdf = DIR_OUTPUT + "convert_pdf_to_svg.svg"
    # Open PDF document
    document = ap.Document(input_pdf)

    # Instantiate an object of SvgSaveOptions
    saveOptions = ap.SvgSaveOptions()

    # Do not compress SVG image to Zip archive
    saveOptions.compress_output_to_zip_archive = False
    saveOptions.treat_target_file_name_as_directory = True

    # Save the output in SVG files
    document.save(output_pdf, saveOptions)