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 TiffDevice.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.



from asposepdf import Api, Device

# init license
documentName = "testdata/license/Aspose.PDF.PythonviaJava.lic"
licenseObject = Api.License()
licenseObject.setLicense(documentName)

# Open PDF document
DIR_INPUT = "testdata/"
DIR_OUTPUT = "testout/"
input_pdf = DIR_INPUT + "source.pdf"
output_image = DIR_OUTPUT + "image.tiff"
# Open PDF document
document = Api.Document(input_pdf)
# Create Resolution object
resolution = Device.Resolution(300)

# Create TiffSettings object
tiffSettings = Device.TiffSettings()
tiffSettings._CompressionType = Device.CompressionType.LZW
tiffSettings._ColorDepth = Device.ColorDepth.Default
tiffSettings._Skip_blank_pages = False

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

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

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



from asposepdf import Api, Device

DIR_INPUT = "testdata/"
DIR_OUTPUT = "testout/"

input_pdf = DIR_INPUT + "source.pdf"
output_pdf = DIR_OUTPUT + "image"
# Open PDF document
document = Api.Document(input_pdf)

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

for i in range(0, document.getPages.size):
    # Create filename for save
    imageFileName = output_pdf + "_page_" + str(i + 1) + "_out.bmp"
    # Convert a particular page and save the image to file
    device.process(document.getPages.getPage(i + 1), outputFileName=imageFileName)

Convert PDF to EMF


from asposepdf import Api, Device

DIR_INPUT = "../../testdata/"
DIR_OUTPUT = "../../testout/"

input_pdf = DIR_INPUT + "source.pdf"
output_pdf = DIR_OUTPUT + "image"
# Open PDF document
document = Api.Document(input_pdf)

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

for i in range(0, document.getPages.size):
    # Create filename for save
    imageFileName = output_pdf + "_page_" + str(i + 1) + "_out.emf"
    # Convert a particular page and save the image to file
    device.process(document.getPages.getPage(i + 1), outputFileName=imageFileName)

Convert PDF to JPEG


from asposepdf import Api, Device

DIR_INPUT = "../../testdata/"
DIR_OUTPUT = "../../testout/"

input_pdf = DIR_INPUT + "source.pdf"
output_pdf = DIR_OUTPUT + "image"
# Open PDF document
document = Api.Document(input_pdf)

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

for i in range(0, document.getPages.size):
    # Create filename for save
    imageFileName = output_pdf + "_page_" + str(i + 1) + "_out.jpeg"
    # Convert a particular page and save the image to file
    device.process(document.getPages.getPage(i + 1), outputFileName=imageFileName)

Convert PDF to PNG


from asposepdf import Api, Device

DIR_INPUT = "../../testdata/"
DIR_OUTPUT = "../../testout/"

input_pdf = DIR_INPUT + "source.pdf"
output_pdf = DIR_OUTPUT + "image"
# Open PDF document
document = Api.Document(input_pdf)

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

for i in range(0, document.getPages.size):
    # Create filename for save
    imageFileName = output_pdf + "_page_" + str(i + 1) + "_out.png"
    # Convert a particular page and save the image to file
    device.process(document.getPages.getPage(i + 1), outputFileName=imageFileName)

Convert PDF to GIF


from asposepdf import Api, Device

DIR_INPUT = "../../testdata/"
DIR_OUTPUT = "../../testout/"

input_pdf = DIR_INPUT + "source.pdf"
output_pdf = DIR_OUTPUT + "image"
# Open PDF document
document = Api.Document(input_pdf)

# Create Resolution object
resolution = Device.Resolution(300)
device = Device.GifDevice(resolution)

for i in range(0, document.getPages.size):
    # Create filename for save
    imageFileName = output_pdf + "_page_" + str(i + 1) + "_out.gif"
    # Convert a particular page and save the image to file
    device.process(document.getPages.getPage(i + 1), outputFileName=imageFileName)

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


from asposepdf import Api

documentName = "testdata/input.pdf"
doc = Api.Document(documentName, None)
documentOutName = "testout/out.svg"
doc.save(documentOutName, Api.SaveFormat.Svg)