SoFunction
Updated on 2025-04-13

Python realizes the interconversion between PDF and multiple image formats (PNG, JPG, BMP, EMF, SVG)

1. Introduction

PDFs and pictures are commonly used file formats in our daily lives and work. Sometimes, we may need to format PDFs and images to meet different application scenarios and usage needs. For example, converting PDF to images is more conducive to displaying file content on web pages or slideshows, while converting images to PDF can integrate multiple pictures into one document, making it easier to do document archiving, e-book production, printing or sharing. This article will explore how to use Python to convert PDF and multiple image formats.

2. Install Python library

We need to use the Python class library to realize the interchange of PDF and image formats. The class library used in this article is for Python, it can convert multiple picture formats to PDF, or it can convert PDF into multiple picture formats. The installation process is relatively simple, just use the pip command:

pip install 

Let’s take a look at how to use this library to achieve PDF and image transfer.

3. Python implements multiple image formats to PDF

Image conversion to PDF can have different scenarios. For example, we can convert a single image to a PDF, or multiple images to a PDF. These two scenarios will be introduced separately below.

1. Convert a single picture to PDF

The principle of converting a single image to a PDF is to draw the image to a PDF. Here are the general steps:

  • Create a new PDF.
  • Load the image and get the width and height of the image.
  • Add a page that matches the width and height of the image to the PDF.
  • Draw the image onto the page.
  • Save the document.

Code:

from  import *
from  import *
 
# Create a PdfDocument instancedoc = PdfDocument()
 
# Delete page margins(0.0)
 
# Load a pictureimage = ("C:/Users/Administrator/Desktop/")
 
# Get the width and height of the imagewidth = 
height = 
 
# Add page to PDF and set the width and height of the page to the same as the imagepage = (SizeF(width, height))       
 
# Draw the image on the page(image, 0.0, 0.0, width, height)
 
# Save the document("Picture Transfer")
()

Note that the above code applies to PNG/JPG/BMP/EMF formats, but not SVG. If you need to convert SVG to PDF, refer to the following code:

from  import *
from  import *
 
# Create a PdfDocument instancedoc = PdfDocument()
# Load SVG("Test.svg")
 
# Save SVG as PDF("SVG", )
()

2. Convert multiple pictures into one PDF

The principle of multiple pictures to PDF is the same as single pictures to PDF, which is to add multiple pages to PDF, and then draw each picture onto the corresponding page in turn. The steps are similar to the above and are omitted here.

Code:

from  import *
from  import *
import os
 
# Create a PdfDocument instancedoc = PdfDocument()
 
# Delete page margins(0.0)
 
# Specify the image folder pathimage_dir = "C:/Users/Administrator/Desktop/Picture/"
 
# traverse pictures in foldersfor fileName in (image_dir):
    image_path = (image_dir, fileName)
 
    # Loading pictures    image = (image_path)    
    # Get the image width and height    width = 
    height = 
 
    # Add a page that is consistent with the width and height of the image to PDF    page = (SizeF(width, height))
 
    # Draw the image on the page    (image, 0.0, 0.0, width, height)
      
# Save the document("Multiple pictures")
()

4. Python implements PDF to multiple image formats

In addition to converting multiple image formats to PDF, we can also convert PDF to PNG/JPG/BMP/EMF/SVG and other image formats.

The general steps for converting PDF to PNG/JPG/BMP/EMF are as follows:

  • Load PDF.
  • Traverse the document page.
  • Convert each page to an image.

Here is the code to convert PDF to PNG/JPG/BMP/EMF:

from  import *
from  import *
 
# Create a PdfDocument instancedoc = PdfDocument()
# Load PDF("Test.pdf")
 
#Travel the document pagefor i in range():
    # Convert each page to an image separately (to convert to other image formats, just modify the image suffix)    fileName = "Output/picture-{0:d}.png".format(i)
    with (i) as imageS:
        (fileName)
 
()

If you want to convert PDF to SVG, you do not need to traverse the page. After loading the document, you can directly use the methods provided by the class library to convert. In addition, you can also set the size of the converted SVG:

from  import *
from  import *
 
# Create a PdfDocument instancedoc = PdfDocument()
# Load PDF("Test.pdf")
 
# Specify the size of the converted SVG file(600.0, 600.0)
 
# Convert each page to SVG separately("PDF to", )
()

I hope that the above content about using Python to convert PDF and multiple image formats can be helpful to you.

This is the article about Python's implementation of the mutual conversion between PDF and multiple image formats (PNG, JPG, BMP, EMF, SVG). For more related contents of Python PDF and multiple image formats, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!