Table of Contents
Install with Pip
pip install Spire.PDF
Related Links
Converting PDF to image offers several benefits, including enhanced compatibility, improved editing flexibility, simplified sharing, and optimized visual representation. Converting PDFs to images ensures compatibility across various platforms and devices, as images are widely supported and easily viewable. It also allows for more flexible editing options, as images can be edited using a range of graphic design software.
Additionally, converting PDFs to images simplifies sharing, as image files are smaller in size and can be easily shared via email or across different digital platforms. Finally, by converting PDFs to images, the visual content is optimized for better representation, making it easier to integrate into presentations, websites, or other visual projects.
This article will demonstrate how to convert PDF files to different image formats using Python. The supported formats include PNG, JPG, EMF, SVG, and TIFF.
- Convert a PDF Document to PNG, JPG, GIF, BMP, EMF Files
- Convert a PDF Document to SVG Files
- Convert a PDF Document to a Multi-Page TIFF File
Python Library for PDF to Image Conversion
Among the many libraries on the market that support converting PDF to images, Spire.PDF for Python provides the most efficient conversion interface. It offers flexibility in choosing the output format, resolution, and page range for conversion. Using it, you're able to convert individual PDF pages or the entire document to both Bitmap graphics and Vector graphics with a few lines of code.
- Bitmap graphics are made of pixels, are resolution-dependent, and are suitable for realistic images. Examples of bitmap formats include JPG, PNG, and GIF.
- Vector graphics are based on mathematical equations, are resolution-independent, and are best for scalable illustrations and designs. Formats such as SVG and AI are examples of vector graphics formats.
Before you're able to run the code snippets given below, you'll need to install Spire.PDF for Python through the following pip command.
pip install Spire.PDF
Convert a PDF Document to PNG, JPG, GIF, BMP, EMF Files in Python
Spire.PDF for Python provides the SaveAsImage method, which enables users to convert specific pages of a PDF document into a Bitmap image stream. This image stream can then be exported as an image file with a PNG, JPG, BPM, GIF, or EMF extension.
The following code snippet shows you how to convert a PDF document to multiple bitmap image files in Python. One PDF page produces one image file.
- Python
from spire.pdf.common import * from spire.pdf import * # Create a PdfDocument object doc = PdfDocument() # Load a PDF document doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf") # Iterate through all pages in the document for i in range(doc.Pages.Count): # Convert a specific page to an image stream with doc.SaveAsImage(i) as image: # Save the image stream as a PNG file image.Save("Output\\ConversionResults\\ToImage-{0:d}.png".format(i)) # Save the image stream as a JPG file image.Save("Output\\ConversionResults\\ToImage-{0:d}.jpg".format(i)) # Save the image stream as a BMP file image.Save("Output\\ConversionResults\\ToImage-{0:d}.bmp".format(i)) # Save the image stream as a GIF file image.Save("Output\\ConversionResults\\ToImage-{0:d}.gif".format(i)) # Save the image stream as an EMF file image.Save("Output\\ConversionResults\\ToImage-{0:d}.emf".format(i)) # Close the document doc.Close()
Convert a PDF Document to SVG Files in Python
To complete the conversion from a PDF to SVG files, you can call the SaveToFile(filename: str, startIndex: int, endIndex: int, fileFormat: FileFormat) method and specify the output file format as SVG. The following Python code snippet loads a PDF document and saves each page as an individual SVG file.
- Python
from spire.pdf.common import * from spire.pdf import * # Create a PdfDocument object doc = PdfDocument() # Load a PDF document doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf") # Iterate through all pages in the document for i in range(doc.Pages.Count): # Convert a specific page to a SVG file doc.SaveToFile("Output\\ConversionResults\\ToImage-{0:d}.svg".format(i), i, i, FileFormat.SVG) # Close the document doc.Close()
Convert a PDF Document to a Multi-Page TIFF File in Python
Spire.PDF for Python does not provide a straightforward method to convert PDF to TIFF. However, you can combine the Bitmap graphics generated by Spire.PDF in a single TIFF document. This requires an additional installation of Python Imaging Library (PIL), which is a free and open source library for manipulating different image file formats using Python language.
pip install Pillow
The following code shows you how to convert a PDF document to a multi-page TIFF file using Spire.PDF for Python and PIL.
- Python
from spire.pdf.common import * from spire.pdf import * from PIL import Image from io import BytesIO # Create a PdfDocument object doc = PdfDocument() # Load a PDF document doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf") # Create an empty list to store PIL Images images = [] # Iterate through all pages in the document for i in range(doc.Pages.Count): # Convert a specific page to an image stream with doc.SaveAsImage(i) as imageData: # Open the image stream as a PIL image img = Image.open(BytesIO(imageData.ToArray())) # Append the PIL image to list images.append(img) # Save the PIL Images as a multi-page TIFF file images[0].save("Output/Result.tiff", save_all=True, append_images=images[1:]) # Close the document doc.Close()
Conclusion
In this blog post, we have explored how to convert PDF to popular image file formats like PNG, JPG, SVG and TIFF using Spire.PDF for Python. With the help of this PDF library, you can convert a particular page or the entire PDF document into high-quality images. On the contrary, this library allows you to convert image to PDF as well.
For more tutorials, please check out our online documentation. If you have any questions, feel free to contact us via the Spire.PDF forum.